@rivet-dev/flue 2.3.7
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 +203 -0
- package/README.md +26 -0
- package/dist/agent-coordinator.d.ts +88 -0
- package/dist/agent-coordinator.d.ts.map +1 -0
- package/dist/agent-coordinator.js +346 -0
- package/dist/agent-coordinator.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +3 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +2 -0
- package/dist/internal.js.map +1 -0
- package/dist/registry-ops-async.d.ts +7 -0
- package/dist/registry-ops-async.d.ts.map +1 -0
- package/dist/registry-ops-async.js +12 -0
- package/dist/registry-ops-async.js.map +1 -0
- package/dist/rivet-db.d.ts +7 -0
- package/dist/rivet-db.d.ts.map +1 -0
- package/dist/rivet-db.js +65 -0
- package/dist/rivet-db.js.map +1 -0
- package/dist/run-store-composite.d.ts +3 -0
- package/dist/run-store-composite.d.ts.map +1 -0
- package/dist/run-store-composite.js +43 -0
- package/dist/run-store-composite.js.map +1 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +7 -0
- package/dist/runtime.js.map +1 -0
- package/dist/stores/async-db.d.ts +10 -0
- package/dist/stores/async-db.d.ts.map +1 -0
- package/dist/stores/async-db.js +5 -0
- package/dist/stores/async-db.js.map +1 -0
- package/dist/stores/attachment-store.d.ts +4 -0
- package/dist/stores/attachment-store.d.ts.map +1 -0
- package/dist/stores/attachment-store.js +59 -0
- package/dist/stores/attachment-store.js.map +1 -0
- package/dist/stores/constants.d.ts +11 -0
- package/dist/stores/constants.d.ts.map +1 -0
- package/dist/stores/constants.js +11 -0
- package/dist/stores/constants.js.map +1 -0
- package/dist/stores/conversation-stream-store.d.ts +4 -0
- package/dist/stores/conversation-stream-store.d.ts.map +1 -0
- package/dist/stores/conversation-stream-store.js +16 -0
- package/dist/stores/conversation-stream-store.js.map +1 -0
- package/dist/stores/event-stream-store.d.ts +6 -0
- package/dist/stores/event-stream-store.d.ts.map +1 -0
- package/dist/stores/event-stream-store.js +171 -0
- package/dist/stores/event-stream-store.js.map +1 -0
- package/dist/stores/helpers.d.ts +2 -0
- package/dist/stores/helpers.d.ts.map +1 -0
- package/dist/stores/helpers.js +6 -0
- package/dist/stores/helpers.js.map +1 -0
- package/dist/stores/index.d.ts +11 -0
- package/dist/stores/index.d.ts.map +1 -0
- package/dist/stores/index.js +23 -0
- package/dist/stores/index.js.map +1 -0
- package/dist/stores/run-store.d.ts +4 -0
- package/dist/stores/run-store.d.ts.map +1 -0
- package/dist/stores/run-store.js +117 -0
- package/dist/stores/run-store.js.map +1 -0
- package/dist/stores/schema.d.ts +3 -0
- package/dist/stores/schema.d.ts.map +1 -0
- package/dist/stores/schema.js +228 -0
- package/dist/stores/schema.js.map +1 -0
- package/dist/stores/submission-store.d.ts +4 -0
- package/dist/stores/submission-store.d.ts.map +1 -0
- package/dist/stores/submission-store.js +261 -0
- package/dist/stores/submission-store.js.map +1 -0
- package/dist/target.d.ts +8 -0
- package/dist/target.d.ts.map +1 -0
- package/dist/target.js +408 -0
- package/dist/target.js.map +1 -0
- package/dist/workflow-coordinator.d.ts +71 -0
- package/dist/workflow-coordinator.d.ts.map +1 -0
- package/dist/workflow-coordinator.js +154 -0
- package/dist/workflow-coordinator.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2023 Rivet Gaming, Inc.
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @rivet-dev/flue
|
|
2
|
+
|
|
3
|
+
A [Flue](https://github.com/rivet-dev/flue) target that compiles agents and
|
|
4
|
+
workflows into a [Rivet](https://rivet.dev) Actor application. It provides
|
|
5
|
+
durable sessions, submission recovery, inter-actor `dispatch()`, and Flue
|
|
6
|
+
storage backed by each actor's native SQLite database (`c.db`).
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm add @rivet-dev/flue
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
// flue.config.ts
|
|
14
|
+
import { defineConfig } from "@flue/cli/config";
|
|
15
|
+
import { rivet } from "@rivet-dev/flue";
|
|
16
|
+
|
|
17
|
+
export default defineConfig({
|
|
18
|
+
target: rivet({ actors: "./actors.ts" }),
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Export a RivetKit registry from `actors.ts`. `flue build` then emits a server
|
|
23
|
+
entrypoint that maps every agent and workflow to a durable Rivet Actor.
|
|
24
|
+
|
|
25
|
+
Full documentation, setup, and configuration:
|
|
26
|
+
**https://rivet.dev/docs/integrations/flue**
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { AgentDefinition, AgentExecutionStore, AttachmentStore, ConversationStreamStore, DispatchInput, FlueContextInternal } from '@flue/runtime/adapter-kit';
|
|
2
|
+
import type { DispatchReceipt } from '@flue/runtime';
|
|
3
|
+
import { type AsyncSqlDb } from './stores/index.js';
|
|
4
|
+
export interface RivetAgentActorContext {
|
|
5
|
+
readonly actorId: string;
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly key: readonly string[];
|
|
8
|
+
readonly region: string;
|
|
9
|
+
readonly env?: Record<string, unknown>;
|
|
10
|
+
readonly request?: Request;
|
|
11
|
+
readonly abortSignal: AbortSignal;
|
|
12
|
+
readonly db: AsyncSqlDb;
|
|
13
|
+
readonly schedule?: {
|
|
14
|
+
after(duration: number, action: string, ...args: unknown[]): Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
keepAwake<T>(promise: Promise<T>): Promise<T>;
|
|
17
|
+
}
|
|
18
|
+
interface PreparedCoordinator {
|
|
19
|
+
readonly agentName: string;
|
|
20
|
+
readonly executionStore: AgentExecutionStore;
|
|
21
|
+
readonly conversationStreamStore: ConversationStreamStore;
|
|
22
|
+
readonly attachmentStore: AttachmentStore;
|
|
23
|
+
}
|
|
24
|
+
export interface RivetAgentRuntimeOptions {
|
|
25
|
+
readonly agents: ReadonlyArray<{
|
|
26
|
+
name: string;
|
|
27
|
+
definition: AgentDefinition;
|
|
28
|
+
}>;
|
|
29
|
+
readonly createContext: (options: {
|
|
30
|
+
readonly executionStore: AgentExecutionStore;
|
|
31
|
+
readonly actor: RivetAgentActorContext;
|
|
32
|
+
readonly agentName: string;
|
|
33
|
+
readonly request: Request;
|
|
34
|
+
readonly initialEventIndex?: number;
|
|
35
|
+
readonly dispatchId?: string;
|
|
36
|
+
}) => FlueContextInternal;
|
|
37
|
+
readonly resolveInstanceId?: (actor: RivetAgentActorContext) => string;
|
|
38
|
+
}
|
|
39
|
+
export interface RivetAgentRuntime {
|
|
40
|
+
prepare(options: {
|
|
41
|
+
db: AsyncSqlDb;
|
|
42
|
+
agentName: string;
|
|
43
|
+
}): Promise<PreparedCoordinator>;
|
|
44
|
+
attach(actor: RivetAgentActorContext, prepared: PreparedCoordinator): RivetAgentCoordinator;
|
|
45
|
+
onWake(actor: RivetAgentActorContext, inherited?: () => Promise<unknown> | unknown): Promise<void>;
|
|
46
|
+
wakeSubmissions(actor: RivetAgentActorContext): Promise<void>;
|
|
47
|
+
admitDispatch(actor: RivetAgentActorContext, input: DispatchInput): Promise<DispatchReceipt>;
|
|
48
|
+
onRequest(actor: RivetAgentActorContext, request: Request): Promise<Response | null>;
|
|
49
|
+
}
|
|
50
|
+
export declare function createRivetAgentRuntime(options: RivetAgentRuntimeOptions): RivetAgentRuntime;
|
|
51
|
+
export declare class RivetAgentCoordinator {
|
|
52
|
+
private readonly actor;
|
|
53
|
+
private readonly prepared;
|
|
54
|
+
private readonly options;
|
|
55
|
+
private readonly activeAttempts;
|
|
56
|
+
private conversationWriter;
|
|
57
|
+
private conversationWriterCreation;
|
|
58
|
+
private conversationMaterialization;
|
|
59
|
+
private activeControllers;
|
|
60
|
+
constructor(actor: RivetAgentActorContext, prepared: PreparedCoordinator, options: RivetAgentRuntimeOptions, activeAttempts: Set<string>);
|
|
61
|
+
onWake(inherited: () => Promise<unknown> | unknown): Promise<void>;
|
|
62
|
+
wakeSubmissions(): Promise<void>;
|
|
63
|
+
onRequest(request: Request): Promise<Response | null>;
|
|
64
|
+
private routeRequest;
|
|
65
|
+
private get agentName();
|
|
66
|
+
private get executionStore();
|
|
67
|
+
private get submissions();
|
|
68
|
+
private get instanceId();
|
|
69
|
+
private ensureConversationWriter;
|
|
70
|
+
private createContext;
|
|
71
|
+
private createDurableContext;
|
|
72
|
+
private armSubmissionWake;
|
|
73
|
+
private restoreSubmissionWake;
|
|
74
|
+
private reconcileSubmissions;
|
|
75
|
+
private resolveAgent;
|
|
76
|
+
private reconcileInterrupted;
|
|
77
|
+
private startSubmissionAttempt;
|
|
78
|
+
private processSubmissionEntry;
|
|
79
|
+
private startSubmissionReconciliation;
|
|
80
|
+
private materializeSubmissionConversation;
|
|
81
|
+
private admitAttachedSubmission;
|
|
82
|
+
admitDispatch(input: DispatchInput): Promise<DispatchReceipt>;
|
|
83
|
+
private abortInstance;
|
|
84
|
+
private attemptKey;
|
|
85
|
+
private logFailure;
|
|
86
|
+
}
|
|
87
|
+
export {};
|
|
88
|
+
//# sourceMappingURL=agent-coordinator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-coordinator.d.ts","sourceRoot":"","sources":["../src/agent-coordinator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,eAAe,E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,CAAC,UAAU;CAQlB"}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { ConversationRecordWriter, SubmissionAbortedError, agentStreamPath, agentSubmissionDispatchId, assertAgentDispatchAdmissionInput, createDirectAgentSubmissionInput, createDispatchAgentSubmissionInput, createSessionStorageKey, handleAgentAttachmentRead, handleAgentConversationHead, handleAgentConversationRead, handleAgentRequest, materializeAgentSubmissionSession, processSubmission, reconcileInterruptedSubmission, submissionSyntheticRequest, } from '@flue/runtime/adapter-kit';
|
|
2
|
+
import { createAsyncSqlStores, ensureAsyncSqlSchema, } from './stores/index.js';
|
|
3
|
+
const WAKE_ACTION = '__flueWakeAgentSubmissions';
|
|
4
|
+
const WAKE_SECONDS = 30;
|
|
5
|
+
const SUBMISSION_HARNESS_NAME = 'default';
|
|
6
|
+
const SUBMISSION_SESSION_NAME = 'default';
|
|
7
|
+
export function createRivetAgentRuntime(options) {
|
|
8
|
+
const coordinators = new WeakMap();
|
|
9
|
+
const activeAttempts = new Set();
|
|
10
|
+
const get = (actor) => {
|
|
11
|
+
const coordinator = coordinators.get(actor);
|
|
12
|
+
if (!coordinator)
|
|
13
|
+
throw new Error('[flue] Rivet agent coordinator was not initialized.');
|
|
14
|
+
return coordinator;
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
async prepare({ db, agentName }) {
|
|
18
|
+
await ensureAsyncSqlSchema(db);
|
|
19
|
+
const stores = createAsyncSqlStores(db);
|
|
20
|
+
return {
|
|
21
|
+
agentName,
|
|
22
|
+
executionStore: stores.executionStore,
|
|
23
|
+
conversationStreamStore: stores.conversationStreamStore,
|
|
24
|
+
attachmentStore: stores.attachmentStore,
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
attach(actor, prepared) {
|
|
28
|
+
const coordinator = new RivetAgentCoordinator(actor, prepared, options, activeAttempts);
|
|
29
|
+
coordinators.set(actor, coordinator);
|
|
30
|
+
return coordinator;
|
|
31
|
+
},
|
|
32
|
+
onWake: (actor, inherited = () => undefined) => get(actor).onWake(inherited),
|
|
33
|
+
wakeSubmissions: (actor) => get(actor).wakeSubmissions(),
|
|
34
|
+
admitDispatch: (actor, input) => get(actor).admitDispatch(input),
|
|
35
|
+
onRequest: (actor, request) => get(actor).onRequest(request),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export class RivetAgentCoordinator {
|
|
39
|
+
actor;
|
|
40
|
+
prepared;
|
|
41
|
+
options;
|
|
42
|
+
activeAttempts;
|
|
43
|
+
conversationWriter;
|
|
44
|
+
conversationWriterCreation;
|
|
45
|
+
conversationMaterialization = Promise.resolve();
|
|
46
|
+
activeControllers = new Map();
|
|
47
|
+
constructor(actor, prepared, options, activeAttempts) {
|
|
48
|
+
this.actor = actor;
|
|
49
|
+
this.prepared = prepared;
|
|
50
|
+
this.options = options;
|
|
51
|
+
this.activeAttempts = activeAttempts;
|
|
52
|
+
}
|
|
53
|
+
async onWake(inherited) {
|
|
54
|
+
await this.restoreSubmissionWake();
|
|
55
|
+
await inherited();
|
|
56
|
+
await this.reconcileSubmissions({ driverAlreadyArmed: true });
|
|
57
|
+
}
|
|
58
|
+
async wakeSubmissions() {
|
|
59
|
+
if (!(await this.submissions.hasUnsettledSubmissions()))
|
|
60
|
+
return;
|
|
61
|
+
await this.armSubmissionWake();
|
|
62
|
+
await this.reconcileSubmissions({ driverAlreadyArmed: true });
|
|
63
|
+
}
|
|
64
|
+
onRequest(request) {
|
|
65
|
+
return this.routeRequest(request);
|
|
66
|
+
}
|
|
67
|
+
async routeRequest(request) {
|
|
68
|
+
if (isAbortRequest(request, this.agentName, this.instanceId)) {
|
|
69
|
+
return Response.json({ aborted: await this.abortInstance() });
|
|
70
|
+
}
|
|
71
|
+
if (request.method === 'GET' || request.method === 'HEAD') {
|
|
72
|
+
const path = agentStreamPath(this.agentName, this.instanceId);
|
|
73
|
+
const segments = new URL(request.url).pathname.split('/');
|
|
74
|
+
const attachmentId = request.method === 'GET' &&
|
|
75
|
+
segments.at(-2) === 'attachments' &&
|
|
76
|
+
decodeURIComponent(segments.at(-3) ?? '') === this.instanceId &&
|
|
77
|
+
decodeURIComponent(segments.at(-4) ?? '') === this.agentName
|
|
78
|
+
? decodeURIComponent(segments.at(-1) ?? '') : undefined;
|
|
79
|
+
if (attachmentId)
|
|
80
|
+
return handleAgentAttachmentRead({
|
|
81
|
+
conversationStore: this.prepared.conversationStreamStore,
|
|
82
|
+
attachmentStore: this.prepared.attachmentStore,
|
|
83
|
+
path,
|
|
84
|
+
attachmentId,
|
|
85
|
+
});
|
|
86
|
+
if (request.method === 'HEAD') {
|
|
87
|
+
return handleAgentConversationHead(this.prepared.conversationStreamStore, path);
|
|
88
|
+
}
|
|
89
|
+
return handleAgentConversationRead({
|
|
90
|
+
store: this.prepared.conversationStreamStore,
|
|
91
|
+
path,
|
|
92
|
+
request,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return handleAgentRequest({
|
|
96
|
+
request,
|
|
97
|
+
id: this.instanceId,
|
|
98
|
+
agentName: this.agentName,
|
|
99
|
+
admitAttachedSubmission: (message, traceCarrier) => this.admitAttachedSubmission(message, traceCarrier),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
get agentName() { return this.prepared.agentName; }
|
|
103
|
+
get executionStore() { return this.prepared.executionStore; }
|
|
104
|
+
get submissions() { return this.executionStore.submissions; }
|
|
105
|
+
get instanceId() {
|
|
106
|
+
return this.options.resolveInstanceId?.(this.actor) ?? this.actor.key[0] ?? this.actor.actorId;
|
|
107
|
+
}
|
|
108
|
+
async ensureConversationWriter() {
|
|
109
|
+
if (this.conversationWriter && !this.conversationWriter.failed)
|
|
110
|
+
return this.conversationWriter;
|
|
111
|
+
if (!this.conversationWriterCreation) {
|
|
112
|
+
const creation = ConversationRecordWriter.create({
|
|
113
|
+
store: this.prepared.conversationStreamStore,
|
|
114
|
+
path: agentStreamPath(this.agentName, this.instanceId),
|
|
115
|
+
identity: { agentName: this.agentName, instanceId: this.instanceId },
|
|
116
|
+
producerId: this.actor.actorId,
|
|
117
|
+
onFailed: (writer) => {
|
|
118
|
+
if (this.conversationWriter === writer)
|
|
119
|
+
this.conversationWriter = undefined;
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
this.conversationWriterCreation = creation;
|
|
123
|
+
void creation.then((writer) => {
|
|
124
|
+
if (!writer.failed)
|
|
125
|
+
this.conversationWriter = writer;
|
|
126
|
+
if (this.conversationWriterCreation === creation)
|
|
127
|
+
this.conversationWriterCreation = undefined;
|
|
128
|
+
}, () => {
|
|
129
|
+
if (this.conversationWriterCreation === creation)
|
|
130
|
+
this.conversationWriterCreation = undefined;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return this.conversationWriterCreation;
|
|
134
|
+
}
|
|
135
|
+
createContext(request, dispatchId) {
|
|
136
|
+
return this.options.createContext({
|
|
137
|
+
executionStore: this.executionStore,
|
|
138
|
+
actor: this.actor,
|
|
139
|
+
agentName: this.agentName,
|
|
140
|
+
request,
|
|
141
|
+
dispatchId,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
createDurableContext(request, dispatchId) {
|
|
145
|
+
const ctx = this.createContext(request, dispatchId);
|
|
146
|
+
ctx.setConversationWriter?.(this.conversationWriter);
|
|
147
|
+
ctx.setAttachmentStore?.(this.prepared.attachmentStore);
|
|
148
|
+
return ctx;
|
|
149
|
+
}
|
|
150
|
+
armSubmissionWake() {
|
|
151
|
+
return this.actor.schedule?.after(WAKE_SECONDS, WAKE_ACTION) ?? Promise.resolve();
|
|
152
|
+
}
|
|
153
|
+
async restoreSubmissionWake() {
|
|
154
|
+
if (!(await this.submissions.hasUnsettledSubmissions()))
|
|
155
|
+
return false;
|
|
156
|
+
await this.armSubmissionWake();
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
async reconcileSubmissions(options = {}) {
|
|
160
|
+
if (!(await this.submissions.hasUnsettledSubmissions()))
|
|
161
|
+
return false;
|
|
162
|
+
if (!options.driverAlreadyArmed)
|
|
163
|
+
await this.restoreSubmissionWake();
|
|
164
|
+
for (const submission of await this.submissions.listUnreadySubmissions()) {
|
|
165
|
+
const agent = this.resolveAgent(submission.input.agent);
|
|
166
|
+
try {
|
|
167
|
+
await this.materializeSubmissionConversation(submission.input, agent);
|
|
168
|
+
await this.submissions.markSubmissionCanonicalReady(submission.submissionId);
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
this.logFailure(submission, 'materialize_submission', error);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
for (const settlement of await this.submissions.listPendingSubmissionSettlements()) {
|
|
175
|
+
const submission = await this.submissions.getSubmission(settlement.submissionId);
|
|
176
|
+
if (!submission || this.activeAttempts.has(this.attemptKey(submission)))
|
|
177
|
+
continue;
|
|
178
|
+
const writer = await this.ensureConversationWriter();
|
|
179
|
+
const attempt = { submissionId: settlement.submissionId, attemptId: settlement.attemptId };
|
|
180
|
+
const canonical = await writer.getRecord(settlement.recordId);
|
|
181
|
+
if (!canonical)
|
|
182
|
+
await writer.append([settlement.record], { submission: attempt });
|
|
183
|
+
else if (JSON.stringify(canonical) !== JSON.stringify(settlement.record)) {
|
|
184
|
+
throw new Error('[flue] Pending settlement conflicts with its canonical record.');
|
|
185
|
+
}
|
|
186
|
+
await this.submissions.finalizeSubmissionSettlement(attempt, settlement.recordId);
|
|
187
|
+
}
|
|
188
|
+
for (const submission of await this.submissions.listRunningSubmissions()) {
|
|
189
|
+
if (this.activeAttempts.has(this.attemptKey(submission)))
|
|
190
|
+
continue;
|
|
191
|
+
try {
|
|
192
|
+
await this.reconcileInterrupted(submission);
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
this.logFailure(submission, 'reconcile_submission', error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
for (const submission of await this.submissions.listRunnableSubmissions()) {
|
|
199
|
+
const claimed = await this.submissions.claimSubmission({
|
|
200
|
+
submissionId: submission.submissionId,
|
|
201
|
+
attemptId: crypto.randomUUID(),
|
|
202
|
+
ownerId: this.actor.actorId,
|
|
203
|
+
leaseExpiresAt: 0,
|
|
204
|
+
});
|
|
205
|
+
if (claimed)
|
|
206
|
+
this.startSubmissionAttempt(claimed);
|
|
207
|
+
}
|
|
208
|
+
return this.submissions.hasUnsettledSubmissions();
|
|
209
|
+
}
|
|
210
|
+
resolveAgent(name) {
|
|
211
|
+
const agent = this.options.agents.find((record) => record.name === name)?.definition;
|
|
212
|
+
if (!agent)
|
|
213
|
+
throw new Error(`[flue] Agent target "${name}" is unavailable.`);
|
|
214
|
+
return agent;
|
|
215
|
+
}
|
|
216
|
+
async reconcileInterrupted(submission) {
|
|
217
|
+
const writer = await this.ensureConversationWriter();
|
|
218
|
+
const replacement = await reconcileInterruptedSubmission(this.submissions, submission, this.resolveAgent(this.agentName), (dispatchId) => this.createDurableContext(submissionSyntheticRequest(submission.input), dispatchId), { ownerId: this.actor.actorId, leaseExpiresAt: 0 }, writer);
|
|
219
|
+
if (replacement)
|
|
220
|
+
this.startSubmissionAttempt(replacement);
|
|
221
|
+
}
|
|
222
|
+
startSubmissionAttempt(submission) {
|
|
223
|
+
if (submission.status !== 'running' || !submission.attemptId)
|
|
224
|
+
return;
|
|
225
|
+
const key = this.attemptKey(submission);
|
|
226
|
+
if (this.activeAttempts.has(key))
|
|
227
|
+
return;
|
|
228
|
+
this.activeAttempts.add(key);
|
|
229
|
+
const controller = new AbortController();
|
|
230
|
+
this.activeControllers.set(submission.submissionId, controller);
|
|
231
|
+
const attempt = { submissionId: submission.submissionId, attemptId: submission.attemptId };
|
|
232
|
+
const running = Promise.resolve().then(async () => {
|
|
233
|
+
await this.submissions.insertAttemptMarker(attempt);
|
|
234
|
+
await this.processSubmissionEntry(submission, controller.signal);
|
|
235
|
+
});
|
|
236
|
+
void this.actor.keepAwake(running).catch((error) => this.logFailure(submission, 'process_submission', error))
|
|
237
|
+
.finally(() => {
|
|
238
|
+
this.activeAttempts.delete(key);
|
|
239
|
+
this.activeControllers.delete(submission.submissionId);
|
|
240
|
+
void this.submissions.deleteAttemptMarker(attempt).catch(() => { });
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
async processSubmissionEntry(submission, signal) {
|
|
244
|
+
const writer = await this.ensureConversationWriter();
|
|
245
|
+
await processSubmission({
|
|
246
|
+
submissions: this.submissions,
|
|
247
|
+
submission,
|
|
248
|
+
resolveAgent: (name) => this.resolveAgent(name),
|
|
249
|
+
createContext: (dispatchId) => this.createDurableContext(submissionSyntheticRequest(submission.input), dispatchId),
|
|
250
|
+
conversationWriter: writer,
|
|
251
|
+
signal,
|
|
252
|
+
onSettled: () => { this.startSubmissionReconciliation(); },
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
startSubmissionReconciliation() {
|
|
256
|
+
const running = Promise.resolve().then(() => this.reconcileSubmissions({ driverAlreadyArmed: true }));
|
|
257
|
+
void this.actor.keepAwake(running).catch((error) => {
|
|
258
|
+
console.error('[flue:submission-reconciliation]', {
|
|
259
|
+
agentName: this.agentName,
|
|
260
|
+
instanceId: this.instanceId,
|
|
261
|
+
}, error);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
materializeSubmissionConversation(input, agent) {
|
|
265
|
+
const operation = this.conversationMaterialization.then(async () => {
|
|
266
|
+
await this.ensureConversationWriter();
|
|
267
|
+
const ctx = this.createDurableContext(submissionSyntheticRequest(input), agentSubmissionDispatchId(input));
|
|
268
|
+
await materializeAgentSubmissionSession(ctx, agent, input, this.prepared.attachmentStore);
|
|
269
|
+
});
|
|
270
|
+
this.conversationMaterialization = operation.catch(() => { });
|
|
271
|
+
return operation;
|
|
272
|
+
}
|
|
273
|
+
async admitAttachedSubmission(message, traceCarrier) {
|
|
274
|
+
const input = createDirectAgentSubmissionInput({
|
|
275
|
+
agent: this.agentName,
|
|
276
|
+
id: this.instanceId,
|
|
277
|
+
message,
|
|
278
|
+
traceCarrier,
|
|
279
|
+
});
|
|
280
|
+
const admitted = await this.submissions.admitDirect(input);
|
|
281
|
+
if (admitted.canonicalReadyAt === null) {
|
|
282
|
+
await this.materializeSubmissionConversation(input, this.resolveAgent(this.agentName));
|
|
283
|
+
await this.submissions.markSubmissionCanonicalReady(input.submissionId);
|
|
284
|
+
}
|
|
285
|
+
const offset = (await this.ensureConversationWriter()).offset;
|
|
286
|
+
await this.armSubmissionWake();
|
|
287
|
+
this.startSubmissionReconciliation();
|
|
288
|
+
return { submissionId: input.submissionId, offset };
|
|
289
|
+
}
|
|
290
|
+
async admitDispatch(input) {
|
|
291
|
+
assertAgentDispatchAdmissionInput(input);
|
|
292
|
+
if (input.agent !== this.agentName || input.id !== this.instanceId) {
|
|
293
|
+
throw new Error('[flue] Invalid dispatch target.');
|
|
294
|
+
}
|
|
295
|
+
const agent = this.resolveAgent(this.agentName);
|
|
296
|
+
const admission = await this.submissions.admitDispatch(input);
|
|
297
|
+
if (admission.kind === 'retained_receipt')
|
|
298
|
+
return {
|
|
299
|
+
dispatchId: admission.receipt.submissionId,
|
|
300
|
+
acceptedAt: new Date(admission.receipt.acceptedAt).toISOString(),
|
|
301
|
+
};
|
|
302
|
+
if (admission.kind === 'conflict') {
|
|
303
|
+
throw new Error('[flue] Conflicting dispatch replay.');
|
|
304
|
+
}
|
|
305
|
+
if (admission.submission.canonicalReadyAt === null) {
|
|
306
|
+
await this.materializeSubmissionConversation(createDispatchAgentSubmissionInput(input), agent);
|
|
307
|
+
if (!(await this.submissions.markSubmissionCanonicalReady(input.dispatchId))) {
|
|
308
|
+
throw new Error('[flue] Dispatch disappeared before canonical readiness.');
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
await this.armSubmissionWake();
|
|
312
|
+
this.startSubmissionReconciliation();
|
|
313
|
+
return { dispatchId: admission.submission.submissionId, acceptedAt: input.acceptedAt };
|
|
314
|
+
}
|
|
315
|
+
async abortInstance() {
|
|
316
|
+
const sessionKey = createSessionStorageKey(this.instanceId, SUBMISSION_HARNESS_NAME, SUBMISSION_SESSION_NAME);
|
|
317
|
+
const affected = await this.submissions.requestSessionAbort(sessionKey);
|
|
318
|
+
for (const id of affected)
|
|
319
|
+
this.activeControllers.get(id)?.abort(new SubmissionAbortedError());
|
|
320
|
+
if (affected.length > 0) {
|
|
321
|
+
await this.armSubmissionWake();
|
|
322
|
+
this.startSubmissionReconciliation();
|
|
323
|
+
}
|
|
324
|
+
return affected.length > 0;
|
|
325
|
+
}
|
|
326
|
+
attemptKey(submission) {
|
|
327
|
+
return `${this.actor.actorId}:${submission.attemptId}`;
|
|
328
|
+
}
|
|
329
|
+
logFailure(submission, operation, error) {
|
|
330
|
+
console.error('[flue:submission]', {
|
|
331
|
+
agentName: this.agentName,
|
|
332
|
+
instanceId: this.instanceId,
|
|
333
|
+
submissionId: submission.submissionId,
|
|
334
|
+
operation,
|
|
335
|
+
}, error);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function isAbortRequest(request, agentName, instanceId) {
|
|
339
|
+
if (request.method !== 'POST')
|
|
340
|
+
return false;
|
|
341
|
+
const segments = new URL(request.url).pathname.split('/');
|
|
342
|
+
return segments.at(-1) === 'abort' &&
|
|
343
|
+
decodeURIComponent(segments.at(-2) ?? '') === instanceId &&
|
|
344
|
+
decodeURIComponent(segments.at(-3) ?? '') === agentName;
|
|
345
|
+
}
|
|
346
|
+
//# sourceMappingURL=agent-coordinator.js.map
|