@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
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { ParallClient } from "@parall/sdk";
|
|
2
|
+
import type { ParallEvent } from "./types.js";
|
|
3
|
+
export type GatewayLogger = {
|
|
4
|
+
info: (msg: string) => void;
|
|
5
|
+
warn: (msg: string) => void;
|
|
6
|
+
error: (msg: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export type DispatchContext = {
|
|
9
|
+
accountId: string;
|
|
10
|
+
apiUrl: string;
|
|
11
|
+
apiKey: string;
|
|
12
|
+
orgId: string;
|
|
13
|
+
agentUserId: string;
|
|
14
|
+
runtimeType: string;
|
|
15
|
+
runtimeKey: string;
|
|
16
|
+
sessionId?: string;
|
|
17
|
+
chatId?: string;
|
|
18
|
+
triggerMessageId?: string;
|
|
19
|
+
noReply: boolean;
|
|
20
|
+
stepIdFilePath?: string;
|
|
21
|
+
client: ParallClient;
|
|
22
|
+
log?: GatewayLogger;
|
|
23
|
+
};
|
|
24
|
+
export type RuntimeEvent = {
|
|
25
|
+
type: "thinking";
|
|
26
|
+
text: string;
|
|
27
|
+
groupKey?: string;
|
|
28
|
+
} | {
|
|
29
|
+
type: "tool_call";
|
|
30
|
+
callId: string;
|
|
31
|
+
toolName: string;
|
|
32
|
+
input: unknown;
|
|
33
|
+
startedAt?: string;
|
|
34
|
+
groupKey?: string;
|
|
35
|
+
} | {
|
|
36
|
+
type: "tool_result";
|
|
37
|
+
callId: string;
|
|
38
|
+
toolName: string;
|
|
39
|
+
output: string;
|
|
40
|
+
error?: string;
|
|
41
|
+
durationMs?: number;
|
|
42
|
+
groupKey?: string;
|
|
43
|
+
} | {
|
|
44
|
+
type: "text";
|
|
45
|
+
text: string;
|
|
46
|
+
project?: boolean;
|
|
47
|
+
groupKey?: string;
|
|
48
|
+
} | {
|
|
49
|
+
type: "error";
|
|
50
|
+
message: string;
|
|
51
|
+
};
|
|
52
|
+
export type DispatchOpts = {
|
|
53
|
+
event: ParallEvent;
|
|
54
|
+
earlierEvents?: ParallEvent[];
|
|
55
|
+
bodyForAgent: string;
|
|
56
|
+
sessionKey: string;
|
|
57
|
+
context: DispatchContext;
|
|
58
|
+
};
|
|
59
|
+
export type ForkSessionHandle = {
|
|
60
|
+
sessionKey: string;
|
|
61
|
+
[key: string]: unknown;
|
|
62
|
+
};
|
|
63
|
+
export type ForkOpts = {
|
|
64
|
+
sessionKey: string;
|
|
65
|
+
context: DispatchContext;
|
|
66
|
+
};
|
|
67
|
+
export type CleanupForkOpts = {
|
|
68
|
+
fork: ForkSessionHandle;
|
|
69
|
+
context: DispatchContext;
|
|
70
|
+
};
|
|
71
|
+
export interface DispatchAdapter {
|
|
72
|
+
/** Dispatch a Parall event to the runtime and emit normalized runtime events. */
|
|
73
|
+
dispatch(opts: DispatchOpts): AsyncIterable<RuntimeEvent>;
|
|
74
|
+
/** Fork the current runtime session for concurrent handling. Return null to buffer on main. */
|
|
75
|
+
forkSession?(opts: ForkOpts): Promise<ForkSessionHandle | null> | ForkSessionHandle | null;
|
|
76
|
+
/** Clean up a fork session created by forkSession(). */
|
|
77
|
+
cleanupFork?(opts: CleanupForkOpts): Promise<void> | void;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=dispatch-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatch-adapter.d.ts","sourceRoot":"","sources":["../src/dispatch-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5B,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5B,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,MAAM,EAAE,YAAY,CAAC;IACrB,GAAG,CAAC,EAAE,aAAa,CAAC;CACrB,CAAC;AAEF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B,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,iBAAiB,CAAC;IACxB,OAAO,EAAE,eAAe,CAAC;CAC1B,CAAC;AAEF,MAAM,WAAW,eAAe;IAC9B,iFAAiF;IACjF,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC;IAE1D,+FAA+F;IAC/F,WAAW,CAAC,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,GAAG,iBAAiB,GAAG,IAAI,CAAC;IAE3F,wDAAwD;IACxD,WAAW,CAAC,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAC3D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-format.d.ts","sourceRoot":"","sources":["../src/event-format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE1D,wBAAgB,cAAc,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CA0BzD;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,MAAM,CASnE"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function buildEventBody(event) {
|
|
2
|
+
const lines = [];
|
|
3
|
+
if (event.type === "message") {
|
|
4
|
+
lines.push(`[Event: message.new]`);
|
|
5
|
+
const chatLabel = event.targetName
|
|
6
|
+
? `"${event.targetName}" (${event.targetId})`
|
|
7
|
+
: event.targetId;
|
|
8
|
+
lines.push(`[Chat: ${chatLabel} | type: ${event.targetType ?? "unknown"}]`);
|
|
9
|
+
lines.push(`[From: ${event.senderName} (${event.senderId})]`);
|
|
10
|
+
lines.push(`[Message ID: ${event.messageId}]`);
|
|
11
|
+
if (event.threadRootId)
|
|
12
|
+
lines.push(`[Thread: ${event.threadRootId}]`);
|
|
13
|
+
if (event.noReply)
|
|
14
|
+
lines.push(`[Hint: no_reply]`);
|
|
15
|
+
if (event.mediaFields?.MediaUrl) {
|
|
16
|
+
lines.push(`[Attachment: ${event.mediaFields.MediaType ?? "file"} ${event.mediaFields.MediaUrl}]`);
|
|
17
|
+
}
|
|
18
|
+
lines.push("", event.body);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
lines.push(`[Event: task.assigned]`);
|
|
22
|
+
const taskLabel = event.targetName
|
|
23
|
+
? `${event.targetName} (${event.targetId})`
|
|
24
|
+
: event.targetId;
|
|
25
|
+
lines.push(`[Task: ${taskLabel} | type: ${event.targetType ?? "task"}]`);
|
|
26
|
+
lines.push(`[Assigned by: ${event.senderName} (${event.senderId})]`);
|
|
27
|
+
lines.push("", event.body);
|
|
28
|
+
}
|
|
29
|
+
return lines.join("\n");
|
|
30
|
+
}
|
|
31
|
+
export function buildForkResultPrefix(results) {
|
|
32
|
+
if (!results.length)
|
|
33
|
+
return "";
|
|
34
|
+
const blocks = results.map((result) => {
|
|
35
|
+
const lines = [`[Fork result]`];
|
|
36
|
+
lines.push(`[Handled: ${result.sourceEvent.type} — ${result.sourceEvent.summary}]`);
|
|
37
|
+
if (result.actions.length)
|
|
38
|
+
lines.push(`[Actions: ${result.actions.join("; ")}]`);
|
|
39
|
+
return lines.join("\n");
|
|
40
|
+
});
|
|
41
|
+
return blocks.join("\n\n") + "\n\n---\n\n";
|
|
42
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { ParallClient, ParallWs } from "@parall/sdk";
|
|
2
|
+
import type { AgentConfigUpdateData } from "@parall/sdk";
|
|
3
|
+
import type { DispatchAdapter, GatewayLogger } from "./dispatch-adapter.js";
|
|
4
|
+
export type ParallGatewayOptions = {
|
|
5
|
+
accountId: string;
|
|
6
|
+
client: ParallClient;
|
|
7
|
+
ws: ParallWs;
|
|
8
|
+
connectionLabel?: string;
|
|
9
|
+
config: {
|
|
10
|
+
parall_url: string;
|
|
11
|
+
api_key: string;
|
|
12
|
+
org_id: string;
|
|
13
|
+
};
|
|
14
|
+
agentUserId: string;
|
|
15
|
+
runtimeType: string;
|
|
16
|
+
runtimeKey: string;
|
|
17
|
+
runtimeRef?: Record<string, unknown>;
|
|
18
|
+
dispatchAdapter: DispatchAdapter;
|
|
19
|
+
log?: GatewayLogger;
|
|
20
|
+
coldStartWindowMs?: number;
|
|
21
|
+
stepIdFilePathForSession?: (sessionKey: string) => string | undefined;
|
|
22
|
+
onConfigUpdate?: (data: AgentConfigUpdateData) => Promise<void> | void;
|
|
23
|
+
onSessionReady?: (state: {
|
|
24
|
+
activeSessionId?: string;
|
|
25
|
+
ws: ParallWs;
|
|
26
|
+
runtimeKey: string;
|
|
27
|
+
}) => Promise<void> | void;
|
|
28
|
+
onBeforeDisconnect?: () => Promise<void> | void;
|
|
29
|
+
};
|
|
30
|
+
export declare class ParallAgentGateway {
|
|
31
|
+
private readonly opts;
|
|
32
|
+
private readonly chatInfoMap;
|
|
33
|
+
private readonly activeDispatches;
|
|
34
|
+
private readonly dispatchedTasks;
|
|
35
|
+
private readonly dispatchedMessages;
|
|
36
|
+
private readonly forkStates;
|
|
37
|
+
private readonly dispatchState;
|
|
38
|
+
private sessionId;
|
|
39
|
+
private activeSessionId;
|
|
40
|
+
private heartbeatTimer;
|
|
41
|
+
private hadSuccessfulHello;
|
|
42
|
+
private lastHeartbeatAt;
|
|
43
|
+
private draining;
|
|
44
|
+
private readonly DISPATCHED_MESSAGES_CAP;
|
|
45
|
+
private readonly COLD_START_WINDOW_MS;
|
|
46
|
+
constructor(opts: ParallGatewayOptions);
|
|
47
|
+
run(abortSignal: AbortSignal): Promise<void>;
|
|
48
|
+
private tryClaimMessage;
|
|
49
|
+
private startTyping;
|
|
50
|
+
private stopTyping;
|
|
51
|
+
private buildDispatchContext;
|
|
52
|
+
private createInputStep;
|
|
53
|
+
private createRuntimeStep;
|
|
54
|
+
private writeStepIdFile;
|
|
55
|
+
private clearStepIdFile;
|
|
56
|
+
private createInputStepsForEarlierEvents;
|
|
57
|
+
private runDispatch;
|
|
58
|
+
private runForkDrainLoop;
|
|
59
|
+
private drainMainBuffer;
|
|
60
|
+
private handleInboundEvent;
|
|
61
|
+
private getOrFetchChatInfo;
|
|
62
|
+
private buildMessageDispatchDecision;
|
|
63
|
+
private handleMessage;
|
|
64
|
+
private handleTaskAssignment;
|
|
65
|
+
private catchUpFromDispatch;
|
|
66
|
+
private handleHello;
|
|
67
|
+
private shutdown;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=gateway-base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway-base.d.ts","sourceRoot":"","sources":["../src/gateway-base.ts"],"names":[],"mappings":"AAIA,OAAO,EAAuB,YAAY,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,KAAK,EACV,qBAAqB,EAStB,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,EAEV,eAAe,EAGf,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAmD/B,MAAM,MAAM,oBAAoB,GAAG;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,YAAY,CAAC;IACrB,EAAE,EAAE,QAAQ,CAAC;IACb,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,MAAM,EAAE;QACN,UAAU,EAAE,MAAM,CAAC;QACnB,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,eAAe,EAAE,eAAe,CAAC;IACjC,GAAG,CAAC,EAAE,aAAa,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC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