@parall/agent-core 1.13.1
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/dispatch-adapter.d.ts +79 -0
- package/dist/dispatch-adapter.d.ts.map +1 -0
- package/dist/dispatch-adapter.js +1 -0
- package/dist/event-format.d.ts +4 -0
- package/dist/event-format.d.ts.map +1 -0
- package/dist/event-format.js +42 -0
- package/dist/gateway-base.d.ts +69 -0
- package/dist/gateway-base.d.ts.map +1 -0
- package/dist/gateway-base.js +865 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/routing.d.ts +25 -0
- package/dist/routing.d.ts.map +1 -0
- package/dist/routing.js +29 -0
- package/dist/session-state.d.ts +17 -0
- package/dist/session-state.d.ts.map +1 -0
- package/dist/session-state.js +54 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +35 -0
- package/src/dispatch-adapter.ts +66 -0
- package/src/event-format.ts +40 -0
- package/src/gateway-base.ts +989 -0
- package/src/index.ts +6 -0
- package/src/routing.ts +47 -0
- package/src/session-state.ts +69 -0
- package/src/types.ts +32 -0
package/src/index.ts
ADDED
package/src/routing.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { DispatchState, ParallEvent } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/** Where an inbound event should be routed. */
|
|
4
|
+
export type TriggerDisposition =
|
|
5
|
+
| { action: "main" }
|
|
6
|
+
| { action: "buffer-main" }
|
|
7
|
+
| { action: "buffer-fork"; forkKey: string }
|
|
8
|
+
| { action: "new-fork" };
|
|
9
|
+
|
|
10
|
+
/** Pluggable strategy for routing triggers when main session is busy. */
|
|
11
|
+
export type RoutingStrategy = (event: ParallEvent, state: DispatchState) => TriggerDisposition;
|
|
12
|
+
|
|
13
|
+
const MAX_CONCURRENT_FORKS = 20;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Default routing strategy:
|
|
17
|
+
* - Same target as main → buffer-main (strict per-target serial ordering)
|
|
18
|
+
* - Active fork for same target → buffer into that fork
|
|
19
|
+
* - Too many forks → buffer-main (backpressure)
|
|
20
|
+
* - Otherwise → create a new fork
|
|
21
|
+
*/
|
|
22
|
+
export const defaultRoutingStrategy: RoutingStrategy = (event, state) => {
|
|
23
|
+
if (state.mainCurrentTargetId === event.targetId) {
|
|
24
|
+
return { action: "buffer-main" };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const existingForkKey = state.activeForks.get(event.targetId);
|
|
28
|
+
if (existingForkKey) return { action: "buffer-fork", forkKey: existingForkKey };
|
|
29
|
+
|
|
30
|
+
if (state.activeForks.size >= MAX_CONCURRENT_FORKS) {
|
|
31
|
+
return { action: "buffer-main" };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return { action: "new-fork" };
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Route an inbound event based on current dispatch state. */
|
|
38
|
+
export function routeTrigger(
|
|
39
|
+
event: ParallEvent,
|
|
40
|
+
state: DispatchState,
|
|
41
|
+
strategy: RoutingStrategy = defaultRoutingStrategy,
|
|
42
|
+
): TriggerDisposition {
|
|
43
|
+
const existingForkKey = state.activeForks.get(event.targetId);
|
|
44
|
+
if (existingForkKey) return { action: "buffer-fork", forkKey: existingForkKey };
|
|
45
|
+
if (!state.mainDispatching) return { action: "main" };
|
|
46
|
+
return strategy(event, state);
|
|
47
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
function normalizeSessionKey(sessionKey: string): string {
|
|
2
|
+
return sessionKey.toLowerCase();
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
// Session key → original chat ID mapping.
|
|
6
|
+
// Runtime session keys may be normalized to lowercase, but Parall IDs are case-sensitive.
|
|
7
|
+
const sessionChatIdMap = new Map<string, string>();
|
|
8
|
+
const sessionMessageIdMap = new Map<string, string>();
|
|
9
|
+
const dispatchMessageIdMap = new Map<string, string>();
|
|
10
|
+
const dispatchGroupKeyMap = new Map<string, string>();
|
|
11
|
+
const dispatchNoReplyMap = new Map<string, boolean>();
|
|
12
|
+
|
|
13
|
+
export function setSessionChatId(sessionKey: string, chatId: string) {
|
|
14
|
+
sessionChatIdMap.set(normalizeSessionKey(sessionKey), chatId);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getSessionChatId(sessionKey: string): string | undefined {
|
|
18
|
+
return sessionChatIdMap.get(normalizeSessionKey(sessionKey));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function setSessionMessageId(sessionKey: string, messageId: string) {
|
|
22
|
+
sessionMessageIdMap.set(normalizeSessionKey(sessionKey), messageId);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getSessionMessageId(sessionKey: string): string | undefined {
|
|
26
|
+
return sessionMessageIdMap.get(normalizeSessionKey(sessionKey));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function clearSessionMessageId(sessionKey: string) {
|
|
30
|
+
sessionMessageIdMap.delete(normalizeSessionKey(sessionKey));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Snapshot the message ID at dispatch time — immune to later session-level overwrites. */
|
|
34
|
+
export function setDispatchMessageId(sessionKey: string, messageId: string) {
|
|
35
|
+
dispatchMessageIdMap.set(normalizeSessionKey(sessionKey), messageId);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Read the dispatch-time snapshot. Prefer this over getSessionMessageId for provenance. */
|
|
39
|
+
export function getDispatchMessageId(sessionKey: string): string | undefined {
|
|
40
|
+
return dispatchMessageIdMap.get(normalizeSessionKey(sessionKey));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function clearDispatchMessageId(sessionKey: string) {
|
|
44
|
+
dispatchMessageIdMap.delete(normalizeSessionKey(sessionKey));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function setDispatchGroupKey(sessionKey: string, groupKey: string) {
|
|
48
|
+
dispatchGroupKeyMap.set(normalizeSessionKey(sessionKey), groupKey);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function getDispatchGroupKey(sessionKey: string): string | undefined {
|
|
52
|
+
return dispatchGroupKeyMap.get(normalizeSessionKey(sessionKey));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function clearDispatchGroupKey(sessionKey: string) {
|
|
56
|
+
dispatchGroupKeyMap.delete(normalizeSessionKey(sessionKey));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function setDispatchNoReply(sessionKey: string, noReply: boolean) {
|
|
60
|
+
dispatchNoReplyMap.set(normalizeSessionKey(sessionKey), noReply);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function getDispatchNoReply(sessionKey: string): boolean {
|
|
64
|
+
return dispatchNoReplyMap.get(normalizeSessionKey(sessionKey)) ?? false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function clearDispatchNoReply(sessionKey: string) {
|
|
68
|
+
dispatchNoReplyMap.delete(normalizeSessionKey(sessionKey));
|
|
69
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Result from a completed fork dispatch, to be injected into main session's next turn. */
|
|
2
|
+
export type ForkResult = {
|
|
3
|
+
forkSessionKey: string;
|
|
4
|
+
sourceEvent: { type: string; targetId: string; summary: string };
|
|
5
|
+
actions: string[];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/** Mutable dispatch state for the orchestrator gateway. */
|
|
9
|
+
export type DispatchState = {
|
|
10
|
+
mainDispatching: boolean;
|
|
11
|
+
mainCurrentTargetId?: string;
|
|
12
|
+
activeForks: Map<string, string>;
|
|
13
|
+
pendingForkResults: ForkResult[];
|
|
14
|
+
mainBuffer: ParallEvent[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Normalized inbound event from Parall. */
|
|
18
|
+
export type ParallEvent = {
|
|
19
|
+
type: "message" | "task";
|
|
20
|
+
targetId: string;
|
|
21
|
+
targetName?: string;
|
|
22
|
+
targetType?: string;
|
|
23
|
+
senderId: string;
|
|
24
|
+
senderName: string;
|
|
25
|
+
messageId: string;
|
|
26
|
+
body: string;
|
|
27
|
+
threadRootId?: string;
|
|
28
|
+
noReply?: boolean;
|
|
29
|
+
mediaFields?: Record<string, string | undefined>;
|
|
30
|
+
ackSourceType?: "message" | "task_activity";
|
|
31
|
+
ackSourceId?: string;
|
|
32
|
+
};
|