@rivus/agent 0.3.1 → 0.4.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/README.md +3 -3
- package/dist/acp.d.ts +98 -0
- package/dist/acp.js +436 -0
- package/dist/agent-loop.d.ts +420 -0
- package/dist/agent-loop.js +118 -0
- package/dist/agent-memory.d.ts +98 -0
- package/dist/cli.js +6 -2
- package/dist/index.d.ts +4 -418
- package/dist/index.js +63 -150
- package/dist/rivus-daemon-cli.js +11 -7
- package/dist/rivus-plugin-testkit.d.ts +3 -98
- package/examples/a-share-briefing-analysis.mjs +93 -0
- package/examples/a-share-briefing-renderer.mjs +257 -0
- package/examples/a-share-index-evidence.mjs +99 -0
- package/examples/a-share-market-briefing.mjs +83 -0
- package/examples/a-share-market-date.mjs +10 -0
- package/examples/a-share-overseas-evidence.mjs +86 -0
- package/examples/a-share-policy-evidence.mjs +145 -0
- package/examples/a-share-provider-response.mjs +21 -0
- package/examples/a-share-sector-evidence.mjs +70 -0
- package/examples/acp-stdio-proxy.mjs +55 -0
- package/examples/current-weather.mjs +6 -32
- package/examples/https-response-reader.mjs +36 -0
- package/examples/rivus-agents.plugin.mjs +55 -3
- package/examples/rivus.config.json +30 -1
- package/package.json +11 -2
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import { n as AgentMemoryIdentity } from "./agent-memory.js";
|
|
2
|
+
import { Effect, Stream } from "effect";
|
|
3
|
+
|
|
4
|
+
//#region src/domain/agent-events.d.ts
|
|
5
|
+
type AgentRunId = string;
|
|
6
|
+
type SessionKey = string;
|
|
7
|
+
interface AgentRunAccepted {
|
|
8
|
+
readonly type: "agent_run_accepted";
|
|
9
|
+
readonly runId: AgentRunId;
|
|
10
|
+
readonly sessionKey: SessionKey;
|
|
11
|
+
readonly prompt: string;
|
|
12
|
+
readonly occurredAt: Date;
|
|
13
|
+
}
|
|
14
|
+
interface AgentTurnStarted {
|
|
15
|
+
readonly type: "agent_turn_started";
|
|
16
|
+
readonly runId: AgentRunId;
|
|
17
|
+
readonly sessionKey: SessionKey;
|
|
18
|
+
readonly occurredAt: Date;
|
|
19
|
+
}
|
|
20
|
+
interface AssistantTextDelta {
|
|
21
|
+
readonly type: "assistant_text_delta";
|
|
22
|
+
readonly runId: AgentRunId;
|
|
23
|
+
readonly delta: string;
|
|
24
|
+
readonly occurredAt: Date;
|
|
25
|
+
}
|
|
26
|
+
interface AssistantThinkingDelta {
|
|
27
|
+
readonly type: "assistant_thinking_delta";
|
|
28
|
+
readonly runId: AgentRunId;
|
|
29
|
+
readonly delta: string;
|
|
30
|
+
readonly occurredAt: Date;
|
|
31
|
+
}
|
|
32
|
+
interface AgentModelExecutionStarted {
|
|
33
|
+
readonly type: "agent_model_execution_started";
|
|
34
|
+
readonly api: string;
|
|
35
|
+
readonly model: string;
|
|
36
|
+
readonly modelCallId: string;
|
|
37
|
+
readonly occurredAt: Date;
|
|
38
|
+
readonly provider: string;
|
|
39
|
+
readonly runId: AgentRunId;
|
|
40
|
+
}
|
|
41
|
+
interface AgentModelUsage {
|
|
42
|
+
readonly cacheRead: number;
|
|
43
|
+
readonly cacheWrite: number;
|
|
44
|
+
readonly cost: number;
|
|
45
|
+
readonly input: number;
|
|
46
|
+
readonly output: number;
|
|
47
|
+
readonly reasoning?: number;
|
|
48
|
+
readonly total: number;
|
|
49
|
+
}
|
|
50
|
+
interface AgentModelExecutionEnded {
|
|
51
|
+
readonly type: "agent_model_execution_ended";
|
|
52
|
+
readonly api: string;
|
|
53
|
+
readonly errorMessage?: string;
|
|
54
|
+
readonly model: string;
|
|
55
|
+
readonly modelCallId: string;
|
|
56
|
+
readonly occurredAt: Date;
|
|
57
|
+
readonly provider: string;
|
|
58
|
+
readonly responseModel?: string;
|
|
59
|
+
readonly runId: AgentRunId;
|
|
60
|
+
readonly stopReason: string;
|
|
61
|
+
readonly usage: AgentModelUsage;
|
|
62
|
+
}
|
|
63
|
+
interface AgentSkillExecutionStarted {
|
|
64
|
+
readonly type: "agent_skill_execution_started";
|
|
65
|
+
readonly occurredAt: Date;
|
|
66
|
+
readonly runId: AgentRunId;
|
|
67
|
+
readonly skillCallId: string;
|
|
68
|
+
readonly skillId: string;
|
|
69
|
+
}
|
|
70
|
+
interface AgentSkillExecutionSucceeded {
|
|
71
|
+
readonly type: "agent_skill_execution_ended";
|
|
72
|
+
readonly contentLength: number;
|
|
73
|
+
readonly digest: string;
|
|
74
|
+
readonly isError: false;
|
|
75
|
+
readonly occurredAt: Date;
|
|
76
|
+
readonly runId: AgentRunId;
|
|
77
|
+
readonly skillCallId: string;
|
|
78
|
+
readonly skillId: string;
|
|
79
|
+
readonly title: string;
|
|
80
|
+
readonly version: string;
|
|
81
|
+
}
|
|
82
|
+
interface AgentSkillExecutionFailed {
|
|
83
|
+
readonly type: "agent_skill_execution_ended";
|
|
84
|
+
readonly isError: true;
|
|
85
|
+
readonly occurredAt: Date;
|
|
86
|
+
readonly runId: AgentRunId;
|
|
87
|
+
readonly skillCallId: string;
|
|
88
|
+
readonly skillId: string;
|
|
89
|
+
}
|
|
90
|
+
type AgentSkillExecutionEnded = AgentSkillExecutionSucceeded | AgentSkillExecutionFailed;
|
|
91
|
+
interface AgentToolExecutionStarted {
|
|
92
|
+
readonly type: "agent_tool_execution_started";
|
|
93
|
+
readonly runId: AgentRunId;
|
|
94
|
+
readonly toolCallId: string;
|
|
95
|
+
readonly toolName: string;
|
|
96
|
+
readonly input: unknown;
|
|
97
|
+
readonly occurredAt: Date;
|
|
98
|
+
}
|
|
99
|
+
interface AgentToolExecutionUpdated {
|
|
100
|
+
readonly type: "agent_tool_execution_updated";
|
|
101
|
+
readonly runId: AgentRunId;
|
|
102
|
+
readonly toolCallId: string;
|
|
103
|
+
readonly toolName: string;
|
|
104
|
+
readonly input: unknown;
|
|
105
|
+
readonly partialResult: unknown;
|
|
106
|
+
readonly occurredAt: Date;
|
|
107
|
+
}
|
|
108
|
+
interface AgentToolExecutionEnded {
|
|
109
|
+
readonly type: "agent_tool_execution_ended";
|
|
110
|
+
readonly runId: AgentRunId;
|
|
111
|
+
readonly toolCallId: string;
|
|
112
|
+
readonly toolName: string;
|
|
113
|
+
readonly result: unknown;
|
|
114
|
+
readonly isError: boolean;
|
|
115
|
+
readonly occurredAt: Date;
|
|
116
|
+
}
|
|
117
|
+
interface AgentTurnCompleted {
|
|
118
|
+
readonly type: "agent_turn_completed";
|
|
119
|
+
readonly runId: AgentRunId;
|
|
120
|
+
readonly finalText: string;
|
|
121
|
+
readonly occurredAt: Date;
|
|
122
|
+
}
|
|
123
|
+
interface AgentRunCompleted {
|
|
124
|
+
readonly type: "agent_run_completed";
|
|
125
|
+
readonly runId: AgentRunId;
|
|
126
|
+
readonly finalText: string;
|
|
127
|
+
readonly occurredAt: Date;
|
|
128
|
+
}
|
|
129
|
+
interface AgentRunFailed {
|
|
130
|
+
readonly type: "agent_run_failed";
|
|
131
|
+
readonly runId: AgentRunId;
|
|
132
|
+
readonly errorMessage: string;
|
|
133
|
+
readonly occurredAt: Date;
|
|
134
|
+
}
|
|
135
|
+
interface AgentRunCancelled {
|
|
136
|
+
readonly type: "agent_run_cancelled";
|
|
137
|
+
readonly runId: AgentRunId;
|
|
138
|
+
readonly reason?: string;
|
|
139
|
+
readonly occurredAt: Date;
|
|
140
|
+
}
|
|
141
|
+
type AgentDomainEvent = AgentRunAccepted | AgentTurnStarted | AssistantTextDelta | AssistantThinkingDelta | AgentModelExecutionStarted | AgentModelExecutionEnded | AgentSkillExecutionStarted | AgentSkillExecutionEnded | AgentToolExecutionStarted | AgentToolExecutionUpdated | AgentToolExecutionEnded | AgentTurnCompleted | AgentRunCompleted | AgentRunFailed | AgentRunCancelled;
|
|
142
|
+
type AgentToolExecutionEvent = AgentToolExecutionStarted | AgentToolExecutionUpdated | AgentToolExecutionEnded;
|
|
143
|
+
type AgentModelExecutionEvent = AgentModelExecutionStarted | AgentModelExecutionEnded;
|
|
144
|
+
type TerminalAgentDomainEvent = AgentRunCompleted | AgentRunFailed | AgentRunCancelled;
|
|
145
|
+
declare function isAssistantTextDeltaEvent(event: AgentDomainEvent): event is AssistantTextDelta;
|
|
146
|
+
declare function isAssistantThinkingDeltaEvent(event: AgentDomainEvent): event is AssistantThinkingDelta;
|
|
147
|
+
declare function isAgentToolExecutionEvent(event: AgentDomainEvent): event is AgentToolExecutionEvent;
|
|
148
|
+
declare function isTerminalAgentDomainEvent(event: AgentDomainEvent): event is TerminalAgentDomainEvent;
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/domain/agent-run-state.d.ts
|
|
151
|
+
type AgentRunPhase = "idle" | "accepted" | "running" | "completed" | "failed" | "cancelled";
|
|
152
|
+
interface AgentRunState {
|
|
153
|
+
readonly phase: AgentRunPhase;
|
|
154
|
+
readonly runId?: AgentRunId;
|
|
155
|
+
readonly sessionKey?: SessionKey;
|
|
156
|
+
readonly prompt?: string;
|
|
157
|
+
readonly finalText: string;
|
|
158
|
+
readonly errorMessage?: string;
|
|
159
|
+
readonly cancellationReason?: string;
|
|
160
|
+
readonly thinkingText?: string;
|
|
161
|
+
readonly skillExecutions: Readonly<Record<string, AgentSkillExecutionState>>;
|
|
162
|
+
readonly toolExecutions: Readonly<Record<string, AgentToolExecutionState>>;
|
|
163
|
+
}
|
|
164
|
+
interface AgentSkillExecutionState {
|
|
165
|
+
readonly contentLength?: number;
|
|
166
|
+
readonly digest?: string;
|
|
167
|
+
readonly isError?: boolean;
|
|
168
|
+
readonly skillCallId: string;
|
|
169
|
+
readonly skillId: string;
|
|
170
|
+
readonly status: "running" | "completed";
|
|
171
|
+
readonly title?: string;
|
|
172
|
+
readonly version?: string;
|
|
173
|
+
}
|
|
174
|
+
interface AgentToolExecutionState {
|
|
175
|
+
readonly toolCallId: string;
|
|
176
|
+
readonly toolName: string;
|
|
177
|
+
readonly input: unknown;
|
|
178
|
+
readonly partialResult?: unknown;
|
|
179
|
+
readonly result?: unknown;
|
|
180
|
+
readonly isError?: boolean;
|
|
181
|
+
readonly status: "running" | "completed";
|
|
182
|
+
}
|
|
183
|
+
declare const initialAgentRunState: AgentRunState;
|
|
184
|
+
declare function isTerminalAgentRunPhase(phase: AgentRunPhase): boolean;
|
|
185
|
+
declare function evolveAgentRun(state: AgentRunState, event: AgentDomainEvent): AgentRunState;
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region src/domain/agent-history.d.ts
|
|
188
|
+
interface AgentRunSummary {
|
|
189
|
+
readonly acceptedAt?: Date;
|
|
190
|
+
readonly cancellationReason?: string;
|
|
191
|
+
readonly cancelledAt?: Date;
|
|
192
|
+
readonly completedAt?: Date;
|
|
193
|
+
readonly errorMessage?: string;
|
|
194
|
+
readonly eventCount: number;
|
|
195
|
+
readonly failedToolExecutionCount: number;
|
|
196
|
+
readonly failedSkillExecutionCount: number;
|
|
197
|
+
readonly failedAt?: Date;
|
|
198
|
+
readonly finalText: string;
|
|
199
|
+
readonly phase: AgentRunPhase;
|
|
200
|
+
readonly prompt?: string;
|
|
201
|
+
readonly runId: AgentRunId;
|
|
202
|
+
readonly sessionKey?: SessionKey;
|
|
203
|
+
readonly startedAt?: Date;
|
|
204
|
+
readonly state: AgentRunState;
|
|
205
|
+
readonly thinkingText: string;
|
|
206
|
+
readonly skillExecutionCount: number;
|
|
207
|
+
readonly toolExecutionCount: number;
|
|
208
|
+
readonly updatedAt: Date;
|
|
209
|
+
}
|
|
210
|
+
interface AgentSessionSummary {
|
|
211
|
+
readonly failedToolExecutionCount: number;
|
|
212
|
+
readonly failedSkillExecutionCount: number;
|
|
213
|
+
readonly finalText: string;
|
|
214
|
+
readonly latestPhase: AgentRunPhase;
|
|
215
|
+
readonly latestRunId: AgentRunId;
|
|
216
|
+
readonly runCount: number;
|
|
217
|
+
readonly runIds: ReadonlyArray<AgentRunId>;
|
|
218
|
+
readonly sessionKey: SessionKey;
|
|
219
|
+
readonly thinkingText: string;
|
|
220
|
+
readonly skillExecutionCount: number;
|
|
221
|
+
readonly toolExecutionCount: number;
|
|
222
|
+
readonly updatedAt: Date;
|
|
223
|
+
}
|
|
224
|
+
interface AgentHistory {
|
|
225
|
+
readonly runs: ReadonlyArray<AgentRunSummary>;
|
|
226
|
+
readonly sessions: ReadonlyArray<AgentSessionSummary>;
|
|
227
|
+
readonly totalEvents: number;
|
|
228
|
+
}
|
|
229
|
+
declare function replayAgentHistory(events: ReadonlyArray<AgentDomainEvent>): AgentHistory;
|
|
230
|
+
//#endregion
|
|
231
|
+
//#region src/domain/agent-transcript.d.ts
|
|
232
|
+
interface AgentTranscript {
|
|
233
|
+
readonly latestTurn?: AgentTranscriptTurn;
|
|
234
|
+
readonly sessionKey: SessionKey;
|
|
235
|
+
readonly turnCount: number;
|
|
236
|
+
readonly turns: ReadonlyArray<AgentTranscriptTurn>;
|
|
237
|
+
}
|
|
238
|
+
interface AgentTranscriptTurn {
|
|
239
|
+
readonly acceptedAt?: Date;
|
|
240
|
+
readonly assistantText: string;
|
|
241
|
+
readonly cancellationReason?: string;
|
|
242
|
+
readonly cancelledAt?: Date;
|
|
243
|
+
readonly completedAt?: Date;
|
|
244
|
+
readonly errorMessage?: string;
|
|
245
|
+
readonly failedAt?: Date;
|
|
246
|
+
readonly failedToolExecutionCount: number;
|
|
247
|
+
readonly phase: AgentRunPhase;
|
|
248
|
+
readonly runId: AgentRunId;
|
|
249
|
+
readonly sessionKey: SessionKey;
|
|
250
|
+
readonly thinkingText: string;
|
|
251
|
+
readonly toolExecutionCount: number;
|
|
252
|
+
readonly updatedAt: Date;
|
|
253
|
+
readonly userText: string;
|
|
254
|
+
}
|
|
255
|
+
type AgentTranscriptMessageRole = "assistant" | "user";
|
|
256
|
+
interface AgentTranscriptMessage {
|
|
257
|
+
readonly content: string;
|
|
258
|
+
readonly role: AgentTranscriptMessageRole;
|
|
259
|
+
}
|
|
260
|
+
interface AgentConversationMessagesOptions {
|
|
261
|
+
readonly text: string;
|
|
262
|
+
readonly transcript?: AgentTranscript;
|
|
263
|
+
}
|
|
264
|
+
declare function replayAgentTranscript(events: ReadonlyArray<AgentDomainEvent>, sessionKey: SessionKey): AgentTranscript;
|
|
265
|
+
declare function createAgentTranscriptTurn(summary: AgentRunSummary & {
|
|
266
|
+
readonly sessionKey: SessionKey;
|
|
267
|
+
}): AgentTranscriptTurn;
|
|
268
|
+
declare function createAgentTranscriptMessages(transcript: AgentTranscript): ReadonlyArray<AgentTranscriptMessage>;
|
|
269
|
+
declare function createAgentConversationMessages(options: AgentConversationMessagesOptions): ReadonlyArray<AgentTranscriptMessage>;
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/domain/agent-invocation.d.ts
|
|
272
|
+
type AgentMemoryInvocationIdentity = AgentMemoryIdentity;
|
|
273
|
+
interface FeishuAgentInvocationOrigin {
|
|
274
|
+
readonly allowedActorOpenIds: ReadonlyArray<string>;
|
|
275
|
+
readonly endpointId: string;
|
|
276
|
+
readonly kind: "feishu";
|
|
277
|
+
readonly memory?: AgentMemoryInvocationIdentity;
|
|
278
|
+
readonly sourceMessageId: string;
|
|
279
|
+
readonly tenantKey: string;
|
|
280
|
+
}
|
|
281
|
+
interface LocalCliAgentInvocationOrigin {
|
|
282
|
+
readonly allowedActorOpenIds: readonly [];
|
|
283
|
+
readonly endpointId: string;
|
|
284
|
+
readonly kind: "local-cli";
|
|
285
|
+
readonly memory?: AgentMemoryInvocationIdentity;
|
|
286
|
+
readonly sourceMessageId: string;
|
|
287
|
+
readonly tenantKey: "local";
|
|
288
|
+
}
|
|
289
|
+
interface AutomationAgentInvocationOrigin {
|
|
290
|
+
readonly allowedActorOpenIds: readonly [];
|
|
291
|
+
readonly automationId: string;
|
|
292
|
+
readonly endpointId: string;
|
|
293
|
+
readonly kind: "automation";
|
|
294
|
+
readonly memory?: AgentMemoryInvocationIdentity;
|
|
295
|
+
readonly sourceMessageId: string;
|
|
296
|
+
readonly tenantKey: "automation";
|
|
297
|
+
readonly tickId: string;
|
|
298
|
+
}
|
|
299
|
+
type AgentInvocationOrigin = FeishuAgentInvocationOrigin | LocalCliAgentInvocationOrigin | AutomationAgentInvocationOrigin;
|
|
300
|
+
//#endregion
|
|
301
|
+
//#region src/application/agent/agent-loop.d.ts
|
|
302
|
+
interface AgentLoopInput {
|
|
303
|
+
readonly abortSignal: AbortSignal;
|
|
304
|
+
readonly invocation?: AgentInvocationOrigin;
|
|
305
|
+
readonly messages?: ReadonlyArray<AgentTranscriptMessage>;
|
|
306
|
+
readonly runId: AgentRunId;
|
|
307
|
+
readonly sessionKey: SessionKey;
|
|
308
|
+
readonly text: string;
|
|
309
|
+
readonly previousSessionState?: AgentRunState;
|
|
310
|
+
readonly previousSessionTranscript?: AgentTranscript;
|
|
311
|
+
}
|
|
312
|
+
type AgentLoopEvent = AgentLoopTextDelta | AgentLoopThinkingDelta | AgentLoopModelExecutionStart | AgentLoopModelExecutionEnd | AgentLoopSkillExecutionStart | AgentLoopSkillExecutionEnd | AgentLoopToolExecutionStart | AgentLoopToolExecutionUpdate | AgentLoopToolExecutionEnd;
|
|
313
|
+
type AgentLoopEventLike = AgentLoopEvent | string;
|
|
314
|
+
interface AgentLoopTextDelta {
|
|
315
|
+
readonly type: "assistant_text_delta";
|
|
316
|
+
readonly delta: string;
|
|
317
|
+
}
|
|
318
|
+
interface AgentLoopThinkingDelta {
|
|
319
|
+
readonly type: "assistant_thinking_delta";
|
|
320
|
+
readonly delta: string;
|
|
321
|
+
}
|
|
322
|
+
interface AgentLoopModelExecutionStart {
|
|
323
|
+
readonly type: "model_execution_start";
|
|
324
|
+
readonly api: string;
|
|
325
|
+
readonly model: string;
|
|
326
|
+
readonly modelCallId: string;
|
|
327
|
+
readonly provider: string;
|
|
328
|
+
}
|
|
329
|
+
interface AgentLoopModelExecutionEnd {
|
|
330
|
+
readonly type: "model_execution_end";
|
|
331
|
+
readonly api: string;
|
|
332
|
+
readonly errorMessage?: string;
|
|
333
|
+
readonly model: string;
|
|
334
|
+
readonly modelCallId: string;
|
|
335
|
+
readonly provider: string;
|
|
336
|
+
readonly responseModel?: string;
|
|
337
|
+
readonly stopReason: string;
|
|
338
|
+
readonly usage: AgentModelUsage;
|
|
339
|
+
}
|
|
340
|
+
interface AgentLoopSkillExecutionStart {
|
|
341
|
+
readonly type: "skill_execution_start";
|
|
342
|
+
readonly skillCallId: string;
|
|
343
|
+
readonly skillId: string;
|
|
344
|
+
}
|
|
345
|
+
interface AgentLoopSkillExecutionSucceeded {
|
|
346
|
+
readonly type: "skill_execution_end";
|
|
347
|
+
readonly contentLength: number;
|
|
348
|
+
readonly digest: string;
|
|
349
|
+
readonly isError: false;
|
|
350
|
+
readonly skillCallId: string;
|
|
351
|
+
readonly skillId: string;
|
|
352
|
+
readonly title: string;
|
|
353
|
+
readonly version: string;
|
|
354
|
+
}
|
|
355
|
+
interface AgentLoopSkillExecutionFailed {
|
|
356
|
+
readonly type: "skill_execution_end";
|
|
357
|
+
readonly isError: true;
|
|
358
|
+
readonly skillCallId: string;
|
|
359
|
+
readonly skillId: string;
|
|
360
|
+
}
|
|
361
|
+
type AgentLoopSkillExecutionEnd = AgentLoopSkillExecutionSucceeded | AgentLoopSkillExecutionFailed;
|
|
362
|
+
interface AgentLoopToolExecutionStart {
|
|
363
|
+
readonly type: "tool_execution_start";
|
|
364
|
+
readonly toolCallId: string;
|
|
365
|
+
readonly toolName: string;
|
|
366
|
+
readonly input: unknown;
|
|
367
|
+
}
|
|
368
|
+
interface AgentLoopToolExecutionUpdate {
|
|
369
|
+
readonly type: "tool_execution_update";
|
|
370
|
+
readonly toolCallId: string;
|
|
371
|
+
readonly toolName: string;
|
|
372
|
+
readonly input: unknown;
|
|
373
|
+
readonly partialResult: unknown;
|
|
374
|
+
}
|
|
375
|
+
interface AgentLoopToolExecutionEnd {
|
|
376
|
+
readonly type: "tool_execution_end";
|
|
377
|
+
readonly toolCallId: string;
|
|
378
|
+
readonly toolName: string;
|
|
379
|
+
readonly result: unknown;
|
|
380
|
+
readonly isError: boolean;
|
|
381
|
+
}
|
|
382
|
+
interface AgentLoop {
|
|
383
|
+
run(input: AgentLoopInput): Stream.Stream<AgentLoopEvent, unknown>;
|
|
384
|
+
}
|
|
385
|
+
type AgentLoopCallbackOutput = Iterable<AgentLoopEventLike> | AsyncIterable<AgentLoopEventLike> | Stream.Stream<AgentLoopEventLike, unknown>;
|
|
386
|
+
type AgentLoopCallbackResult = AgentLoopCallbackOutput | PromiseLike<AgentLoopCallbackOutput> | Effect.Effect<AgentLoopCallbackOutput, unknown>;
|
|
387
|
+
type AgentLoopCallback = (input: AgentLoopInput) => AgentLoopCallbackResult;
|
|
388
|
+
interface AsyncIterableAgentLoopOptions {
|
|
389
|
+
readonly run: (input: AgentLoopInput) => AsyncIterable<AgentLoopEventLike>;
|
|
390
|
+
}
|
|
391
|
+
interface EventAgentLoopOptions {
|
|
392
|
+
readonly events: ReadonlyArray<AgentLoopEventLike> | ((input: AgentLoopInput) => ReadonlyArray<AgentLoopEventLike> | Promise<ReadonlyArray<AgentLoopEventLike>>);
|
|
393
|
+
}
|
|
394
|
+
interface TextAgentLoopOptions {
|
|
395
|
+
readonly generate: (input: AgentLoopInput) => Effect.Effect<string, unknown>;
|
|
396
|
+
}
|
|
397
|
+
type TextAgentLoopCallback = (input: AgentLoopInput) => string | Promise<string>;
|
|
398
|
+
type AgentLoopToolExecutionStartOptions = Omit<AgentLoopToolExecutionStart, "type">;
|
|
399
|
+
type AgentLoopToolExecutionUpdateOptions = Omit<AgentLoopToolExecutionUpdate, "type">;
|
|
400
|
+
type AgentLoopToolExecutionEndOptions = Omit<AgentLoopToolExecutionEnd, "type">;
|
|
401
|
+
type AgentLoopModelExecutionStartOptions = Omit<AgentLoopModelExecutionStart, "type">;
|
|
402
|
+
type AgentLoopModelExecutionEndOptions = Omit<AgentLoopModelExecutionEnd, "type">;
|
|
403
|
+
type AgentLoopSkillExecutionStartOptions = Omit<AgentLoopSkillExecutionStart, "type">;
|
|
404
|
+
type AgentLoopSkillExecutionEndOptions = Omit<AgentLoopSkillExecutionSucceeded, "type"> | Omit<AgentLoopSkillExecutionFailed, "type">;
|
|
405
|
+
declare function createAgentLoopTextDelta(delta: string): AgentLoopTextDelta;
|
|
406
|
+
declare function createAgentLoopThinkingDelta(delta: string): AgentLoopThinkingDelta;
|
|
407
|
+
declare function createAgentLoopModelExecutionStart(options: AgentLoopModelExecutionStartOptions): AgentLoopModelExecutionStart;
|
|
408
|
+
declare function createAgentLoopModelExecutionEnd(options: AgentLoopModelExecutionEndOptions): AgentLoopModelExecutionEnd;
|
|
409
|
+
declare function createAgentLoopSkillExecutionStart(options: AgentLoopSkillExecutionStartOptions): AgentLoopSkillExecutionStart;
|
|
410
|
+
declare function createAgentLoopSkillExecutionEnd(options: AgentLoopSkillExecutionEndOptions): AgentLoopSkillExecutionEnd;
|
|
411
|
+
declare function createAgentLoopToolExecutionStart(options: AgentLoopToolExecutionStartOptions): AgentLoopToolExecutionStart;
|
|
412
|
+
declare function createAgentLoopToolExecutionUpdate(options: AgentLoopToolExecutionUpdateOptions): AgentLoopToolExecutionUpdate;
|
|
413
|
+
declare function createAgentLoopToolExecutionEnd(options: AgentLoopToolExecutionEndOptions): AgentLoopToolExecutionEnd;
|
|
414
|
+
declare function createEventAgentLoop(options: EventAgentLoopOptions): AgentLoop;
|
|
415
|
+
declare function createAsyncIterableAgentLoop(options: AsyncIterableAgentLoopOptions): AgentLoop;
|
|
416
|
+
declare function createAgentLoopFromCallback(run: AgentLoopCallback): AgentLoop;
|
|
417
|
+
declare function createTextAgentLoop(options: TextAgentLoopOptions): AgentLoop;
|
|
418
|
+
declare function createTextAgentLoopFromCallback(generate: TextAgentLoopCallback): AgentLoop;
|
|
419
|
+
//#endregion
|
|
420
|
+
export { createAgentTranscriptTurn as $, createAgentLoopModelExecutionStart as A, AssistantThinkingDelta as At, createTextAgentLoop as B, AgentLoopToolExecutionUpdateOptions as C, AgentToolExecutionEnded as Ct, TextAgentLoopOptions as D, AgentTurnCompleted as Dt, TextAgentLoopCallback as E, AgentToolExecutionUpdated as Et, createAgentLoopToolExecutionEnd as F, isAssistantThinkingDeltaEvent as Ft, LocalCliAgentInvocationOrigin as G, AgentInvocationOrigin as H, createAgentLoopToolExecutionStart as I, isTerminalAgentDomainEvent as It, AgentTranscriptMessage as J, AgentConversationMessagesOptions as K, createAgentLoopToolExecutionUpdate as L, createAgentLoopSkillExecutionStart as M, TerminalAgentDomainEvent as Mt, createAgentLoopTextDelta as N, isAgentToolExecutionEvent as Nt, createAgentLoopFromCallback as O, AgentTurnStarted as Ot, createAgentLoopThinkingDelta as P, isAssistantTextDeltaEvent as Pt, createAgentTranscriptMessages as Q, createAsyncIterableAgentLoop as R, AgentLoopToolExecutionUpdate as S, AgentSkillExecutionStarted as St, EventAgentLoopOptions as T, AgentToolExecutionStarted as Tt, AutomationAgentInvocationOrigin as U, createTextAgentLoopFromCallback as V, FeishuAgentInvocationOrigin as W, AgentTranscriptTurn as X, AgentTranscriptMessageRole as Y, createAgentConversationMessages as Z, AgentLoopThinkingDelta as _, AgentRunCancelled as _t, AgentLoopEvent as a, AgentRunPhase as at, AgentLoopToolExecutionStart as b, AgentRunId as bt, AgentLoopModelExecutionEnd as c, evolveAgentRun as ct, AgentLoopModelExecutionStartOptions as d, AgentDomainEvent as dt, replayAgentTranscript as et, AgentLoopSkillExecutionEnd as f, AgentModelExecutionEnded as ft, AgentLoopTextDelta as g, AgentRunAccepted as gt, AgentLoopSkillExecutionStartOptions as h, AgentModelUsage as ht, AgentLoopCallbackResult as i, replayAgentHistory as it, createAgentLoopSkillExecutionEnd as j, SessionKey as jt, createAgentLoopModelExecutionEnd as k, AssistantTextDelta as kt, AgentLoopModelExecutionEndOptions as l, initialAgentRunState as lt, AgentLoopSkillExecutionStart as m, AgentModelExecutionStarted as mt, AgentLoopCallback as n, AgentRunSummary as nt, AgentLoopEventLike as o, AgentRunState as ot, AgentLoopSkillExecutionEndOptions as p, AgentModelExecutionEvent as pt, AgentTranscript as q, AgentLoopCallbackOutput as r, AgentSessionSummary as rt, AgentLoopInput as s, AgentSkillExecutionState as st, AgentLoop as t, AgentHistory as tt, AgentLoopModelExecutionStart as u, isTerminalAgentRunPhase as ut, AgentLoopToolExecutionEnd as v, AgentRunCompleted as vt, AsyncIterableAgentLoopOptions as w, AgentToolExecutionEvent as wt, AgentLoopToolExecutionStartOptions as x, AgentSkillExecutionEnded as xt, AgentLoopToolExecutionEndOptions as y, AgentRunFailed as yt, createEventAgentLoop as z };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Effect, Stream } from "effect";
|
|
2
|
+
//#region src/application/agent/agent-loop.ts
|
|
3
|
+
function createAgentLoopTextDelta(delta) {
|
|
4
|
+
return {
|
|
5
|
+
delta,
|
|
6
|
+
type: "assistant_text_delta"
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function normalizeAgentLoopEvent(event) {
|
|
10
|
+
return typeof event === "string" ? createAgentLoopTextDelta(event) : event;
|
|
11
|
+
}
|
|
12
|
+
function createAgentLoopThinkingDelta(delta) {
|
|
13
|
+
return {
|
|
14
|
+
delta,
|
|
15
|
+
type: "assistant_thinking_delta"
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function createAgentLoopModelExecutionStart(options) {
|
|
19
|
+
return {
|
|
20
|
+
...options,
|
|
21
|
+
type: "model_execution_start"
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function createAgentLoopModelExecutionEnd(options) {
|
|
25
|
+
return {
|
|
26
|
+
...options,
|
|
27
|
+
type: "model_execution_end"
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function createAgentLoopSkillExecutionStart(options) {
|
|
31
|
+
return {
|
|
32
|
+
...options,
|
|
33
|
+
type: "skill_execution_start"
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function createAgentLoopSkillExecutionEnd(options) {
|
|
37
|
+
return {
|
|
38
|
+
...options,
|
|
39
|
+
type: "skill_execution_end"
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function createAgentLoopToolExecutionStart(options) {
|
|
43
|
+
return {
|
|
44
|
+
input: options.input,
|
|
45
|
+
toolCallId: options.toolCallId,
|
|
46
|
+
toolName: options.toolName,
|
|
47
|
+
type: "tool_execution_start"
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function createAgentLoopToolExecutionUpdate(options) {
|
|
51
|
+
return {
|
|
52
|
+
input: options.input,
|
|
53
|
+
partialResult: options.partialResult,
|
|
54
|
+
toolCallId: options.toolCallId,
|
|
55
|
+
toolName: options.toolName,
|
|
56
|
+
type: "tool_execution_update"
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function createAgentLoopToolExecutionEnd(options) {
|
|
60
|
+
return {
|
|
61
|
+
isError: options.isError,
|
|
62
|
+
result: options.result,
|
|
63
|
+
toolCallId: options.toolCallId,
|
|
64
|
+
toolName: options.toolName,
|
|
65
|
+
type: "tool_execution_end"
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function createEventAgentLoop(options) {
|
|
69
|
+
return { run: (input) => {
|
|
70
|
+
const events = typeof options.events === "function" ? options.events(input) : options.events;
|
|
71
|
+
if (isPromiseLike(events)) return Stream.fromEffect(Effect.tryPromise({
|
|
72
|
+
try: () => events,
|
|
73
|
+
catch: (cause) => cause
|
|
74
|
+
})).pipe(Stream.flatMap((resolvedEvents) => Stream.fromIterable(resolvedEvents)), Stream.map(normalizeAgentLoopEvent));
|
|
75
|
+
return Stream.fromIterable(events).pipe(Stream.map(normalizeAgentLoopEvent));
|
|
76
|
+
} };
|
|
77
|
+
}
|
|
78
|
+
function createAsyncIterableAgentLoop(options) {
|
|
79
|
+
return { run: (input) => Stream.fromAsyncIterable(options.run(input), (error) => error).pipe(Stream.map(normalizeAgentLoopEvent)) };
|
|
80
|
+
}
|
|
81
|
+
function createAgentLoopFromCallback(run) {
|
|
82
|
+
return { run: (input) => Stream.fromEffect(Effect.try({
|
|
83
|
+
try: () => run(input),
|
|
84
|
+
catch: (cause) => cause
|
|
85
|
+
})).pipe(Stream.flatMap(streamFromAgentLoopCallbackResult)) };
|
|
86
|
+
}
|
|
87
|
+
function createTextAgentLoop(options) {
|
|
88
|
+
return { run: (input) => Stream.fromEffect(options.generate(input)).pipe(Stream.map((delta) => createAgentLoopTextDelta(delta))) };
|
|
89
|
+
}
|
|
90
|
+
function createTextAgentLoopFromCallback(generate) {
|
|
91
|
+
return createTextAgentLoop({ generate: (input) => Effect.tryPromise({
|
|
92
|
+
try: async () => generate(input),
|
|
93
|
+
catch: (cause) => cause
|
|
94
|
+
}) });
|
|
95
|
+
}
|
|
96
|
+
function isPromiseLike(value) {
|
|
97
|
+
return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
98
|
+
}
|
|
99
|
+
function streamFromAgentLoopCallbackResult(result) {
|
|
100
|
+
if (Effect.isEffect(result)) return Stream.fromEffect(result).pipe(Stream.flatMap(streamFromAgentLoopCallbackOutput));
|
|
101
|
+
if (isPromiseLike(result)) return Stream.fromEffect(Effect.tryPromise({
|
|
102
|
+
try: () => Promise.resolve(result),
|
|
103
|
+
catch: (cause) => cause
|
|
104
|
+
})).pipe(Stream.flatMap(streamFromAgentLoopCallbackOutput));
|
|
105
|
+
return streamFromAgentLoopCallbackOutput(result);
|
|
106
|
+
}
|
|
107
|
+
function streamFromAgentLoopCallbackOutput(output) {
|
|
108
|
+
if (isEffectStream(output)) return output.pipe(Stream.map(normalizeAgentLoopEvent));
|
|
109
|
+
return (isAsyncIterable(output) ? Stream.fromAsyncIterable(output, (error) => error) : Stream.fromIterable(output)).pipe(Stream.map(normalizeAgentLoopEvent));
|
|
110
|
+
}
|
|
111
|
+
function isAsyncIterable(value) {
|
|
112
|
+
return Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
113
|
+
}
|
|
114
|
+
function isEffectStream(value) {
|
|
115
|
+
return typeof value === "object" && value !== null && Stream.StreamTypeId in value;
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
export { createAgentLoopSkillExecutionStart as a, createAgentLoopToolExecutionEnd as c, createAsyncIterableAgentLoop as d, createEventAgentLoop as f, createAgentLoopSkillExecutionEnd as i, createAgentLoopToolExecutionStart as l, createTextAgentLoopFromCallback as m, createAgentLoopModelExecutionEnd as n, createAgentLoopTextDelta as o, createTextAgentLoop as p, createAgentLoopModelExecutionStart as r, createAgentLoopThinkingDelta as s, createAgentLoopFromCallback as t, createAgentLoopToolExecutionUpdate as u };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
//#region src/domain/agent-memory.d.ts
|
|
2
|
+
declare const MEMORY_SCOPES: readonly ["conversation", "agent-private", "shared-user-profile"];
|
|
3
|
+
declare const RIVUS_MEMORY_TOOL_ID = "memory";
|
|
4
|
+
declare const RIVUS_MEMORY_TOOL_PLUGIN_ID = "rivus-core";
|
|
5
|
+
declare const RIVUS_MEMORY_TOOL_VERSION = "1.0.0";
|
|
6
|
+
type MemoryScope = (typeof MEMORY_SCOPES)[number];
|
|
7
|
+
type MemoryState = "proposed" | "confirmed" | "superseded" | "tombstoned";
|
|
8
|
+
type MemoryInvocationAudience = "group" | "private";
|
|
9
|
+
interface AgentMemoryIdentity {
|
|
10
|
+
readonly audience: MemoryInvocationAudience;
|
|
11
|
+
readonly conversationId?: string;
|
|
12
|
+
readonly subjectId: string;
|
|
13
|
+
readonly tenantId: string;
|
|
14
|
+
}
|
|
15
|
+
interface AgentMemoryAuthority extends AgentMemoryIdentity {
|
|
16
|
+
readonly scopes: ReadonlyArray<MemoryScope>;
|
|
17
|
+
}
|
|
18
|
+
interface MemoryBinding {
|
|
19
|
+
readonly agentId: string;
|
|
20
|
+
readonly conversationId?: string;
|
|
21
|
+
readonly scope: MemoryScope;
|
|
22
|
+
readonly subjectId: string;
|
|
23
|
+
readonly tenantId: string;
|
|
24
|
+
}
|
|
25
|
+
interface MemoryRecord {
|
|
26
|
+
readonly content: string;
|
|
27
|
+
readonly conversationSafe: boolean;
|
|
28
|
+
readonly id: string;
|
|
29
|
+
readonly revision: number;
|
|
30
|
+
readonly scope: MemoryScope;
|
|
31
|
+
readonly state: MemoryState;
|
|
32
|
+
readonly tombstoneReason?: string;
|
|
33
|
+
}
|
|
34
|
+
interface AgentMemorySnapshot {
|
|
35
|
+
readonly binding: MemoryBinding;
|
|
36
|
+
readonly record: MemoryRecord;
|
|
37
|
+
}
|
|
38
|
+
declare function createMemoryNamespace(binding: MemoryBinding): string;
|
|
39
|
+
declare function restrictMemoryScopesForAudience(scopes: ReadonlyArray<MemoryScope>, audience: MemoryInvocationAudience): ReadonlyArray<MemoryScope>;
|
|
40
|
+
declare function createRivusMemoryToolContract(scopes: ReadonlyArray<MemoryScope>): Readonly<{
|
|
41
|
+
description: "Search and read Memory inside Host-bound scopes; propose or request forgetting only in writable private scopes.";
|
|
42
|
+
digest: "sha256:rivus-memory-v3";
|
|
43
|
+
id: "memory";
|
|
44
|
+
idempotency: "required";
|
|
45
|
+
inputSchema: Readonly<{
|
|
46
|
+
additionalProperties: false;
|
|
47
|
+
properties: {
|
|
48
|
+
scope?: {
|
|
49
|
+
description: string;
|
|
50
|
+
enum: ("conversation" | "agent-private" | "shared-user-profile")[];
|
|
51
|
+
type: string;
|
|
52
|
+
};
|
|
53
|
+
command: {
|
|
54
|
+
description: string;
|
|
55
|
+
enum: string[];
|
|
56
|
+
type: string;
|
|
57
|
+
};
|
|
58
|
+
id: {
|
|
59
|
+
description: string;
|
|
60
|
+
minLength: number;
|
|
61
|
+
type: string;
|
|
62
|
+
};
|
|
63
|
+
input: {
|
|
64
|
+
additionalProperties: boolean;
|
|
65
|
+
description: string;
|
|
66
|
+
properties: {
|
|
67
|
+
content: {
|
|
68
|
+
minLength: number;
|
|
69
|
+
type: string;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
required: string[];
|
|
73
|
+
type: string;
|
|
74
|
+
};
|
|
75
|
+
query: {
|
|
76
|
+
additionalProperties: boolean;
|
|
77
|
+
description: string;
|
|
78
|
+
properties: {
|
|
79
|
+
query: {
|
|
80
|
+
type: string;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
required: string[];
|
|
84
|
+
type: string;
|
|
85
|
+
};
|
|
86
|
+
reason: {
|
|
87
|
+
description: string;
|
|
88
|
+
type: string;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
required: string[];
|
|
92
|
+
type: "object";
|
|
93
|
+
}>;
|
|
94
|
+
risk: "mutate";
|
|
95
|
+
version: "1.0.0";
|
|
96
|
+
}>;
|
|
97
|
+
//#endregion
|
|
98
|
+
export { MemoryBinding as a, MemoryScope as c, RIVUS_MEMORY_TOOL_PLUGIN_ID as d, RIVUS_MEMORY_TOOL_VERSION as f, restrictMemoryScopesForAudience as h, MEMORY_SCOPES as i, MemoryState as l, createRivusMemoryToolContract as m, AgentMemoryIdentity as n, MemoryInvocationAudience as o, createMemoryNamespace as p, AgentMemorySnapshot as r, MemoryRecord as s, AgentMemoryAuthority as t, RIVUS_MEMORY_TOOL_ID as u };
|
package/dist/cli.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { C as resolveNodeRivusPluginModulePath, D as resolveFeishuEndpointCredentials, O as loadMergedLocalEnvFile, T as loadRivusDeploymentManifest, j as validateRivusDeploymentManifest, t as runRivusDaemonCli } from "./rivus-daemon-cli.js";
|
|
3
|
+
import { Effect } from "effect";
|
|
3
4
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
5
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
|
-
import { Effect } from "effect";
|
|
6
6
|
import { lstat, mkdir, readFile, rmdir, stat, unlink, writeFile } from "node:fs/promises";
|
|
7
7
|
//#region src/infrastructure/project/rivus-project-doctor.ts
|
|
8
8
|
const REQUIRED_FILES = Object.freeze([
|
|
@@ -153,6 +153,7 @@ function isMissingPath$1(error) {
|
|
|
153
153
|
//#region src/infrastructure/project/rivus-project-initializer.ts
|
|
154
154
|
const TEMPLATE_FILES = Object.freeze({
|
|
155
155
|
"current-weather.mjs": "current-weather.mjs",
|
|
156
|
+
"https-response-reader.mjs": "https-response-reader.mjs",
|
|
156
157
|
"rivus-agents.plugin.mjs": "rivus-starter.plugin.mjs",
|
|
157
158
|
"rivus.bootstrap.ts": "pi-feishu-deployment.bootstrap.ts"
|
|
158
159
|
});
|
|
@@ -364,7 +365,10 @@ function environmentTemplate() {
|
|
|
364
365
|
}
|
|
365
366
|
function systemdService(directory, nodeExecutable) {
|
|
366
367
|
const cli = join(directory, "node_modules", "@rivus", "agent", "dist", "cli.js");
|
|
367
|
-
return `[Unit]\nDescription=Rivus Agent\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nType=simple\nWorkingDirectory=${
|
|
368
|
+
return `[Unit]\nDescription=Rivus Agent\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nType=simple\nWorkingDirectory=${systemdPath(directory)}\nExecStart=${systemdQuote(nodeExecutable)} ${systemdQuote(cli)} --env-file .env.local --bootstrap ./rivus.bootstrap.ts --manifest ./rivus.config.json\nRestart=on-failure\nRestartSec=5\nEnvironment=NODE_ENV=production\n\n[Install]\nWantedBy=default.target\n`;
|
|
369
|
+
}
|
|
370
|
+
function systemdPath(value) {
|
|
371
|
+
return value.replaceAll("%", "%%");
|
|
368
372
|
}
|
|
369
373
|
function systemdQuote(value) {
|
|
370
374
|
return `"${value.replace(/%/g, "%%").replace(/\$/g, () => "$$").replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|