@looopy-ai/core 1.1.1 → 1.1.2
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/core/loop.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { type Observable } from 'rxjs';
|
|
2
1
|
import type { AnyEvent } from '../types/event';
|
|
3
2
|
import type { Message } from '../types/message';
|
|
4
3
|
import type { LoopConfig, TurnContext } from './types';
|
|
5
|
-
export declare const runLoop: (context: TurnContext, config: LoopConfig, history: Message[]) => Observable<AnyEvent>;
|
|
4
|
+
export declare const runLoop: (context: TurnContext, config: LoopConfig, history: Message[]) => import("rxjs").Observable<AnyEvent>;
|
package/dist/core/loop.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { concat, EMPTY,
|
|
1
|
+
import { concat, EMPTY, mergeMap, of, reduce, shareReplay } from 'rxjs';
|
|
2
2
|
import { createTaskCompleteEvent, createTaskCreatedEvent, createTaskStatusEvent } from '../events';
|
|
3
3
|
import { startAgentLoopSpan } from '../observability/spans';
|
|
4
|
+
import { recursiveMerge } from '../utils/recursive-merge';
|
|
4
5
|
import { runIteration } from './iteration';
|
|
5
6
|
export const runLoop = (context, config, history) => {
|
|
6
7
|
const logger = context.logger.child({ component: 'loop' });
|
|
@@ -49,34 +50,6 @@ export const runLoop = (context, config, history) => {
|
|
|
49
50
|
}));
|
|
50
51
|
return concat(of(taskEvent, workingEvent), merged$, finalSummary$).pipe(tapFinish);
|
|
51
52
|
};
|
|
52
|
-
function recursiveMerge(initial, eventsFor, next, isStop) {
|
|
53
|
-
const seed = {
|
|
54
|
-
state: initial,
|
|
55
|
-
iteration: 0,
|
|
56
|
-
events$: eventsFor({ ...initial, iteration: 0 }).pipe(shareReplay()),
|
|
57
|
-
};
|
|
58
|
-
const iterations$ = of(seed).pipe(expand(({ state, iteration, events$ }) => events$.pipe(reduce((acc, e) => {
|
|
59
|
-
acc.events.push(e);
|
|
60
|
-
if (isStop(e))
|
|
61
|
-
acc.sawStop = true;
|
|
62
|
-
return acc;
|
|
63
|
-
}, { events: [], sawStop: false }), mergeMap(({ events, sawStop }) => {
|
|
64
|
-
if (sawStop)
|
|
65
|
-
return EMPTY;
|
|
66
|
-
return of(next(state, { iteration, events })).pipe(map((nextState) => {
|
|
67
|
-
const nextIter = iteration + 1;
|
|
68
|
-
return {
|
|
69
|
-
state: nextState,
|
|
70
|
-
iteration: nextIter,
|
|
71
|
-
events$: eventsFor({
|
|
72
|
-
...nextState,
|
|
73
|
-
iteration: nextIter,
|
|
74
|
-
}).pipe(share()),
|
|
75
|
-
};
|
|
76
|
-
}));
|
|
77
|
-
}))));
|
|
78
|
-
return iterations$.pipe(mergeMap(({ events$ }) => events$));
|
|
79
|
-
}
|
|
80
53
|
const eventsToMessages = (events) => {
|
|
81
54
|
const messages = [];
|
|
82
55
|
for (const event of events) {
|
|
@@ -88,22 +61,13 @@ const eventsToMessages = (events) => {
|
|
|
88
61
|
content: event.content,
|
|
89
62
|
});
|
|
90
63
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
id: event.toolCallId,
|
|
99
|
-
type: 'function',
|
|
100
|
-
function: {
|
|
101
|
-
name: event.toolName,
|
|
102
|
-
arguments: event.arguments,
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
],
|
|
106
|
-
});
|
|
64
|
+
if (event.finishReason === 'tool_calls' && (event.toolCalls?.length ?? 0) > 0) {
|
|
65
|
+
messages.push({
|
|
66
|
+
role: 'assistant',
|
|
67
|
+
content: '',
|
|
68
|
+
toolCalls: event.toolCalls,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
107
71
|
break;
|
|
108
72
|
case 'tool-complete':
|
|
109
73
|
messages.push({
|
package/dist/core/tools.js
CHANGED
|
@@ -43,7 +43,9 @@ export const runToolCall = (context, toolCall) => {
|
|
|
43
43
|
logger.trace({
|
|
44
44
|
success: result.success,
|
|
45
45
|
}, 'Tool execution complete');
|
|
46
|
-
return
|
|
46
|
+
return result.success
|
|
47
|
+
? createToolCompleteEvent(context, toolCall, result.result)
|
|
48
|
+
: createToolErrorEvent(context, toolCall, result.error || 'Unknown error');
|
|
47
49
|
}
|
|
48
50
|
catch (error) {
|
|
49
51
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Observable } from 'rxjs';
|
|
2
|
+
export declare function recursiveMerge<S, E>(initial: S, eventsFor: (state: S & {
|
|
3
|
+
iteration: number;
|
|
4
|
+
}) => Observable<E>, next: (state: S, info: {
|
|
5
|
+
iteration: number;
|
|
6
|
+
events: E[];
|
|
7
|
+
}) => S, isStop: (e: E) => boolean): Observable<E>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { EMPTY, expand, map, mergeMap, of, reduce, share, shareReplay, } from 'rxjs';
|
|
2
|
+
export function recursiveMerge(initial, eventsFor, next, isStop) {
|
|
3
|
+
const seed = {
|
|
4
|
+
state: initial,
|
|
5
|
+
iteration: 0,
|
|
6
|
+
events$: eventsFor({ ...initial, iteration: 0 }).pipe(shareReplay()),
|
|
7
|
+
};
|
|
8
|
+
const iterations$ = of(seed).pipe(expand(({ state, iteration, events$ }) => events$.pipe(reduce((acc, e) => {
|
|
9
|
+
acc.events.push(e);
|
|
10
|
+
if (isStop(e))
|
|
11
|
+
acc.sawStop = true;
|
|
12
|
+
return acc;
|
|
13
|
+
}, { events: [], sawStop: false }), mergeMap(({ events, sawStop }) => {
|
|
14
|
+
if (sawStop)
|
|
15
|
+
return EMPTY;
|
|
16
|
+
return of(next(state, { iteration, events })).pipe(map((nextState) => {
|
|
17
|
+
const nextIter = iteration + 1;
|
|
18
|
+
return {
|
|
19
|
+
state: nextState,
|
|
20
|
+
iteration: nextIter,
|
|
21
|
+
events$: eventsFor({
|
|
22
|
+
...nextState,
|
|
23
|
+
iteration: nextIter,
|
|
24
|
+
}).pipe(share()),
|
|
25
|
+
};
|
|
26
|
+
}));
|
|
27
|
+
}))));
|
|
28
|
+
return iterations$.pipe(mergeMap(({ events$ }) => events$));
|
|
29
|
+
}
|