@loom-node/core 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.
Files changed (130) hide show
  1. package/LICENSE +21 -0
  2. package/dist/__tests__/errors.test.d.ts +2 -0
  3. package/dist/__tests__/errors.test.d.ts.map +1 -0
  4. package/dist/__tests__/errors.test.js +32 -0
  5. package/dist/__tests__/errors.test.js.map +1 -0
  6. package/dist/__tests__/event-bus.test.d.ts +2 -0
  7. package/dist/__tests__/event-bus.test.d.ts.map +1 -0
  8. package/dist/__tests__/event-bus.test.js +34 -0
  9. package/dist/__tests__/event-bus.test.js.map +1 -0
  10. package/dist/__tests__/interceptor.test.d.ts +2 -0
  11. package/dist/__tests__/interceptor.test.d.ts.map +1 -0
  12. package/dist/__tests__/interceptor.test.js +38 -0
  13. package/dist/__tests__/interceptor.test.js.map +1 -0
  14. package/dist/agent/agent-loop.d.ts +23 -0
  15. package/dist/agent/agent-loop.d.ts.map +1 -0
  16. package/dist/agent/agent-loop.js +38 -0
  17. package/dist/agent/agent-loop.js.map +1 -0
  18. package/dist/agent/agent.d.ts +52 -0
  19. package/dist/agent/agent.d.ts.map +1 -0
  20. package/dist/agent/agent.js +96 -0
  21. package/dist/agent/agent.js.map +1 -0
  22. package/dist/agent/index.d.ts +5 -0
  23. package/dist/agent/index.d.ts.map +1 -0
  24. package/dist/agent/index.js +3 -0
  25. package/dist/agent/index.js.map +1 -0
  26. package/dist/agent/strategies/react.d.ts +7 -0
  27. package/dist/agent/strategies/react.d.ts.map +1 -0
  28. package/dist/agent/strategies/react.js +105 -0
  29. package/dist/agent/strategies/react.js.map +1 -0
  30. package/dist/agent/strategies/stream-helper.d.ts +8 -0
  31. package/dist/agent/strategies/stream-helper.d.ts.map +1 -0
  32. package/dist/agent/strategies/stream-helper.js +42 -0
  33. package/dist/agent/strategies/stream-helper.js.map +1 -0
  34. package/dist/agent/strategies/tool-use.d.ts +7 -0
  35. package/dist/agent/strategies/tool-use.d.ts.map +1 -0
  36. package/dist/agent/strategies/tool-use.js +100 -0
  37. package/dist/agent/strategies/tool-use.js.map +1 -0
  38. package/dist/context/context-orchestrator.d.ts +25 -0
  39. package/dist/context/context-orchestrator.d.ts.map +1 -0
  40. package/dist/context/context-orchestrator.js +82 -0
  41. package/dist/context/context-orchestrator.js.map +1 -0
  42. package/dist/errors/agent.d.ts +10 -0
  43. package/dist/errors/agent.d.ts.map +1 -0
  44. package/dist/errors/agent.js +18 -0
  45. package/dist/errors/agent.js.map +1 -0
  46. package/dist/errors/index.d.ts +4 -0
  47. package/dist/errors/index.d.ts.map +1 -0
  48. package/dist/errors/index.js +4 -0
  49. package/dist/errors/index.js.map +1 -0
  50. package/dist/errors/llm.d.ts +23 -0
  51. package/dist/errors/llm.d.ts.map +1 -0
  52. package/dist/errors/llm.js +48 -0
  53. package/dist/errors/llm.js.map +1 -0
  54. package/dist/errors/tool.d.ts +15 -0
  55. package/dist/errors/tool.d.ts.map +1 -0
  56. package/dist/errors/tool.js +27 -0
  57. package/dist/errors/tool.js.map +1 -0
  58. package/dist/events/event-bus.d.ts +89 -0
  59. package/dist/events/event-bus.d.ts.map +1 -0
  60. package/dist/events/event-bus.js +63 -0
  61. package/dist/events/event-bus.js.map +1 -0
  62. package/dist/events/index.d.ts +3 -0
  63. package/dist/events/index.d.ts.map +1 -0
  64. package/dist/events/index.js +2 -0
  65. package/dist/events/index.js.map +1 -0
  66. package/dist/index.d.ts +26 -0
  67. package/dist/index.d.ts.map +1 -0
  68. package/dist/index.js +19 -0
  69. package/dist/index.js.map +1 -0
  70. package/dist/interceptor/index.d.ts +3 -0
  71. package/dist/interceptor/index.d.ts.map +1 -0
  72. package/dist/interceptor/index.js +2 -0
  73. package/dist/interceptor/index.js.map +1 -0
  74. package/dist/interceptor/interceptor.d.ts +17 -0
  75. package/dist/interceptor/interceptor.d.ts.map +1 -0
  76. package/dist/interceptor/interceptor.js +18 -0
  77. package/dist/interceptor/interceptor.js.map +1 -0
  78. package/dist/session/index.d.ts +3 -0
  79. package/dist/session/index.d.ts.map +1 -0
  80. package/dist/session/index.js +2 -0
  81. package/dist/session/index.js.map +1 -0
  82. package/dist/session/session.d.ts +14 -0
  83. package/dist/session/session.d.ts.map +1 -0
  84. package/dist/session/session.js +12 -0
  85. package/dist/session/session.js.map +1 -0
  86. package/dist/types/amoeba.d.ts +107 -0
  87. package/dist/types/amoeba.d.ts.map +1 -0
  88. package/dist/types/amoeba.js +2 -0
  89. package/dist/types/amoeba.js.map +1 -0
  90. package/dist/types/context.d.ts +25 -0
  91. package/dist/types/context.d.ts.map +1 -0
  92. package/dist/types/context.js +3 -0
  93. package/dist/types/context.js.map +1 -0
  94. package/dist/types/events.d.ts +53 -0
  95. package/dist/types/events.d.ts.map +1 -0
  96. package/dist/types/events.js +2 -0
  97. package/dist/types/events.js.map +1 -0
  98. package/dist/types/index.d.ts +11 -0
  99. package/dist/types/index.d.ts.map +1 -0
  100. package/dist/types/index.js +11 -0
  101. package/dist/types/index.js.map +1 -0
  102. package/dist/types/knowledge.d.ts +95 -0
  103. package/dist/types/knowledge.d.ts.map +1 -0
  104. package/dist/types/knowledge.js +2 -0
  105. package/dist/types/knowledge.js.map +1 -0
  106. package/dist/types/llm.d.ts +49 -0
  107. package/dist/types/llm.d.ts.map +1 -0
  108. package/dist/types/llm.js +2 -0
  109. package/dist/types/llm.js.map +1 -0
  110. package/dist/types/memory.d.ts +44 -0
  111. package/dist/types/memory.d.ts.map +1 -0
  112. package/dist/types/memory.js +2 -0
  113. package/dist/types/memory.js.map +1 -0
  114. package/dist/types/message.d.ts +30 -0
  115. package/dist/types/message.d.ts.map +1 -0
  116. package/dist/types/message.js +2 -0
  117. package/dist/types/message.js.map +1 -0
  118. package/dist/types/skill.d.ts +58 -0
  119. package/dist/types/skill.d.ts.map +1 -0
  120. package/dist/types/skill.js +2 -0
  121. package/dist/types/skill.js.map +1 -0
  122. package/dist/types/strategy.d.ts +25 -0
  123. package/dist/types/strategy.d.ts.map +1 -0
  124. package/dist/types/strategy.js +2 -0
  125. package/dist/types/strategy.js.map +1 -0
  126. package/dist/types/tool.d.ts +31 -0
  127. package/dist/types/tool.d.ts.map +1 -0
  128. package/dist/types/tool.js +2 -0
  129. package/dist/types/tool.js.map +1 -0
  130. package/package.json +28 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Loom Node Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=errors.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/errors.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,32 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { LoomError, LLMRateLimitError, LLMAuthError } from '../errors/llm.js';
3
+ describe('LoomError', () => {
4
+ it('creates with code and message', () => {
5
+ const err = new LoomError('TEST', 'test message');
6
+ expect(err.code).toBe('TEST');
7
+ expect(err.message).toBe('test message');
8
+ expect(err).toBeInstanceOf(Error);
9
+ });
10
+ it('wraps unknown errors via from()', () => {
11
+ const err = LoomError.from('string error');
12
+ expect(err.code).toBe('UNKNOWN');
13
+ expect(err.message).toBe('string error');
14
+ });
15
+ it('passes through existing LoomError', () => {
16
+ const original = new LoomError('X', 'y');
17
+ expect(LoomError.from(original)).toBe(original);
18
+ });
19
+ });
20
+ describe('LLM Errors', () => {
21
+ it('LLMRateLimitError has 429 status', () => {
22
+ const err = new LLMRateLimitError('openai', 5000);
23
+ expect(err.statusCode).toBe(429);
24
+ expect(err.retryAfterMs).toBe(5000);
25
+ expect(err.provider).toBe('openai');
26
+ });
27
+ it('LLMAuthError has 401 status', () => {
28
+ const err = new LLMAuthError('openai');
29
+ expect(err.statusCode).toBe(401);
30
+ });
31
+ });
32
+ //# sourceMappingURL=errors.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.test.js","sourceRoot":"","sources":["../../src/__tests__/errors.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAY,iBAAiB,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAExF,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,EAAE,CAAC,+BAA+B,EAAE,GAAG,EAAE;QACvC,MAAM,GAAG,GAAG,IAAI,SAAS,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;QAClD,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC9B,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QACzC,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC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@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=event-bus.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-bus.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/event-bus.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,34 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { TypedEventBus } from '../events/event-bus.js';
3
+ describe('TypedEventBus', () => {
4
+ it('emits and receives events', () => {
5
+ const bus = new TypedEventBus();
6
+ const received = [];
7
+ bus.on('tool:call', (data) => received.push(data));
8
+ bus.emit('tool:call', { name: 'test', input: 'hello' });
9
+ expect(received).toEqual([{ name: 'test', input: 'hello' }]);
10
+ });
11
+ it('unsubscribes via returned function', () => {
12
+ const bus = new TypedEventBus();
13
+ const received = [];
14
+ const unsub = bus.on('error', (data) => received.push(data));
15
+ bus.emit('error', { source: 'a', error: new Error('x') });
16
+ unsub();
17
+ bus.emit('error', { source: 'b', error: new Error('y') });
18
+ expect(received).toHaveLength(1);
19
+ });
20
+ it('swallows listener errors', () => {
21
+ const bus = new TypedEventBus();
22
+ bus.on('agent:start', () => { throw new Error('boom'); });
23
+ expect(() => bus.emit('agent:start', { taskId: '1', input: 'hi' })).not.toThrow();
24
+ });
25
+ it('supports multiple listeners', () => {
26
+ const bus = new TypedEventBus();
27
+ let count = 0;
28
+ bus.on('agent:done', () => count++);
29
+ bus.on('agent:done', () => count++);
30
+ bus.emit('agent:done', { taskId: '1', output: 'done' });
31
+ expect(count).toBe(2);
32
+ });
33
+ });
34
+ //# sourceMappingURL=event-bus.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-bus.test.js","sourceRoot":"","sources":["../../src/__tests__/event-bus.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAEvD,QAAQ,CAAC,e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AAE;QAClC,MAAM,GAAG,GAAG,IAAI,aAAa,EAAE,CAAC;QAChC,GAAG,CAAC,EAAE,CAAC,aAAa,EAAE,GAAG,EAAE,GAAG,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1D,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IACpF,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,6BAA6B,EAAE,GAAG,EAAE;QACrC,MAAM,GAAG,GAAG,IAAI,aAAa,EAAE,CAAC;QAChC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,GAAG,CAAC,EAAE,CAAC,YAAY,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC;QACpC,GAAG,CAAC,EAAE,CAAC,YAAY,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC;QACpC,GAAG,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QACxD,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interceptor.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interceptor.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/interceptor.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,38 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { InterceptorChain } from '../interceptor/interceptor.js';
3
+ describe('InterceptorChain', () => {
4
+ it('runs interceptors in order', async () => {
5
+ const order = [];
6
+ const a = { name: 'a', intercept: async (_ctx, next) => { order.push(1); await next(); order.push(3); } };
7
+ const b = { name: 'b', intercept: async (_ctx, next) => { order.push(2); await next(); } };
8
+ const chain = new InterceptorChain([a, b]);
9
+ await chain.run({ messages: [], metadata: {} });
10
+ expect(order).toEqual([1, 2, 3]);
11
+ });
12
+ it('allows interceptor to modify messages', async () => {
13
+ const injector = {
14
+ name: 'injector',
15
+ intercept: async (ctx, next) => {
16
+ ctx.messages.push({ role: 'system', content: 'injected' });
17
+ await next();
18
+ },
19
+ };
20
+ const chain = new InterceptorChain([injector]);
21
+ const ctx = { messages: [], metadata: {} };
22
+ await chain.run(ctx);
23
+ expect(ctx.messages).toHaveLength(1);
24
+ expect(ctx.messages[0].content).toBe('injected');
25
+ });
26
+ it('works with empty chain', async () => {
27
+ const chain = new InterceptorChain([]);
28
+ await expect(chain.run({ messages: [], metadata: {} })).resolves.toBeUndefined();
29
+ });
30
+ it('supports use() to add interceptors', async () => {
31
+ const chain = new InterceptorChain();
32
+ let called = false;
33
+ chain.use({ name: 'x', intercept: async (_ctx, next) => { called = true; await next(); } });
34
+ await chain.run({ messages: [], metadata: {} });
35
+ expect(called).toBe(true);
36
+ });
37
+ });
38
+ //# sourceMappingURL=interceptor.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interceptor.test.js","sourceRoot":"","sources":["../../src/__tests__/interceptor.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAGjE,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAChC,EAAE,CAAC,4BAA4B,EAAE,KAAK,IAAI,EAAE;QAC1C,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,MAAM,CAAC,GAAgB,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,GAA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MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;QACrC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACnD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wBAAwB,EAAE,KAAK,IAAI,EAAE;QACtC,MAAM,KAAK,GAAG,IAAI,gBAAgB,CAAC,EAAE,CAAC,CAAC;QACvC,MAAM,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,aAAa,EAAE,CAAC;IACnF,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oCAAoC,EAAE,KAAK,IAAI,EAAE;QAClD,MAAM,KAAK,GAAG,IAAI,gBAAgB,EAAE,CAAC;QACrC,IAAI,MAAM,GAAG,KAAK,CAAC;QACnB,KAAK,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,GAAG,MAAM,GAAG,IAAI,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAC5F,MAAM,KAAK,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC;QAChD,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { Message } from '../types/message.js';
2
+ import type { AgentEvent } from '../types/events.js';
3
+ import type { LLMProvider } from '../types/llm.js';
4
+ import type { ToolDefinition, ToolExecutionConfig } from '../types/tool.js';
5
+ import type { InterceptorChain } from '../interceptor/interceptor.js';
6
+ import type { TypedEventBus } from '../events/event-bus.js';
7
+ import type { LoopStrategy } from '../types/strategy.js';
8
+ export interface AgentLoopConfig {
9
+ provider: LLMProvider;
10
+ tools: ToolDefinition[];
11
+ interceptors?: InterceptorChain;
12
+ events?: TypedEventBus;
13
+ systemPrompt?: string;
14
+ maxSteps: number;
15
+ signal?: AbortSignal;
16
+ toolConfig?: ToolExecutionConfig;
17
+ strategy?: LoopStrategy;
18
+ streaming?: boolean;
19
+ sessionId?: string;
20
+ tenantId?: string;
21
+ }
22
+ export declare function agentLoop(messages: Message[], config: AgentLoopConfig): AsyncGenerator<AgentEvent>;
23
+ //# sourceMappingURL=agent-loop.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-loop.d.ts","sourceRoot":"","sources":["../../src/agent/agent-loop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAGzD,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,WAAW,CAAC;IACtB,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,gBAAgB,CAAC;IAChC,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,UAAU,CAAC,EAAE,mBAAmB,CAAC;IACjC,QAAQ,CAAC,EAAE,YAAY,CAAC;IACxB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,wBAAuB,SAAS,CAC9B,QAAQ,EAAE,OAAO,EAAE,EACnB,MAAM,EAAE,eAAe,GACtB,cAAc,CAAC,UAAU,CAAC,CAsB5B"}
@@ -0,0 +1,38 @@
1
+ import { ToolUseStrategy } from './strategies/tool-use.js';
2
+ export async function* agentLoop(messages, config) {
3
+ const strategy = config.strategy ?? new ToolUseStrategy();
4
+ const gen = strategy.execute({
5
+ messages,
6
+ provider: config.provider,
7
+ tools: config.tools,
8
+ interceptors: config.interceptors,
9
+ events: config.events,
10
+ systemPrompt: config.systemPrompt,
11
+ maxSteps: config.maxSteps,
12
+ signal: config.signal,
13
+ toolConfig: config.toolConfig,
14
+ streaming: config.streaming,
15
+ sessionId: config.sessionId,
16
+ tenantId: config.tenantId,
17
+ });
18
+ const bus = config.events;
19
+ for await (const event of gen) {
20
+ if (bus)
21
+ emitToBus(bus, event);
22
+ yield event;
23
+ }
24
+ }
25
+ function emitToBus(bus, event) {
26
+ switch (event.type) {
27
+ case 'tool-call-start':
28
+ bus.emit('tool:call', { name: event.toolName, input: event.input });
29
+ break;
30
+ case 'tool-call-end':
31
+ bus.emit('tool:result', { name: event.toolName, output: event.result, durationMs: event.durationMs });
32
+ break;
33
+ case 'error':
34
+ bus.emit('error', { source: 'agent', error: event.error });
35
+ break;
36
+ }
37
+ }
38
+ //# sourceMappingURL=agent-loop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-loop.js","sourceRoot":"","sources":["../../src/agent/agent-loop.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAiB3D,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,SAAS,CAC9B,QAAmB,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AAC,CAAC;YACtG,MAAM;QACR,KAAK,OAAO;YACV,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;YAC3D,MAAM;IACV,CAAC;AACH,CAAC"}
@@ -0,0 +1,52 @@
1
+ import type { AgentEvent, DoneEvent } from '../types/events.js';
2
+ import type { LLMProvider } from '../types/llm.js';
3
+ import type { ToolDefinition, ToolExecutionConfig } from '../types/tool.js';
4
+ import type { MemoryManager } from '../types/memory.js';
5
+ import type { ContextOrchestrator } from '../types/context.js';
6
+ import type { LoopStrategy } from '../types/strategy.js';
7
+ import { InterceptorChain } from '../interceptor/interceptor.js';
8
+ import { TypedEventBus } from '../events/event-bus.js';
9
+ export interface AgentConfig {
10
+ name: string;
11
+ provider: LLMProvider;
12
+ systemPrompt?: string;
13
+ tools?: ToolDefinition[];
14
+ memory?: MemoryManager;
15
+ context?: ContextOrchestrator;
16
+ contextBudget?: number;
17
+ interceptors?: InterceptorChain;
18
+ maxSteps?: number;
19
+ toolConfig?: ToolExecutionConfig;
20
+ strategy?: LoopStrategy;
21
+ streaming?: boolean;
22
+ eventBus?: TypedEventBus;
23
+ sessionId?: string;
24
+ tenantId?: string;
25
+ }
26
+ export declare class Agent {
27
+ readonly name: string;
28
+ private readonly provider;
29
+ private readonly systemPrompt?;
30
+ private readonly tools;
31
+ private readonly memory?;
32
+ private readonly context?;
33
+ private readonly contextBudget;
34
+ private readonly interceptors;
35
+ private readonly events;
36
+ private readonly maxSteps;
37
+ private readonly toolConfig?;
38
+ private readonly strategy?;
39
+ private readonly streaming;
40
+ private readonly sessionId?;
41
+ private readonly tenantId?;
42
+ constructor(config: AgentConfig);
43
+ stream(input: string, opts?: {
44
+ signal?: AbortSignal;
45
+ }): AsyncGenerator<AgentEvent>;
46
+ run(input: string, opts?: {
47
+ signal?: AbortSignal;
48
+ }): Promise<DoneEvent>;
49
+ on<K extends keyof import('../events/event-bus.js').AgentEventMap>(event: K, handler: (payload: import('../events/event-bus.js').AgentEventMap[K]) => void): void;
50
+ private buildMessages;
51
+ }
52
+ //# sourceMappingURL=agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/agent/agent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAGvD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,WAAW,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,cAAc,EAAE,CAAC;IACzB,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAC9B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,mBAAmB,CAAC;IACjC,QAAQ,CAAC,EAAE,YAAY,CAAC;IACxB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,Q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AI,GAC5E,IAAI;YAIO,aAAa;CAwB5B"}
@@ -0,0 +1,96 @@
1
+ import { InterceptorChain } from '../interceptor/interceptor.js';
2
+ import { TypedEventBus } from '../events/event-bus.js';
3
+ import { agentLoop } from './agent-loop.js';
4
+ export class Agent {
5
+ name;
6
+ provider;
7
+ systemPrompt;
8
+ tools;
9
+ memory;
10
+ context;
11
+ contextBudget;
12
+ interceptors;
13
+ events;
14
+ maxSteps;
15
+ toolConfig;
16
+ strategy;
17
+ streaming;
18
+ sessionId;
19
+ tenantId;
20
+ constructor(config) {
21
+ this.name = config.name;
22
+ this.provider = config.provider;
23
+ this.systemPrompt = config.systemPrompt;
24
+ this.tools = config.tools ?? [];
25
+ this.memory = config.memory;
26
+ this.context = config.context;
27
+ this.contextBudget = config.contextBudget ?? 4096;
28
+ this.interceptors = config.interceptors ?? new InterceptorChain();
29
+ this.events = config.eventBus ?? new TypedEventBus();
30
+ this.maxSteps = config.maxSteps ?? 10;
31
+ this.toolConfig = config.toolConfig;
32
+ this.strategy = config.strategy;
33
+ this.streaming = config.streaming ?? false;
34
+ this.sessionId = config.sessionId;
35
+ this.tenantId = config.tenantId;
36
+ }
37
+ async *stream(input, opts) {
38
+ const messages = await this.buildMessages(input);
39
+ yield* agentLoop(messages, {
40
+ provider: this.provider,
41
+ tools: this.tools,
42
+ interceptors: this.interceptors,
43
+ events: this.events,
44
+ systemPrompt: this.systemPrompt,
45
+ maxSteps: this.maxSteps,
46
+ signal: opts?.signal,
47
+ toolConfig: this.toolConfig,
48
+ strategy: this.strategy,
49
+ streaming: this.streaming,
50
+ sessionId: this.sessionId,
51
+ tenantId: this.tenantId,
52
+ });
53
+ }
54
+ async run(input, opts) {
55
+ let done;
56
+ for await (const event of this.stream(input, opts)) {
57
+ if (event.type === 'done')
58
+ done = event;
59
+ }
60
+ if (!done)
61
+ throw new Error('Agent loop ended without done event');
62
+ if (this.memory) {
63
+ await this.memory.persist([
64
+ { role: 'user', content: input },
65
+ { role: 'assistant', content: done.content },
66
+ ]);
67
+ }
68
+ return done;
69
+ }
70
+ on(event, handler) {
71
+ this.events.on(event, handler);
72
+ }
73
+ async buildMessages(input) {
74
+ // Build base messages: from memory (conversation history) or system prompt + user input
75
+ const msgs = this.memory
76
+ ? await this.memory.buildContext(input)
77
+ : [
78
+ ...(this.systemPrompt ? [{ role: 'system', content: this.systemPrompt }] : []),
79
+ { role: 'user', content: input },
80
+ ];
81
+ // Inject context fragments (works WITH or WITHOUT memory)
82
+ if (this.context) {
83
+ const computed = this.context.computeBudget(this.systemPrompt).available;
84
+ const budget = Number.isFinite(computed) ? computed : this.contextBudget;
85
+ const fragments = await this.context.gather(input, budget);
86
+ if (fragments.length > 0) {
87
+ const contextText = fragments.map(f => `[${f.source}] ${f.content}`).join('\n');
88
+ const sysIdx = msgs.findIndex(m => m.role === 'system');
89
+ const insertIdx = sysIdx >= 0 ? sysIdx + 1 : 0;
90
+ msgs.splice(insertIdx, 0, { role: 'system', content: contextText });
91
+ }
92
+ }
93
+ return msgs;
94
+ }
95
+ }
96
+ //# sourceMappingURL=agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/agent/agent.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAoB5C,MAAM,OAAO,KAAK;IACP,IAAI,CAAS;IACL,QAAQ,CAAc;IACtB,YAAY,CAAU;IACtB,KAAK,CAAmB;IACxB,MAAM,CAAiB;IACvB,OAAO,CAAuB;IAC9B,aAAa,CAAS;IACtB,YAAY,CAAmB;IAC/B,MAAM,CAAgB;IACtB,QAAQ,CAAS;IACjB,UAAU,CAAuB;IACjC,QAAQ,CAAgB;IACxB,SAAS,CAAU;IACnB,SAAS,CAAU;IACnB,QAAQ,CAAU;IAEnC,YAA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AAO,EAAE,WAAW,EAAE,CAAC,CAAC;YACtE,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ export { Agent } from './agent.js';
2
+ export type { AgentConfig } from './agent.js';
3
+ export { agentLoop } from './agent-loop.js';
4
+ export type { AgentLoopConfig } from './agent-loop.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/agent/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { Agent } from './agent.js';
2
+ export { agentLoop } from './agent-loop.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agent/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { AgentEvent } from '../../types/events.js';
2
+ import type { LoopStrategy, LoopContext } from '../../types/strategy.js';
3
+ export declare class ReactStrategy implements LoopStrategy {
4
+ readonly name = "react";
5
+ execute(ctx: LoopContext): AsyncGenerator<AgentEvent>;
6
+ }
7
+ //# sourceMappingURL=react.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../../src/agent/strategies/react.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAc,MAAM,uBAAuB,CAAC;AAEpE,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAUzE,qBAAa,aAAc,YAAW,YAAY;IAChD,QAAQ,CAAC,IAAI,WAAW;IAEjB,OAAO,CAAC,GAAG,EAAE,WAAW,GAAG,cAAc,CAAC,UAAU,CAAC;CA4F7D"}
@@ -0,0 +1,105 @@
1
+ import { AgentAbortError, AgentMaxStepsError } from '../../errors/agent.js';
2
+ const REACT_SUFFIX = `\nUse this format:
3
+ Thought: <reasoning about what to do>
4
+ Action: use a tool OR respond to the user
5
+ Observation: <tool result>
6
+ ... (repeat until done)
7
+ Final Answer: <your response>`;
8
+ export class ReactStrategy {
9
+ name = 'react';
10
+ async *execute(ctx) {
11
+ const { provider, tools, maxSteps, signal, messages } = ctx;
12
+ const totalUsage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
13
+ const startTime = Date.now();
14
+ let accumulatedContent = '';
15
+ // Inject ReAct framing into system prompt
16
+ if (messages.length > 0 && messages[0].role === 'system') {
17
+ messages[0] = { ...messages[0], content: messages[0].content + REACT_SUFFIX };
18
+ }
19
+ const jsonTools = tools.map(t => ({
20
+ name: t.name, description: t.description, parameters: t.parameters.toJsonSchema(),
21
+ }));
22
+ for (let step = 0; step < maxSteps; step++) {
23
+ if (signal?.aborted) {
24
+ yield { type: 'error', error: new AgentAbortError(), recoverable: false };
25
+ return;
26
+ }
27
+ yield { type: 'step-start', step, totalSteps: maxSteps };
28
+ if (ctx.interceptors) {
29
+ await ctx.interceptors.run({ messages, metadata: { step, strategy: 'react' } });
30
+ }
31
+ let result;
32
+ try {
33
+ result = await provider.complete({ messages, tools: jsonTools });
34
+ }
35
+ catch (err) {
36
+ yield { type: 'error', error: err instanceof Error ? err : new Error(String(err)), recoverable: false };
37
+ return;
38
+ }
39
+ accumulateUsage(totalUsage, result.usage);
40
+ if (result.content) {
41
+ accumulatedContent += result.content;
42
+ yield { type: 'text-delta', content: result.content };
43
+ }
44
+ // Check for Final Answer or no tool calls
45
+ const hasFinal = result.content?.includes('Final Answer:');
46
+ if (hasFinal || result.finishReason === 'stop' || !result.toolCalls?.length) {
47
+ yield { type: 'step-end', step, reason: 'complete' };
48
+ const finalContent = hasFinal
49
+ ? result.content.split('Final Answer:').pop().trim()
50
+ : accumulatedContent;
51
+ yield { type: 'done', content: finalContent, usage: totalUsage, steps: step + 1, durationMs: Date.now() - startTime };
52
+ return;
53
+ }
54
+ // Tool execution (Action → Observation)
55
+ messages.push({ role: 'assistant', content: result.content, toolCalls: result.toolCalls });
56
+ const toolMap = new Map(tools.map(t => [t.name, t]));
57
+ for (const call of result.toolCalls) {
58
+ const tool = toolMap.get(call.name);
59
+ const start = Date.now();
60
+ let parsed;
61
+ try {
62
+ parsed = JSON.parse(call.arguments);
63
+ }
64
+ catch {
65
+ parsed = {};
66
+ }
67
+ yield { type: 'tool-call-start', toolCallId: call.id, toolName: call.name, input: parsed };
68
+ if (!tool) {
69
+ messages.push({ role: 'tool', toolCallId: call.id, content: `Observation: Unknown tool: ${call.name}` });
70
+ yield { type: 'tool-call-end', toolCallId: call.id, toolName: call.name, result: null, durationMs: 0, tokenCost: 0 };
71
+ continue;
72
+ }
73
+ try {
74
+ const validInput = tool.parameters.parse(parsed);
75
+ const timeoutMs = ctx.toolConfig?.timeoutMs ?? 30_000;
76
+ const output = await withTimeout(tool.execute(validInput, { signal, sessionId: ctx.sessionId, tenantId: ctx.tenantId }), timeoutMs);
77
+ const dur = Date.now() - start;
78
+ messages.push({ role: 'tool', toolCallId: call.id, content: `Observation: ${String(output)}` });
79
+ yield { type: 'tool-call-end', toolCallId: call.id, toolName: call.name, result: output, durationMs: dur, tokenCost: 0 };
80
+ }
81
+ catch (err) {
82
+ const dur = Date.now() - start;
83
+ const msg = err instanceof Error ? err.message : String(err);
84
+ messages.push({ role: 'tool', toolCallId: call.id, content: `Observation: Error - ${msg}` });
85
+ yield { type: 'tool-call-end', toolCallId: call.id, toolName: call.name, result: null, durationMs: dur, tokenCost: 0 };
86
+ }
87
+ }
88
+ yield { type: 'step-end', step, reason: 'tool-use' };
89
+ }
90
+ yield { type: 'error', error: new AgentMaxStepsError(maxSteps, accumulatedContent), recoverable: false };
91
+ yield { type: 'done', content: accumulatedContent, usage: totalUsage, steps: maxSteps, durationMs: Date.now() - startTime };
92
+ }
93
+ }
94
+ function withTimeout(promise, ms) {
95
+ return new Promise((resolve, reject) => {
96
+ const timer = setTimeout(() => reject(new Error(`Tool timed out after ${ms}ms`)), ms);
97
+ promise.then(resolve, reject).finally(() => clearTimeout(timer));
98
+ });
99
+ }
100
+ function accumulateUsage(total, delta) {
101
+ total.promptTokens += delta.promptTokens;
102
+ total.completionTokens += delta.completionTokens;
103
+ total.totalTokens += delta.totalTokens;
104
+ }
105
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.js","sourceRoot":"","sources":["../../../src/agent/strategies/react.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAE5E,MAAM,YAAY,GAAG;;;;;8BAKS,CAAC;AAE/B,MAAM,OAAO,aAAa;IACf,IAAI,GAAG,OAAO,CAAC;IAExB,KAAK,CAAC,CAAC,OAAO,CAAC,GAAgB;QAC7B,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC;QAC5D,MAAM,UAAU,GAAe,EAAE,YAAY,EAAE,CAAC,EAAE,gBAAgB,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,CAAC;QACxF,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC7B,IAAI,kBAAkB,GAAG,EAAE,CAAC;QAE5B,0CAA0C;QAC1C,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACzD,QAAQ,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,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AAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;QACvD,CAAC;QAED,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,kBAAkB,CAAC,QAAQ,EAAE,kBAAkB,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;QACzG,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,kBAAkB,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,EAAE,CAAC;IAC9H,CAAC;CACF;AAED,SAAS,WAAW,CAAI,OAAmB,EAAE,EAAU;IACrD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,wBAAwB,EAAE,IAAI,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACtF,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;IACnE,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,eAAe,CAAC,KAAiB,EAAE,KAAiB;IAC3D,KAAK,CAAC,YAAY,IAAI,KAAK,CAAC,YAAY,CAAC;IACzC,KAAK,CAAC,gBAAgB,IAAI,KAAK,CAAC,gBAAgB,CAAC;IACjD,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,WAAW,CAAC;AACzC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { AgentEvent } from '../../types/events.js';
2
+ import type { CompletionResult, CompletionParams, LLMProvider } from '../../types/llm.js';
3
+ export interface StreamResult {
4
+ result: CompletionResult;
5
+ events: AgentEvent[];
6
+ }
7
+ export declare function streamToCompletion(provider: LLMProvider, params: CompletionParams, signal?: AbortSignal): Promise<StreamResult>;
8
+ //# sourceMappingURL=stream-helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-helper.d.ts","sourceRoot":"","sources":["../../../src/agent/strategies/stream-helper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAc,MAAM,uBAAuB,CAAC;AACpE,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,WAAW,EAAe,MAAM,oBAAoB,CAAC;AAGvG,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,gBAAgB,CAAC;IACzB,MAAM,EAAE,UAAU,EAAE,CAAC;CACtB;AAED,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,WAAW,EACrB,MAAM,EAAE,gBAAgB,EACxB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,YAAY,CAAC,CAwCvB"}
@@ -0,0 +1,42 @@
1
+ export async function streamToCompletion(provider, params, signal) {
2
+ let content = '';
3
+ const usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
4
+ const toolCallMap = new Map();
5
+ const events = [];
6
+ for await (const chunk of provider.stream(params, { signal })) {
7
+ switch (chunk.type) {
8
+ case 'text':
9
+ content += chunk.content;
10
+ events.push({ type: 'text-delta', content: chunk.content });
11
+ break;
12
+ case 'tool-call-delta': {
13
+ const existing = toolCallMap.get(chunk.toolCallId);
14
+ if (existing) {
15
+ existing.args += chunk.content;
16
+ }
17
+ else {
18
+ toolCallMap.set(chunk.toolCallId, { name: chunk.name ?? '', args: chunk.content });
19
+ }
20
+ break;
21
+ }
22
+ case 'usage':
23
+ if (chunk.usage.promptTokens)
24
+ usage.promptTokens += chunk.usage.promptTokens;
25
+ if (chunk.usage.completionTokens)
26
+ usage.completionTokens += chunk.usage.completionTokens;
27
+ if (chunk.usage.totalTokens)
28
+ usage.totalTokens += chunk.usage.totalTokens;
29
+ break;
30
+ }
31
+ }
32
+ const toolCalls = [];
33
+ for (const [id, tc] of toolCallMap) {
34
+ toolCalls.push({ id, name: tc.name, arguments: tc.args });
35
+ }
36
+ const finishReason = toolCalls.length > 0 ? 'tool_calls' : 'stop';
37
+ return {
38
+ result: { content, toolCalls: toolCalls.length > 0 ? toolCalls : undefined, usage, finishReason },
39
+ events,
40
+ };
41
+ }
42
+ //# sourceMappingURL=stream-helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-helper.js","sourceRoot":"","sources":["../../../src/agent/strategies/stream-helper.ts"],"names":[],"mappings":"AASA,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,QAAqB,EACrB,MAAwB,EACxB,MAAoB;IAEpB,IAAI,OAAO,GAAG,EAAE,CAAC;IACjB,MAAM,KAAK,GAAe,EAAE,YAAY,EAAE,CAAC,EAAE,gBAAgB,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,CAAC;IACnF,MAAM,WAAW,GAAG,IAAI,GAAG,EAA0C,CAAC;IACtE,MAAM,MAAM,GAAiB,EAAE,CAAC;IAEhC,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC;QAC9D,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;YACnB,KAAK,MAAM;gBACT,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC;gBACzB,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;gBAC5D,MAAM;YACR,KAAK,iBAAiB,CAAC,CAAC,CAAC;gBACvB,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;gBACnD,IAAI,QAAQ,EAAE,CAAC;oBACb,QAAQ,CAAC,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC;gBACjC,CAAC;qBAAM,CAAC;oBACN,WA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