@rivus/runtime 0.16.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/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/acp.d.ts +179 -0
- package/dist/acp.js +768 -0
- package/dist/chunks/agent-loop.d.ts +481 -0
- package/dist/chunks/agent-loop.js +164 -0
- package/dist/chunks/rivus-runtime-tool.d.ts +386 -0
- package/dist/chunks/tool-input-digest.js +119 -0
- package/dist/index.d.ts +1306 -0
- package/dist/index.js +4164 -0
- package/dist/pi.d.ts +840 -0
- package/dist/pi.js +2143 -0
- package/package.json +69 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import { Effect, Stream } from "effect";
|
|
2
|
+
|
|
3
|
+
//#region src/core/application/agent-execution/contracts/agent-domain-events.d.ts
|
|
4
|
+
type AgentRunId = string;
|
|
5
|
+
type SessionKey = string;
|
|
6
|
+
interface AgentRunAccepted {
|
|
7
|
+
readonly type: "agent_run_accepted";
|
|
8
|
+
readonly runId: AgentRunId;
|
|
9
|
+
readonly sessionKey: SessionKey;
|
|
10
|
+
readonly prompt: string;
|
|
11
|
+
readonly occurredAt: Date;
|
|
12
|
+
}
|
|
13
|
+
interface AgentTurnStarted {
|
|
14
|
+
readonly type: "agent_turn_started";
|
|
15
|
+
readonly runId: AgentRunId;
|
|
16
|
+
readonly sessionKey: SessionKey;
|
|
17
|
+
readonly occurredAt: Date;
|
|
18
|
+
}
|
|
19
|
+
interface AssistantTextDelta {
|
|
20
|
+
readonly type: "assistant_text_delta";
|
|
21
|
+
readonly runId: AgentRunId;
|
|
22
|
+
readonly delta: string;
|
|
23
|
+
readonly occurredAt: Date;
|
|
24
|
+
}
|
|
25
|
+
interface AssistantThinkingDelta {
|
|
26
|
+
readonly type: "assistant_thinking_delta";
|
|
27
|
+
readonly runId: AgentRunId;
|
|
28
|
+
readonly delta: string;
|
|
29
|
+
readonly occurredAt: Date;
|
|
30
|
+
}
|
|
31
|
+
interface AgentModelExecutionStarted {
|
|
32
|
+
readonly type: "agent_model_execution_started";
|
|
33
|
+
readonly api: string;
|
|
34
|
+
readonly model: string;
|
|
35
|
+
readonly modelCallId: string;
|
|
36
|
+
readonly occurredAt: Date;
|
|
37
|
+
readonly provider: string;
|
|
38
|
+
readonly runId: AgentRunId;
|
|
39
|
+
}
|
|
40
|
+
interface AgentModelUsage {
|
|
41
|
+
readonly cacheRead: number;
|
|
42
|
+
readonly cacheWrite: number;
|
|
43
|
+
readonly cost: number;
|
|
44
|
+
readonly input: number;
|
|
45
|
+
readonly output: number;
|
|
46
|
+
readonly reasoning?: number;
|
|
47
|
+
readonly total: number;
|
|
48
|
+
}
|
|
49
|
+
interface AgentModelExecutionEnded {
|
|
50
|
+
readonly type: "agent_model_execution_ended";
|
|
51
|
+
readonly api: string;
|
|
52
|
+
readonly errorMessage?: string;
|
|
53
|
+
readonly model: string;
|
|
54
|
+
readonly modelCallId: string;
|
|
55
|
+
readonly occurredAt: Date;
|
|
56
|
+
readonly provider: string;
|
|
57
|
+
readonly responseModel?: string;
|
|
58
|
+
readonly runId: AgentRunId;
|
|
59
|
+
readonly stopReason: string;
|
|
60
|
+
readonly usage: AgentModelUsage;
|
|
61
|
+
}
|
|
62
|
+
interface AgentSkillExecutionStarted {
|
|
63
|
+
readonly type: "agent_skill_execution_started";
|
|
64
|
+
readonly occurredAt: Date;
|
|
65
|
+
readonly runId: AgentRunId;
|
|
66
|
+
readonly skillCallId: string;
|
|
67
|
+
readonly skillId: string;
|
|
68
|
+
}
|
|
69
|
+
interface AgentSkillExecutionSucceeded {
|
|
70
|
+
readonly type: "agent_skill_execution_ended";
|
|
71
|
+
readonly contentLength: number;
|
|
72
|
+
readonly digest: string;
|
|
73
|
+
readonly isError: false;
|
|
74
|
+
readonly occurredAt: Date;
|
|
75
|
+
readonly runId: AgentRunId;
|
|
76
|
+
readonly skillCallId: string;
|
|
77
|
+
readonly skillId: string;
|
|
78
|
+
readonly title: string;
|
|
79
|
+
readonly version: string;
|
|
80
|
+
}
|
|
81
|
+
interface AgentSkillExecutionFailed {
|
|
82
|
+
readonly type: "agent_skill_execution_ended";
|
|
83
|
+
readonly isError: true;
|
|
84
|
+
readonly occurredAt: Date;
|
|
85
|
+
readonly runId: AgentRunId;
|
|
86
|
+
readonly skillCallId: string;
|
|
87
|
+
readonly skillId: string;
|
|
88
|
+
}
|
|
89
|
+
type AgentSkillExecutionEnded = AgentSkillExecutionSucceeded | AgentSkillExecutionFailed;
|
|
90
|
+
interface AgentToolExecutionStarted {
|
|
91
|
+
readonly type: "agent_tool_execution_started";
|
|
92
|
+
readonly runId: AgentRunId;
|
|
93
|
+
readonly toolCallId: string;
|
|
94
|
+
readonly toolName: string;
|
|
95
|
+
readonly input: unknown;
|
|
96
|
+
readonly occurredAt: Date;
|
|
97
|
+
}
|
|
98
|
+
interface AgentToolExecutionUpdated {
|
|
99
|
+
readonly type: "agent_tool_execution_updated";
|
|
100
|
+
readonly runId: AgentRunId;
|
|
101
|
+
readonly toolCallId: string;
|
|
102
|
+
readonly toolName: string;
|
|
103
|
+
readonly input: unknown;
|
|
104
|
+
readonly partialResult: unknown;
|
|
105
|
+
readonly occurredAt: Date;
|
|
106
|
+
}
|
|
107
|
+
interface AgentToolExecutionEnded {
|
|
108
|
+
readonly type: "agent_tool_execution_ended";
|
|
109
|
+
readonly runId: AgentRunId;
|
|
110
|
+
readonly toolCallId: string;
|
|
111
|
+
readonly toolName: string;
|
|
112
|
+
readonly result: unknown;
|
|
113
|
+
readonly isError: boolean;
|
|
114
|
+
readonly occurredAt: Date;
|
|
115
|
+
}
|
|
116
|
+
interface AgentTurnCompleted {
|
|
117
|
+
readonly type: "agent_turn_completed";
|
|
118
|
+
readonly runId: AgentRunId;
|
|
119
|
+
readonly finalText: string;
|
|
120
|
+
readonly occurredAt: Date;
|
|
121
|
+
}
|
|
122
|
+
interface AgentRunCompleted {
|
|
123
|
+
readonly type: "agent_run_completed";
|
|
124
|
+
readonly runId: AgentRunId;
|
|
125
|
+
readonly finalText: string;
|
|
126
|
+
readonly occurredAt: Date;
|
|
127
|
+
}
|
|
128
|
+
interface AgentRunFailed {
|
|
129
|
+
readonly type: "agent_run_failed";
|
|
130
|
+
readonly runId: AgentRunId;
|
|
131
|
+
readonly errorMessage: string;
|
|
132
|
+
readonly occurredAt: Date;
|
|
133
|
+
}
|
|
134
|
+
interface AgentRunCancelled {
|
|
135
|
+
readonly type: "agent_run_cancelled";
|
|
136
|
+
readonly runId: AgentRunId;
|
|
137
|
+
readonly reason?: string;
|
|
138
|
+
readonly occurredAt: Date;
|
|
139
|
+
}
|
|
140
|
+
type AgentDomainEvent = AgentRunAccepted | AgentTurnStarted | AssistantTextDelta | AssistantThinkingDelta | AgentModelExecutionStarted | AgentModelExecutionEnded | AgentSkillExecutionStarted | AgentSkillExecutionEnded | AgentToolExecutionStarted | AgentToolExecutionUpdated | AgentToolExecutionEnded | AgentTurnCompleted | AgentRunCompleted | AgentRunFailed | AgentRunCancelled;
|
|
141
|
+
type AgentToolExecutionEvent = AgentToolExecutionStarted | AgentToolExecutionUpdated | AgentToolExecutionEnded;
|
|
142
|
+
type AgentModelExecutionEvent = AgentModelExecutionStarted | AgentModelExecutionEnded;
|
|
143
|
+
type TerminalAgentDomainEvent = AgentRunCompleted | AgentRunFailed | AgentRunCancelled;
|
|
144
|
+
declare function isAssistantTextDeltaEvent(event: AgentDomainEvent): event is AssistantTextDelta;
|
|
145
|
+
declare function isAssistantThinkingDeltaEvent(event: AgentDomainEvent): event is AssistantThinkingDelta;
|
|
146
|
+
declare function isAgentToolExecutionEvent(event: AgentDomainEvent): event is AgentToolExecutionEvent;
|
|
147
|
+
declare function isTerminalAgentDomainEvent(event: AgentDomainEvent): event is TerminalAgentDomainEvent;
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/core/application/agent-execution/contracts/agent-run-state.d.ts
|
|
150
|
+
type AgentRunPhase = "idle" | "accepted" | "running" | "completed" | "failed" | "cancelled";
|
|
151
|
+
interface AgentRunState {
|
|
152
|
+
readonly phase: AgentRunPhase;
|
|
153
|
+
readonly runId?: AgentRunId;
|
|
154
|
+
readonly sessionKey?: SessionKey;
|
|
155
|
+
readonly prompt?: string;
|
|
156
|
+
readonly finalText: string;
|
|
157
|
+
readonly errorMessage?: string;
|
|
158
|
+
readonly cancellationReason?: string;
|
|
159
|
+
readonly thinkingText?: string;
|
|
160
|
+
readonly skillExecutions: Readonly<Record<string, AgentSkillExecutionState>>;
|
|
161
|
+
readonly toolExecutions: Readonly<Record<string, AgentToolExecutionState>>;
|
|
162
|
+
}
|
|
163
|
+
interface AgentSkillExecutionState {
|
|
164
|
+
readonly contentLength?: number;
|
|
165
|
+
readonly digest?: string;
|
|
166
|
+
readonly isError?: boolean;
|
|
167
|
+
readonly skillCallId: string;
|
|
168
|
+
readonly skillId: string;
|
|
169
|
+
readonly status: "running" | "completed";
|
|
170
|
+
readonly title?: string;
|
|
171
|
+
readonly version?: string;
|
|
172
|
+
}
|
|
173
|
+
interface AgentToolExecutionState {
|
|
174
|
+
readonly toolCallId: string;
|
|
175
|
+
readonly toolName: string;
|
|
176
|
+
readonly input: unknown;
|
|
177
|
+
readonly partialResult?: unknown;
|
|
178
|
+
readonly result?: unknown;
|
|
179
|
+
readonly isError?: boolean;
|
|
180
|
+
readonly status: "running" | "completed";
|
|
181
|
+
}
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/core/application/agent-execution/history/agent-history.d.ts
|
|
184
|
+
interface AgentRunSummary {
|
|
185
|
+
readonly acceptedAt?: Date;
|
|
186
|
+
readonly cancellationReason?: string;
|
|
187
|
+
readonly cancelledAt?: Date;
|
|
188
|
+
readonly completedAt?: Date;
|
|
189
|
+
readonly errorMessage?: string;
|
|
190
|
+
readonly eventCount: number;
|
|
191
|
+
readonly failedToolExecutionCount: number;
|
|
192
|
+
readonly failedSkillExecutionCount: number;
|
|
193
|
+
readonly failedAt?: Date;
|
|
194
|
+
readonly finalText: string;
|
|
195
|
+
readonly phase: AgentRunPhase;
|
|
196
|
+
readonly prompt?: string;
|
|
197
|
+
readonly runId: AgentRunId;
|
|
198
|
+
readonly sessionKey?: SessionKey;
|
|
199
|
+
readonly startedAt?: Date;
|
|
200
|
+
readonly state: AgentRunState;
|
|
201
|
+
readonly thinkingText: string;
|
|
202
|
+
readonly skillExecutionCount: number;
|
|
203
|
+
readonly toolExecutionCount: number;
|
|
204
|
+
readonly updatedAt: Date;
|
|
205
|
+
}
|
|
206
|
+
interface AgentSessionSummary {
|
|
207
|
+
readonly failedToolExecutionCount: number;
|
|
208
|
+
readonly failedSkillExecutionCount: number;
|
|
209
|
+
readonly finalText: string;
|
|
210
|
+
readonly latestPhase: AgentRunPhase;
|
|
211
|
+
readonly latestRunId: AgentRunId;
|
|
212
|
+
readonly runCount: number;
|
|
213
|
+
readonly runIds: ReadonlyArray<AgentRunId>;
|
|
214
|
+
readonly sessionKey: SessionKey;
|
|
215
|
+
readonly thinkingText: string;
|
|
216
|
+
readonly skillExecutionCount: number;
|
|
217
|
+
readonly toolExecutionCount: number;
|
|
218
|
+
readonly updatedAt: Date;
|
|
219
|
+
}
|
|
220
|
+
interface AgentHistory {
|
|
221
|
+
readonly runs: ReadonlyArray<AgentRunSummary>;
|
|
222
|
+
readonly sessions: ReadonlyArray<AgentSessionSummary>;
|
|
223
|
+
readonly totalEvents: number;
|
|
224
|
+
}
|
|
225
|
+
declare function replayAgentHistory(events: ReadonlyArray<AgentDomainEvent>): AgentHistory;
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/core/application/agent-execution/transcript/agent-transcript.d.ts
|
|
228
|
+
interface AgentTranscript {
|
|
229
|
+
readonly latestTurn?: AgentTranscriptTurn;
|
|
230
|
+
readonly sessionKey: SessionKey;
|
|
231
|
+
readonly turnCount: number;
|
|
232
|
+
readonly turns: ReadonlyArray<AgentTranscriptTurn>;
|
|
233
|
+
}
|
|
234
|
+
interface AgentTranscriptTurn {
|
|
235
|
+
readonly acceptedAt?: Date;
|
|
236
|
+
readonly assistantText: string;
|
|
237
|
+
readonly cancellationReason?: string;
|
|
238
|
+
readonly cancelledAt?: Date;
|
|
239
|
+
readonly completedAt?: Date;
|
|
240
|
+
readonly errorMessage?: string;
|
|
241
|
+
readonly failedAt?: Date;
|
|
242
|
+
readonly failedToolExecutionCount: number;
|
|
243
|
+
readonly phase: AgentRunPhase;
|
|
244
|
+
readonly runId: AgentRunId;
|
|
245
|
+
readonly sessionKey: SessionKey;
|
|
246
|
+
readonly thinkingText: string;
|
|
247
|
+
readonly toolExecutionCount: number;
|
|
248
|
+
readonly updatedAt: Date;
|
|
249
|
+
readonly userText: string;
|
|
250
|
+
}
|
|
251
|
+
type AgentTranscriptMessageRole = "assistant" | "user";
|
|
252
|
+
interface AgentTranscriptMessage {
|
|
253
|
+
readonly content: string;
|
|
254
|
+
readonly role: AgentTranscriptMessageRole;
|
|
255
|
+
}
|
|
256
|
+
interface AgentConversationMessagesOptions {
|
|
257
|
+
readonly text: string;
|
|
258
|
+
readonly transcript?: AgentTranscript;
|
|
259
|
+
}
|
|
260
|
+
declare function replayAgentTranscript(events: ReadonlyArray<AgentDomainEvent>, sessionKey: SessionKey): AgentTranscript;
|
|
261
|
+
declare function createAgentTranscriptTurn(summary: AgentRunSummary & {
|
|
262
|
+
readonly sessionKey: SessionKey;
|
|
263
|
+
}): AgentTranscriptTurn;
|
|
264
|
+
declare function createAgentTranscriptMessages(transcript: AgentTranscript): ReadonlyArray<AgentTranscriptMessage>;
|
|
265
|
+
declare function createAgentConversationMessages(options: AgentConversationMessagesOptions): ReadonlyArray<AgentTranscriptMessage>;
|
|
266
|
+
//#endregion
|
|
267
|
+
//#region src/core/application/agent-execution/invocation/agent-invocation.d.ts
|
|
268
|
+
interface AgentMemoryInvocationIdentity {
|
|
269
|
+
readonly audience: "group" | "private";
|
|
270
|
+
readonly conversationId?: string;
|
|
271
|
+
readonly projectId?: string;
|
|
272
|
+
readonly subjectId: string;
|
|
273
|
+
readonly tenantId: string;
|
|
274
|
+
}
|
|
275
|
+
interface FeishuAgentInvocationOrigin {
|
|
276
|
+
readonly allowedActorOpenIds: ReadonlyArray<string>;
|
|
277
|
+
readonly endpointId: string;
|
|
278
|
+
readonly kind: "feishu";
|
|
279
|
+
readonly memory?: AgentMemoryInvocationIdentity;
|
|
280
|
+
readonly sourceMessageId: string;
|
|
281
|
+
readonly tenantKey: string;
|
|
282
|
+
}
|
|
283
|
+
interface LocalCliAgentInvocationOrigin {
|
|
284
|
+
readonly allowedActorOpenIds: readonly [];
|
|
285
|
+
readonly endpointId: string;
|
|
286
|
+
readonly kind: "local-cli";
|
|
287
|
+
readonly memory?: AgentMemoryInvocationIdentity;
|
|
288
|
+
readonly sourceMessageId: string;
|
|
289
|
+
readonly tenantKey: "local";
|
|
290
|
+
}
|
|
291
|
+
interface AutomationAgentInvocationOrigin {
|
|
292
|
+
readonly allowedActorOpenIds: readonly [];
|
|
293
|
+
readonly automationId: string;
|
|
294
|
+
readonly endpointId: string;
|
|
295
|
+
readonly kind: "automation";
|
|
296
|
+
readonly memory?: AgentMemoryInvocationIdentity;
|
|
297
|
+
readonly sourceMessageId: string;
|
|
298
|
+
readonly tenantKey: "automation";
|
|
299
|
+
readonly tickId: string;
|
|
300
|
+
}
|
|
301
|
+
interface BackgroundSessionAgentInvocationOrigin {
|
|
302
|
+
readonly allowedActorOpenIds: ReadonlyArray<string>;
|
|
303
|
+
readonly conversationId?: string;
|
|
304
|
+
readonly endpointId: string;
|
|
305
|
+
readonly kind: "background-session";
|
|
306
|
+
readonly memory?: AgentMemoryInvocationIdentity;
|
|
307
|
+
readonly sessionId: string;
|
|
308
|
+
readonly sourceMessageId: string;
|
|
309
|
+
readonly tenantKey: string;
|
|
310
|
+
}
|
|
311
|
+
type AgentInvocationOrigin = FeishuAgentInvocationOrigin | LocalCliAgentInvocationOrigin | AutomationAgentInvocationOrigin | BackgroundSessionAgentInvocationOrigin;
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region src/core/application/agent-execution/loop/agent-loop.d.ts
|
|
314
|
+
interface AgentLoopInput$1 {
|
|
315
|
+
readonly abortSignal: AbortSignal;
|
|
316
|
+
readonly invocation?: AgentInvocationOrigin;
|
|
317
|
+
readonly messages?: ReadonlyArray<AgentTranscriptMessage>;
|
|
318
|
+
readonly runId: AgentRunId;
|
|
319
|
+
readonly sessionKey: SessionKey;
|
|
320
|
+
/** Optional mailbox for user steering while the run is active. */
|
|
321
|
+
readonly steering?: AgentSteeringChannel$1;
|
|
322
|
+
readonly text: string;
|
|
323
|
+
readonly previousSessionState?: AgentRunState;
|
|
324
|
+
readonly previousSessionTranscript?: AgentTranscript;
|
|
325
|
+
}
|
|
326
|
+
interface AgentSteeringChannel$1 {
|
|
327
|
+
next(): Effect.Effect<string>;
|
|
328
|
+
/** Seal admission at provider completion and return accepted messages still waiting. */
|
|
329
|
+
close?(): Effect.Effect<readonly string[]>;
|
|
330
|
+
}
|
|
331
|
+
type AgentLoopEvent = AgentLoopTurnStart | AgentLoopTextDelta | AgentLoopThinkingDelta | AgentLoopModelExecutionStart | AgentLoopModelExecutionEnd | AgentLoopSkillExecutionStart | AgentLoopSkillExecutionEnd | AgentLoopToolExecutionStart | AgentLoopToolExecutionUpdate | AgentLoopToolExecutionEnd;
|
|
332
|
+
type AgentLoopEventLike = AgentLoopEvent | string;
|
|
333
|
+
interface AgentLoopTextDelta {
|
|
334
|
+
readonly type: "assistant_text_delta";
|
|
335
|
+
readonly delta: string;
|
|
336
|
+
}
|
|
337
|
+
interface AgentLoopTurnStart {
|
|
338
|
+
readonly type: "turn_start";
|
|
339
|
+
}
|
|
340
|
+
interface AgentLoopThinkingDelta {
|
|
341
|
+
readonly type: "assistant_thinking_delta";
|
|
342
|
+
readonly delta: string;
|
|
343
|
+
}
|
|
344
|
+
interface AgentLoopModelExecutionStart {
|
|
345
|
+
readonly type: "model_execution_start";
|
|
346
|
+
readonly api: string;
|
|
347
|
+
readonly model: string;
|
|
348
|
+
readonly modelCallId: string;
|
|
349
|
+
readonly provider: string;
|
|
350
|
+
}
|
|
351
|
+
interface AgentLoopModelExecutionEnd {
|
|
352
|
+
readonly type: "model_execution_end";
|
|
353
|
+
readonly api: string;
|
|
354
|
+
readonly errorMessage?: string;
|
|
355
|
+
readonly model: string;
|
|
356
|
+
readonly modelCallId: string;
|
|
357
|
+
readonly provider: string;
|
|
358
|
+
readonly responseModel?: string;
|
|
359
|
+
readonly stopReason: string;
|
|
360
|
+
readonly usage: AgentModelUsage;
|
|
361
|
+
}
|
|
362
|
+
interface AgentLoopSkillExecutionStart {
|
|
363
|
+
readonly type: "skill_execution_start";
|
|
364
|
+
readonly skillCallId: string;
|
|
365
|
+
readonly skillId: string;
|
|
366
|
+
}
|
|
367
|
+
interface AgentLoopSkillExecutionSucceeded {
|
|
368
|
+
readonly type: "skill_execution_end";
|
|
369
|
+
readonly contentLength: number;
|
|
370
|
+
readonly digest: string;
|
|
371
|
+
readonly isError: false;
|
|
372
|
+
readonly skillCallId: string;
|
|
373
|
+
readonly skillId: string;
|
|
374
|
+
readonly title: string;
|
|
375
|
+
readonly version: string;
|
|
376
|
+
}
|
|
377
|
+
interface AgentLoopSkillExecutionFailed {
|
|
378
|
+
readonly type: "skill_execution_end";
|
|
379
|
+
readonly isError: true;
|
|
380
|
+
readonly skillCallId: string;
|
|
381
|
+
readonly skillId: string;
|
|
382
|
+
}
|
|
383
|
+
type AgentLoopSkillExecutionEnd = AgentLoopSkillExecutionSucceeded | AgentLoopSkillExecutionFailed;
|
|
384
|
+
interface AgentLoopToolExecutionStart {
|
|
385
|
+
readonly type: "tool_execution_start";
|
|
386
|
+
readonly toolCallId: string;
|
|
387
|
+
readonly toolName: string;
|
|
388
|
+
readonly input: unknown;
|
|
389
|
+
}
|
|
390
|
+
interface AgentLoopToolExecutionUpdate {
|
|
391
|
+
readonly type: "tool_execution_update";
|
|
392
|
+
readonly toolCallId: string;
|
|
393
|
+
readonly toolName: string;
|
|
394
|
+
readonly input: unknown;
|
|
395
|
+
readonly partialResult: unknown;
|
|
396
|
+
}
|
|
397
|
+
interface AgentLoopToolExecutionEnd {
|
|
398
|
+
readonly type: "tool_execution_end";
|
|
399
|
+
readonly toolCallId: string;
|
|
400
|
+
readonly toolName: string;
|
|
401
|
+
readonly result: unknown;
|
|
402
|
+
readonly isError: boolean;
|
|
403
|
+
}
|
|
404
|
+
interface AgentLoop$1 {
|
|
405
|
+
run(input: AgentLoopInput$1): Stream.Stream<AgentLoopEvent, unknown>;
|
|
406
|
+
/** Provider adapters opt in only when they can consume steering safely. */
|
|
407
|
+
readonly supportsSteering?: boolean;
|
|
408
|
+
}
|
|
409
|
+
interface TextAgentLoopOptions$1 {
|
|
410
|
+
readonly generate: (input: AgentLoopInput$1) => Effect.Effect<string, unknown>;
|
|
411
|
+
}
|
|
412
|
+
type AgentLoopToolExecutionStartOptions = Omit<AgentLoopToolExecutionStart, "type">;
|
|
413
|
+
type AgentLoopToolExecutionUpdateOptions = Omit<AgentLoopToolExecutionUpdate, "type">;
|
|
414
|
+
type AgentLoopToolExecutionEndOptions = Omit<AgentLoopToolExecutionEnd, "type">;
|
|
415
|
+
type AgentLoopModelExecutionStartOptions = Omit<AgentLoopModelExecutionStart, "type">;
|
|
416
|
+
type AgentLoopModelExecutionEndOptions = Omit<AgentLoopModelExecutionEnd, "type">;
|
|
417
|
+
type AgentLoopSkillExecutionStartOptions = Omit<AgentLoopSkillExecutionStart, "type">;
|
|
418
|
+
type AgentLoopSkillExecutionEndOptions = Omit<AgentLoopSkillExecutionSucceeded, "type"> | Omit<AgentLoopSkillExecutionFailed, "type">;
|
|
419
|
+
declare function createAgentLoopTextDelta(delta: string): AgentLoopTextDelta;
|
|
420
|
+
declare function createAgentLoopTurnStart(): AgentLoopTurnStart;
|
|
421
|
+
declare function normalizeAgentLoopEvent(event: AgentLoopEventLike): AgentLoopEvent;
|
|
422
|
+
declare function createAgentLoopThinkingDelta(delta: string): AgentLoopThinkingDelta;
|
|
423
|
+
declare function createAgentLoopModelExecutionStart(options: AgentLoopModelExecutionStartOptions): AgentLoopModelExecutionStart;
|
|
424
|
+
declare function createAgentLoopModelExecutionEnd(options: AgentLoopModelExecutionEndOptions): AgentLoopModelExecutionEnd;
|
|
425
|
+
declare function createAgentLoopSkillExecutionStart(options: AgentLoopSkillExecutionStartOptions): AgentLoopSkillExecutionStart;
|
|
426
|
+
declare function createAgentLoopSkillExecutionEnd(options: AgentLoopSkillExecutionEndOptions): AgentLoopSkillExecutionEnd;
|
|
427
|
+
declare function createAgentLoopToolExecutionStart(options: AgentLoopToolExecutionStartOptions): AgentLoopToolExecutionStart;
|
|
428
|
+
declare function createAgentLoopToolExecutionUpdate(options: AgentLoopToolExecutionUpdateOptions): AgentLoopToolExecutionUpdate;
|
|
429
|
+
declare function createAgentLoopToolExecutionEnd(options: AgentLoopToolExecutionEndOptions): AgentLoopToolExecutionEnd;
|
|
430
|
+
interface EventAgentLoopOptions$1 {
|
|
431
|
+
readonly events: ReadonlyArray<AgentLoopEventLike> | ((input: AgentLoopInput$1) => ReadonlyArray<AgentLoopEventLike> | Promise<ReadonlyArray<AgentLoopEventLike>>);
|
|
432
|
+
}
|
|
433
|
+
declare function createEventAgentLoop$1(options: EventAgentLoopOptions$1): AgentLoop$1;
|
|
434
|
+
declare function createTextAgentLoop$1(options: TextAgentLoopOptions$1): AgentLoop$1;
|
|
435
|
+
//#endregion
|
|
436
|
+
//#region src/adapters/compatibility/agent-execution/loop/agent-loop.d.ts
|
|
437
|
+
interface AgentLoopInput {
|
|
438
|
+
readonly runId: AgentRunId;
|
|
439
|
+
readonly sessionKey: SessionKey;
|
|
440
|
+
readonly text: string;
|
|
441
|
+
readonly abortSignal: AbortSignal;
|
|
442
|
+
readonly previousSessionState?: AgentRunState;
|
|
443
|
+
readonly previousSessionTranscript?: AgentTranscript;
|
|
444
|
+
readonly messages?: ReadonlyArray<AgentTranscriptMessage>;
|
|
445
|
+
readonly invocation?: AgentInvocationOrigin;
|
|
446
|
+
/** Optional mailbox for user steering while the run is active. */
|
|
447
|
+
readonly steering?: AgentSteeringChannel;
|
|
448
|
+
}
|
|
449
|
+
interface AgentSteeringChannel {
|
|
450
|
+
next(signal?: AbortSignal): Promise<string>;
|
|
451
|
+
close?(): Promise<readonly string[]>;
|
|
452
|
+
}
|
|
453
|
+
interface AgentLoop {
|
|
454
|
+
run(input: AgentLoopInput): Stream.Stream<AgentLoopEvent, unknown>;
|
|
455
|
+
/** Provider adapters opt in only when they can consume steering safely. */
|
|
456
|
+
readonly supportsSteering?: boolean;
|
|
457
|
+
}
|
|
458
|
+
type AgentLoopCallbackOutput = Iterable<AgentLoopEventLike> | AsyncIterable<AgentLoopEventLike> | Stream.Stream<AgentLoopEventLike, unknown>;
|
|
459
|
+
type AgentLoopCallbackResult = AgentLoopCallbackOutput | PromiseLike<AgentLoopCallbackOutput> | Effect.Effect<AgentLoopCallbackOutput, unknown>;
|
|
460
|
+
type AgentLoopCallback = (input: AgentLoopInput) => AgentLoopCallbackResult;
|
|
461
|
+
interface AsyncIterableAgentLoopOptions {
|
|
462
|
+
readonly run: (input: AgentLoopInput) => AsyncIterable<AgentLoopEventLike>;
|
|
463
|
+
}
|
|
464
|
+
interface EventAgentLoopOptions {
|
|
465
|
+
readonly events: ReadonlyArray<AgentLoopEventLike> | ((input: AgentLoopInput) => ReadonlyArray<AgentLoopEventLike> | Promise<ReadonlyArray<AgentLoopEventLike>>);
|
|
466
|
+
}
|
|
467
|
+
type TextAgentLoopCallback = (input: AgentLoopInput) => string | Promise<string>;
|
|
468
|
+
interface TextAgentLoopOptions {
|
|
469
|
+
readonly generate: (input: AgentLoopInput) => Effect.Effect<string, unknown>;
|
|
470
|
+
}
|
|
471
|
+
declare function createEventAgentLoop(options: EventAgentLoopOptions): AgentLoop;
|
|
472
|
+
declare function createAsyncIterableAgentLoop(options: AsyncIterableAgentLoopOptions): AgentLoop;
|
|
473
|
+
declare function createAgentLoopFromCallback(run: AgentLoopCallback): AgentLoop;
|
|
474
|
+
declare function createTextAgentLoop(options: TextAgentLoopOptions): AgentLoop;
|
|
475
|
+
declare function createTextAgentLoopFromCallback(generate: TextAgentLoopCallback): AgentLoop;
|
|
476
|
+
declare function toEffectAgentLoop(loop: AgentLoop): AgentLoop$1;
|
|
477
|
+
declare function fromEffectAgentLoop(loop: AgentLoop$1): AgentLoop;
|
|
478
|
+
declare function toEffectAgentLoopInput(input: AgentLoopInput): AgentLoopInput$1;
|
|
479
|
+
declare function toCompatibilityAgentLoopInput(input: AgentLoopInput$1): AgentLoopInput;
|
|
480
|
+
//#endregion
|
|
481
|
+
export { createAgentLoopTurnStart as $, AgentLoopSkillExecutionStart as A, AgentModelUsage as At, AgentLoopTurnStart as B, AgentToolExecutionUpdated as Bt, AgentLoopInput$1 as C, AgentRunState as Ct, AgentLoopModelExecutionStartOptions as D, AgentModelExecutionEnded as Dt, AgentLoopModelExecutionStart as E, AgentDomainEvent as Et, AgentLoopToolExecutionEndOptions as F, AgentSkillExecutionEnded as Ft, createAgentLoopModelExecutionStart as G, SessionKey as Gt, EventAgentLoopOptions$1 as H, AgentTurnStarted as Ht, AgentLoopToolExecutionStart as I, AgentSkillExecutionStarted as It, createAgentLoopTextDelta as J, isAssistantTextDeltaEvent as Jt, createAgentLoopSkillExecutionEnd as K, TerminalAgentDomainEvent as Kt, AgentLoopToolExecutionStartOptions as L, AgentToolExecutionEnded as Lt, AgentLoopTextDelta as M, AgentRunCompleted as Mt, AgentLoopThinkingDelta as N, AgentRunFailed as Nt, AgentLoopSkillExecutionEnd as O, AgentModelExecutionEvent as Ot, AgentLoopToolExecutionEnd as P, AgentRunId as Pt, createAgentLoopToolExecutionUpdate as Q, AgentLoopToolExecutionUpdate as R, AgentToolExecutionEvent as Rt, AgentLoopEventLike as S, AgentRunPhase as St, AgentLoopModelExecutionEndOptions as T, AgentToolExecutionState as Tt, TextAgentLoopOptions$1 as U, AssistantTextDelta as Ut, AgentSteeringChannel$1 as V, AgentTurnCompleted as Vt, createAgentLoopModelExecutionEnd as W, AssistantThinkingDelta as Wt, createAgentLoopToolExecutionEnd as X, isTerminalAgentDomainEvent as Xt, createAgentLoopThinkingDelta as Y, isAssistantThinkingDeltaEvent as Yt, createAgentLoopToolExecutionStart as Z, toCompatibilityAgentLoopInput as _, replayAgentTranscript as _t, AgentLoopInput as a, AutomationAgentInvocationOrigin as at, AgentLoop$1 as b, AgentSessionSummary as bt, EventAgentLoopOptions as c, LocalCliAgentInvocationOrigin as ct, createAgentLoopFromCallback as d, AgentTranscriptMessage as dt, createEventAgentLoop$1 as et, createAsyncIterableAgentLoop as f, AgentTranscriptMessageRole as ft, fromEffectAgentLoop as g, createAgentTranscriptTurn as gt, createTextAgentLoopFromCallback as h, createAgentTranscriptMessages as ht, AgentLoopCallbackResult as i, AgentMemoryInvocationIdentity as it, AgentLoopSkillExecutionStartOptions as j, AgentRunAccepted as jt, AgentLoopSkillExecutionEndOptions as k, AgentModelExecutionStarted as kt, TextAgentLoopCallback as l, AgentConversationMessagesOptions as lt, createTextAgentLoop as m, createAgentConversationMessages as mt, AgentLoopCallback as n, normalizeAgentLoopEvent as nt, AgentSteeringChannel as o, BackgroundSessionAgentInvocationOrigin as ot, createEventAgentLoop as p, AgentTranscriptTurn as pt, createAgentLoopSkillExecutionStart as q, isAgentToolExecutionEvent as qt, AgentLoopCallbackOutput as r, AgentInvocationOrigin as rt, AsyncIterableAgentLoopOptions as s, FeishuAgentInvocationOrigin as st, AgentLoop as t, createTextAgentLoop$1 as tt, TextAgentLoopOptions as u, AgentTranscript as ut, toEffectAgentLoop as v, AgentHistory as vt, AgentLoopModelExecutionEnd as w, AgentSkillExecutionState as wt, AgentLoopEvent as x, replayAgentHistory as xt, toEffectAgentLoopInput as y, AgentRunSummary as yt, AgentLoopToolExecutionUpdateOptions as z, AgentToolExecutionStarted as zt };
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Effect, Stream } from "effect";
|
|
2
|
+
//#region src/core/application/agent-execution/loop/agent-loop.ts
|
|
3
|
+
function createAgentLoopTextDelta(delta) {
|
|
4
|
+
return {
|
|
5
|
+
delta,
|
|
6
|
+
type: "assistant_text_delta"
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function createAgentLoopTurnStart() {
|
|
10
|
+
return { type: "turn_start" };
|
|
11
|
+
}
|
|
12
|
+
function normalizeAgentLoopEvent(event) {
|
|
13
|
+
return typeof event === "string" ? createAgentLoopTextDelta(event) : event;
|
|
14
|
+
}
|
|
15
|
+
function createAgentLoopThinkingDelta(delta) {
|
|
16
|
+
return {
|
|
17
|
+
delta,
|
|
18
|
+
type: "assistant_thinking_delta"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function createAgentLoopModelExecutionStart(options) {
|
|
22
|
+
return {
|
|
23
|
+
...options,
|
|
24
|
+
type: "model_execution_start"
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function createAgentLoopModelExecutionEnd(options) {
|
|
28
|
+
return {
|
|
29
|
+
...options,
|
|
30
|
+
type: "model_execution_end"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function createAgentLoopSkillExecutionStart(options) {
|
|
34
|
+
return {
|
|
35
|
+
...options,
|
|
36
|
+
type: "skill_execution_start"
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function createAgentLoopSkillExecutionEnd(options) {
|
|
40
|
+
return {
|
|
41
|
+
...options,
|
|
42
|
+
type: "skill_execution_end"
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function createAgentLoopToolExecutionStart(options) {
|
|
46
|
+
return {
|
|
47
|
+
input: options.input,
|
|
48
|
+
toolCallId: options.toolCallId,
|
|
49
|
+
toolName: options.toolName,
|
|
50
|
+
type: "tool_execution_start"
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function createAgentLoopToolExecutionUpdate(options) {
|
|
54
|
+
return {
|
|
55
|
+
input: options.input,
|
|
56
|
+
partialResult: options.partialResult,
|
|
57
|
+
toolCallId: options.toolCallId,
|
|
58
|
+
toolName: options.toolName,
|
|
59
|
+
type: "tool_execution_update"
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function createAgentLoopToolExecutionEnd(options) {
|
|
63
|
+
return {
|
|
64
|
+
isError: options.isError,
|
|
65
|
+
result: options.result,
|
|
66
|
+
toolCallId: options.toolCallId,
|
|
67
|
+
toolName: options.toolName,
|
|
68
|
+
type: "tool_execution_end"
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function isPromiseLike$1(value) {
|
|
72
|
+
return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
73
|
+
}
|
|
74
|
+
function createEventAgentLoop$1(options) {
|
|
75
|
+
return { run: (input) => {
|
|
76
|
+
const events = typeof options.events === "function" ? options.events(input) : options.events;
|
|
77
|
+
if (isPromiseLike$1(events)) return Stream.fromEffect(Effect.tryPromise({
|
|
78
|
+
try: () => events,
|
|
79
|
+
catch: (cause) => cause
|
|
80
|
+
})).pipe(Stream.flatMap((resolvedEvents) => Stream.fromIterable(resolvedEvents)), Stream.map(normalizeAgentLoopEvent));
|
|
81
|
+
return Stream.fromIterable(events).pipe(Stream.map(normalizeAgentLoopEvent));
|
|
82
|
+
} };
|
|
83
|
+
}
|
|
84
|
+
function createTextAgentLoop$1(options) {
|
|
85
|
+
return { run: (input) => Stream.fromEffect(options.generate(input)).pipe(Stream.map((delta) => createAgentLoopTextDelta(delta))) };
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/adapters/compatibility/agent-execution/loop/agent-loop.ts
|
|
89
|
+
function createEventAgentLoop(options) {
|
|
90
|
+
return fromEffectAgentLoop(createEventAgentLoop$1({ events: (input) => typeof options.events === "function" ? options.events(toCompatibilityAgentLoopInput(input)) : options.events }));
|
|
91
|
+
}
|
|
92
|
+
function createAsyncIterableAgentLoop(options) {
|
|
93
|
+
return { run: (input) => Stream.fromAsyncIterable(options.run(input), (error) => error).pipe(Stream.map(normalizeAgentLoopEvent)) };
|
|
94
|
+
}
|
|
95
|
+
function createAgentLoopFromCallback(run) {
|
|
96
|
+
return { run: (input) => Stream.fromEffect(Effect.try({
|
|
97
|
+
try: () => run(input),
|
|
98
|
+
catch: (cause) => cause
|
|
99
|
+
})).pipe(Stream.flatMap(streamFromAgentLoopCallbackResult)) };
|
|
100
|
+
}
|
|
101
|
+
function createTextAgentLoop(options) {
|
|
102
|
+
return fromEffectAgentLoop(createTextAgentLoop$1({ generate: (input) => options.generate(toCompatibilityAgentLoopInput(input)) }));
|
|
103
|
+
}
|
|
104
|
+
function createTextAgentLoopFromCallback(generate) {
|
|
105
|
+
return createTextAgentLoop({ generate: (input) => Effect.tryPromise({
|
|
106
|
+
try: async () => generate(input),
|
|
107
|
+
catch: (cause) => cause
|
|
108
|
+
}) });
|
|
109
|
+
}
|
|
110
|
+
function toEffectAgentLoop(loop) {
|
|
111
|
+
return {
|
|
112
|
+
run: (input) => loop.run(toCompatibilityAgentLoopInput(input)),
|
|
113
|
+
...loop.supportsSteering ? { supportsSteering: true } : {}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function fromEffectAgentLoop(loop) {
|
|
117
|
+
return {
|
|
118
|
+
run: (input) => loop.run(toEffectAgentLoopInput(input)),
|
|
119
|
+
...loop.supportsSteering ? { supportsSteering: true } : {}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function toEffectAgentLoopInput(input) {
|
|
123
|
+
const { steering, ...rest } = input;
|
|
124
|
+
return {
|
|
125
|
+
...rest,
|
|
126
|
+
...steering ? { steering: {
|
|
127
|
+
...steering.close ? { close: () => Effect.promise(() => steering.close()) } : {},
|
|
128
|
+
next: () => Effect.promise((signal) => steering.next(signal))
|
|
129
|
+
} } : {}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function toCompatibilityAgentLoopInput(input) {
|
|
133
|
+
const { steering, ...rest } = input;
|
|
134
|
+
return {
|
|
135
|
+
...rest,
|
|
136
|
+
...steering ? { steering: {
|
|
137
|
+
...steering.close ? { close: () => Effect.runPromise(steering.close()) } : {},
|
|
138
|
+
next: (signal) => Effect.runPromise(steering.next(), { signal })
|
|
139
|
+
} } : {}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function isPromiseLike(value) {
|
|
143
|
+
return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
144
|
+
}
|
|
145
|
+
function streamFromAgentLoopCallbackResult(result) {
|
|
146
|
+
if (Effect.isEffect(result)) return Stream.fromEffect(result).pipe(Stream.flatMap(streamFromAgentLoopCallbackOutput));
|
|
147
|
+
if (isPromiseLike(result)) return Stream.fromEffect(Effect.tryPromise({
|
|
148
|
+
try: () => Promise.resolve(result),
|
|
149
|
+
catch: (cause) => cause
|
|
150
|
+
})).pipe(Stream.flatMap(streamFromAgentLoopCallbackOutput));
|
|
151
|
+
return streamFromAgentLoopCallbackOutput(result);
|
|
152
|
+
}
|
|
153
|
+
function streamFromAgentLoopCallbackOutput(output) {
|
|
154
|
+
if (isEffectStream(output)) return output.pipe(Stream.map(normalizeAgentLoopEvent));
|
|
155
|
+
return (isAsyncIterable(output) ? Stream.fromAsyncIterable(output, (error) => error) : Stream.fromIterable(output)).pipe(Stream.map(normalizeAgentLoopEvent));
|
|
156
|
+
}
|
|
157
|
+
function isAsyncIterable(value) {
|
|
158
|
+
return Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
159
|
+
}
|
|
160
|
+
function isEffectStream(value) {
|
|
161
|
+
return typeof value === "object" && value !== null && Stream.StreamTypeId in value;
|
|
162
|
+
}
|
|
163
|
+
//#endregion
|
|
164
|
+
export { normalizeAgentLoopEvent as S, createAgentLoopToolExecutionStart as _, createTextAgentLoopFromCallback as a, createEventAgentLoop$1 as b, toEffectAgentLoop as c, createAgentLoopModelExecutionStart as d, createAgentLoopSkillExecutionEnd as f, createAgentLoopToolExecutionEnd as g, createAgentLoopThinkingDelta as h, createTextAgentLoop as i, toEffectAgentLoopInput as l, createAgentLoopTextDelta as m, createAsyncIterableAgentLoop as n, fromEffectAgentLoop as o, createAgentLoopSkillExecutionStart as p, createEventAgentLoop as r, toCompatibilityAgentLoopInput as s, createAgentLoopFromCallback as t, createAgentLoopModelExecutionEnd as u, createAgentLoopToolExecutionUpdate as v, createTextAgentLoop$1 as x, createAgentLoopTurnStart as y };
|