@vantageos/agent-engine 0.3.0-alpha.0 → 0.4.0-alpha.0
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 +1 -1
- package/dist/component/_generated/api.d.ts +26 -2
- package/dist/component/_generated/component.d.ts +20 -1
- package/dist/component/_generated/dataModel.d.ts +38 -0
- package/dist/component/_generated/server.d.ts +106 -0
- package/dist/component/engine/agents.d.ts +3 -3
- package/dist/component/engine/awaitEventTimeout.d.ts +6 -5
- package/dist/component/engine/awaitEventTimeout.js +50 -33
- package/dist/component/engine/delegation.d.ts +6 -6
- package/dist/component/engine/durableJob.d.ts +4 -27
- package/dist/component/engine/durableJob.js +14 -217
- package/dist/component/engine/mandates.d.ts +2 -2
- package/dist/component/engine/resolvers.js +1 -1
- package/dist/component/engine/sessions.d.ts +2 -2
- package/dist/component/engine/sessions.js +10 -38
- package/dist/component/engine/streams.d.ts +1 -1
- package/dist/component/engine/wakeup.d.ts +2 -2
- package/dist/component/engine/workflows.d.ts +3 -27
- package/dist/component/engine/workflows.js +2 -66
- package/dist/component/engine/world.d.ts +28 -30
- package/dist/component/engine/world.js +79 -42
- package/dist/component/schema.d.ts +31 -132
- package/dist/component/schema.js +3 -59
- package/package.json +6 -5
|
@@ -3,10 +3,10 @@ import { WorkflowManager } from "@convex-dev/workflow";
|
|
|
3
3
|
import { components, internal } from "../_generated/api.js";
|
|
4
4
|
import { internalMutation, internalQuery, mutation, query, } from "../_generated/server.js";
|
|
5
5
|
import { requireOrgId } from "../lib/auth.js";
|
|
6
|
-
import { appendEventCore,
|
|
6
|
+
import { appendEventCore, bindWorkflowToRun, loadRun, openRun, workflowOfRun, } from "./world.js";
|
|
7
7
|
const CURRENT_WORKFLOW_VERSION = "v2";
|
|
8
8
|
async function readJob(ctx, orgId, runId, notFoundMessage = "Job not found") {
|
|
9
|
-
const run = await loadRun(ctx, orgId, runId, "
|
|
9
|
+
const run = await loadRun(ctx, orgId, runId, "job", notFoundMessage);
|
|
10
10
|
const num = (field) => {
|
|
11
11
|
const value = run.created[field];
|
|
12
12
|
if (typeof value !== "number") {
|
|
@@ -55,23 +55,9 @@ export const start = mutation({
|
|
|
55
55
|
if (args.stepFunctionHandle.length === 0) {
|
|
56
56
|
throw new Error("Validation: stepFunctionHandle must not be empty");
|
|
57
57
|
}
|
|
58
|
-
const
|
|
59
|
-
const legacyJobId = await ctx.db.insert("durableJobs", {
|
|
58
|
+
const runId = await openRun(ctx, {
|
|
60
59
|
orgId,
|
|
61
|
-
kind:
|
|
62
|
-
stepFunctionHandle: args.stepFunctionHandle,
|
|
63
|
-
totalSteps: args.totalSteps,
|
|
64
|
-
maxAttemptsPerStep: args.maxAttemptsPerStep,
|
|
65
|
-
cursor: 0,
|
|
66
|
-
status: "running",
|
|
67
|
-
workflowVersion: CURRENT_WORKFLOW_VERSION,
|
|
68
|
-
createdAt: now,
|
|
69
|
-
updatedAt: now,
|
|
70
|
-
});
|
|
71
|
-
const runId = await openLegacyRun(ctx, {
|
|
72
|
-
orgId,
|
|
73
|
-
table: "durableJobs",
|
|
74
|
-
legacyId: legacyJobId,
|
|
60
|
+
kind: "job",
|
|
75
61
|
data: {
|
|
76
62
|
kind: args.kind,
|
|
77
63
|
stepFunctionHandle: args.stepFunctionHandle,
|
|
@@ -81,7 +67,12 @@ export const start = mutation({
|
|
|
81
67
|
},
|
|
82
68
|
});
|
|
83
69
|
const workflowId = await workflow.start(ctx, internal.engine.durableJob.durableJobWorkflowV2, { orgId, runId });
|
|
84
|
-
await ctx
|
|
70
|
+
await bindWorkflowToRun(ctx, {
|
|
71
|
+
orgId,
|
|
72
|
+
runId,
|
|
73
|
+
workflowId,
|
|
74
|
+
workflowVersion: CURRENT_WORKFLOW_VERSION,
|
|
75
|
+
});
|
|
85
76
|
return runId;
|
|
86
77
|
},
|
|
87
78
|
});
|
|
@@ -133,7 +124,7 @@ export const cancel = mutation({
|
|
|
133
124
|
returns: v.null(),
|
|
134
125
|
handler: async (ctx, args) => {
|
|
135
126
|
const orgId = requireOrgId(args.orgId);
|
|
136
|
-
const run = await loadRun(ctx, orgId, args.jobId, "
|
|
127
|
+
const run = await loadRun(ctx, orgId, args.jobId, "job", "Job not found");
|
|
137
128
|
if (run.status !== "running")
|
|
138
129
|
return null;
|
|
139
130
|
await appendEventCore(ctx, {
|
|
@@ -142,184 +133,10 @@ export const cancel = mutation({
|
|
|
142
133
|
eventType: "run_cancelled",
|
|
143
134
|
dataJson: "{}",
|
|
144
135
|
});
|
|
145
|
-
const
|
|
146
|
-
if (
|
|
147
|
-
await
|
|
148
|
-
status: "cancelled",
|
|
149
|
-
updatedAt: Date.now(),
|
|
150
|
-
});
|
|
151
|
-
if (legacy.workflowId) {
|
|
152
|
-
await workflow.cancel(ctx, legacy.workflowId);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return null;
|
|
156
|
-
},
|
|
157
|
-
});
|
|
158
|
-
export const getJobInternal = internalQuery({
|
|
159
|
-
args: { jobId: v.id("durableJobs") },
|
|
160
|
-
returns: v.union(v.object({
|
|
161
|
-
cursor: v.number(),
|
|
162
|
-
totalSteps: v.number(),
|
|
163
|
-
maxAttemptsPerStep: v.number(),
|
|
164
|
-
status: v.union(v.literal("running"), v.literal("completed"), v.literal("abandoned"), v.literal("cancelled")),
|
|
165
|
-
}), v.null()),
|
|
166
|
-
handler: async (ctx, args) => {
|
|
167
|
-
const job = await ctx.db.get(args.jobId);
|
|
168
|
-
if (!job)
|
|
169
|
-
return null;
|
|
170
|
-
return {
|
|
171
|
-
cursor: job.cursor,
|
|
172
|
-
totalSteps: job.totalSteps,
|
|
173
|
-
maxAttemptsPerStep: job.maxAttemptsPerStep,
|
|
174
|
-
status: job.status,
|
|
175
|
-
};
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
export const runStepInternal = internalMutation({
|
|
179
|
-
args: { jobId: v.id("durableJobs"), stepIndex: v.number() },
|
|
180
|
-
returns: v.null(),
|
|
181
|
-
handler: async (ctx, args) => {
|
|
182
|
-
const job = await ctx.db.get(args.jobId);
|
|
183
|
-
if (!job)
|
|
184
|
-
throw new Error("Job not found");
|
|
185
|
-
if (job.status === "cancelled")
|
|
186
|
-
return null;
|
|
187
|
-
if (args.stepIndex < job.cursor) {
|
|
188
|
-
return null;
|
|
189
|
-
}
|
|
190
|
-
const stepId = String(args.stepIndex);
|
|
191
|
-
await mirrorToRun(ctx, {
|
|
192
|
-
orgId: job.orgId,
|
|
193
|
-
table: "durableJobs",
|
|
194
|
-
legacyId: args.jobId,
|
|
195
|
-
eventType: "step_created",
|
|
196
|
-
stepId,
|
|
197
|
-
});
|
|
198
|
-
await mirrorToRun(ctx, {
|
|
199
|
-
orgId: job.orgId,
|
|
200
|
-
table: "durableJobs",
|
|
201
|
-
legacyId: args.jobId,
|
|
202
|
-
eventType: "step_started",
|
|
203
|
-
stepId,
|
|
204
|
-
});
|
|
205
|
-
let lastError;
|
|
206
|
-
for (let attempt = 1; attempt <= job.maxAttemptsPerStep; attempt++) {
|
|
207
|
-
try {
|
|
208
|
-
await ctx.runMutation(job.stepFunctionHandle, { orgId: job.orgId, jobId: args.jobId, stepIndex: args.stepIndex });
|
|
209
|
-
await ctx.db.patch(args.jobId, {
|
|
210
|
-
cursor: args.stepIndex + 1,
|
|
211
|
-
updatedAt: Date.now(),
|
|
212
|
-
});
|
|
213
|
-
await mirrorToRun(ctx, {
|
|
214
|
-
orgId: job.orgId,
|
|
215
|
-
table: "durableJobs",
|
|
216
|
-
legacyId: args.jobId,
|
|
217
|
-
eventType: "step_completed",
|
|
218
|
-
stepId,
|
|
219
|
-
dataJson: JSON.stringify({ attempt }),
|
|
220
|
-
});
|
|
221
|
-
return null;
|
|
222
|
-
}
|
|
223
|
-
catch (err) {
|
|
224
|
-
lastError = err;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
throw lastError instanceof Error
|
|
228
|
-
? lastError
|
|
229
|
-
: new Error(String(lastError));
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
export const markAbandonedInternal = internalMutation({
|
|
233
|
-
args: { jobId: v.id("durableJobs"), stepIndex: v.number(), error: v.string() },
|
|
234
|
-
returns: v.null(),
|
|
235
|
-
handler: async (ctx, args) => {
|
|
236
|
-
const job = await ctx.db.get(args.jobId);
|
|
237
|
-
if (!job || job.status !== "running")
|
|
238
|
-
return null;
|
|
239
|
-
await ctx.db.patch(args.jobId, {
|
|
240
|
-
status: "abandoned",
|
|
241
|
-
abandonedAtStep: args.stepIndex,
|
|
242
|
-
lastError: args.error,
|
|
243
|
-
updatedAt: Date.now(),
|
|
244
|
-
});
|
|
245
|
-
const stepId = String(args.stepIndex);
|
|
246
|
-
await mirrorToRun(ctx, {
|
|
247
|
-
orgId: job.orgId,
|
|
248
|
-
table: "durableJobs",
|
|
249
|
-
legacyId: args.jobId,
|
|
250
|
-
eventType: "step_created",
|
|
251
|
-
stepId,
|
|
252
|
-
});
|
|
253
|
-
await mirrorToRun(ctx, {
|
|
254
|
-
orgId: job.orgId,
|
|
255
|
-
table: "durableJobs",
|
|
256
|
-
legacyId: args.jobId,
|
|
257
|
-
eventType: "step_failed",
|
|
258
|
-
stepId,
|
|
259
|
-
dataJson: JSON.stringify({ error: args.error }),
|
|
260
|
-
});
|
|
261
|
-
await mirrorToRun(ctx, {
|
|
262
|
-
orgId: job.orgId,
|
|
263
|
-
table: "durableJobs",
|
|
264
|
-
legacyId: args.jobId,
|
|
265
|
-
eventType: "run_failed",
|
|
266
|
-
dataJson: JSON.stringify({
|
|
267
|
-
reason: "abandoned",
|
|
268
|
-
abandonedAtStep: args.stepIndex,
|
|
269
|
-
lastError: args.error,
|
|
270
|
-
}),
|
|
271
|
-
});
|
|
272
|
-
return null;
|
|
273
|
-
},
|
|
274
|
-
});
|
|
275
|
-
export const markCompletedInternal = internalMutation({
|
|
276
|
-
args: { jobId: v.id("durableJobs") },
|
|
277
|
-
returns: v.null(),
|
|
278
|
-
handler: async (ctx, args) => {
|
|
279
|
-
const job = await ctx.db.get(args.jobId);
|
|
280
|
-
if (!job || job.status !== "running")
|
|
281
|
-
return null;
|
|
282
|
-
await ctx.db.patch(args.jobId, {
|
|
283
|
-
status: "completed",
|
|
284
|
-
updatedAt: Date.now(),
|
|
285
|
-
});
|
|
286
|
-
await mirrorToRun(ctx, {
|
|
287
|
-
orgId: job.orgId,
|
|
288
|
-
table: "durableJobs",
|
|
289
|
-
legacyId: args.jobId,
|
|
290
|
-
eventType: "run_completed",
|
|
291
|
-
});
|
|
292
|
-
return null;
|
|
293
|
-
},
|
|
294
|
-
});
|
|
295
|
-
export const durableJobWorkflowV1 = workflow.define({
|
|
296
|
-
args: { jobId: v.id("durableJobs") },
|
|
297
|
-
returns: v.null(),
|
|
298
|
-
handler: async (step, args) => {
|
|
299
|
-
const job = await step.runQuery(internal.engine.durableJob.getJobInternal, {
|
|
300
|
-
jobId: args.jobId,
|
|
301
|
-
});
|
|
302
|
-
if (!job || job.status !== "running")
|
|
303
|
-
return null;
|
|
304
|
-
for (let i = job.cursor; i < job.totalSteps; i++) {
|
|
305
|
-
try {
|
|
306
|
-
await step.runMutation(internal.engine.durableJob.runStepInternal, {
|
|
307
|
-
jobId: args.jobId,
|
|
308
|
-
stepIndex: i,
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
catch (err) {
|
|
312
|
-
await step.runMutation(internal.engine.durableJob.markAbandonedInternal, {
|
|
313
|
-
jobId: args.jobId,
|
|
314
|
-
stepIndex: i,
|
|
315
|
-
error: err instanceof Error ? err.message : String(err),
|
|
316
|
-
});
|
|
317
|
-
return null;
|
|
318
|
-
}
|
|
136
|
+
const bound = await workflowOfRun(ctx, orgId, run.runId);
|
|
137
|
+
if (bound !== null) {
|
|
138
|
+
await workflow.cancel(ctx, bound.workflowId);
|
|
319
139
|
}
|
|
320
|
-
await step.runMutation(internal.engine.durableJob.markCompletedInternal, {
|
|
321
|
-
jobId: args.jobId,
|
|
322
|
-
});
|
|
323
140
|
return null;
|
|
324
141
|
},
|
|
325
142
|
});
|
|
@@ -379,13 +196,6 @@ export const runStepInternalV2 = internalMutation({
|
|
|
379
196
|
for (let attempt = 1; attempt <= job.maxAttemptsPerStep; attempt++) {
|
|
380
197
|
try {
|
|
381
198
|
await ctx.runMutation(job.stepFunctionHandle, { orgId: args.orgId, jobId: args.runId, stepIndex: args.stepIndex });
|
|
382
|
-
const legacy = await legacyJobOf(ctx, job.run, args.orgId);
|
|
383
|
-
if (legacy !== null) {
|
|
384
|
-
await ctx.db.patch(legacy._id, {
|
|
385
|
-
cursor: args.stepIndex + 1,
|
|
386
|
-
updatedAt: Date.now(),
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
199
|
await appendEventCore(ctx, {
|
|
390
200
|
orgId: args.orgId,
|
|
391
201
|
runId: args.runId,
|
|
@@ -416,15 +226,6 @@ export const markAbandonedInternalV2 = internalMutation({
|
|
|
416
226
|
const job = await readJob(ctx, args.orgId, args.runId);
|
|
417
227
|
if (job.run.status !== "running")
|
|
418
228
|
return null;
|
|
419
|
-
const legacy = await legacyJobOf(ctx, job.run, args.orgId);
|
|
420
|
-
if (legacy !== null) {
|
|
421
|
-
await ctx.db.patch(legacy._id, {
|
|
422
|
-
status: "abandoned",
|
|
423
|
-
abandonedAtStep: args.stepIndex,
|
|
424
|
-
lastError: args.error,
|
|
425
|
-
updatedAt: Date.now(),
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
229
|
const stepId = String(args.stepIndex);
|
|
429
230
|
await appendEventCore(ctx, {
|
|
430
231
|
orgId: args.orgId,
|
|
@@ -460,10 +261,6 @@ export const markCompletedInternalV2 = internalMutation({
|
|
|
460
261
|
const job = await readJob(ctx, args.orgId, args.runId);
|
|
461
262
|
if (job.run.status !== "running")
|
|
462
263
|
return null;
|
|
463
|
-
const legacy = await legacyJobOf(ctx, job.run, args.orgId);
|
|
464
|
-
if (legacy !== null) {
|
|
465
|
-
await ctx.db.patch(legacy._id, { status: "completed", updatedAt: Date.now() });
|
|
466
|
-
}
|
|
467
264
|
await appendEventCore(ctx, {
|
|
468
265
|
orgId: args.orgId,
|
|
469
266
|
runId: args.runId,
|
|
@@ -2,11 +2,11 @@ import type { Id } from "../_generated/dataModel.js";
|
|
|
2
2
|
export declare const set: import("convex/server").RegisteredMutation<"public", {
|
|
3
3
|
capacity?: number | undefined;
|
|
4
4
|
orgId: string;
|
|
5
|
-
kind: "token bucket" | "fixed window";
|
|
6
5
|
agentId: import("convex/values").GenericId<"agents">;
|
|
7
6
|
scope: string;
|
|
8
7
|
rate: number;
|
|
9
8
|
period: number;
|
|
9
|
+
kind: "token bucket" | "fixed window";
|
|
10
10
|
}, Promise<import("convex/values").GenericId<"mandates">>>;
|
|
11
11
|
export declare const get: import("convex/server").RegisteredQuery<"public", {
|
|
12
12
|
orgId: string;
|
|
@@ -19,11 +19,11 @@ export declare const get: import("convex/server").RegisteredQuery<"public", {
|
|
|
19
19
|
orgId: string;
|
|
20
20
|
createdAt: number;
|
|
21
21
|
updatedAt: number;
|
|
22
|
-
kind: "token bucket" | "fixed window";
|
|
23
22
|
agentId: import("convex/values").GenericId<"agents">;
|
|
24
23
|
scope: string;
|
|
25
24
|
rate: number;
|
|
26
25
|
period: number;
|
|
26
|
+
kind: "token bucket" | "fixed window";
|
|
27
27
|
} | null>>;
|
|
28
28
|
export declare const spend: import("convex/server").RegisteredMutation<"public", {
|
|
29
29
|
orgId: string;
|
|
@@ -16,7 +16,7 @@ export const resolveManifest = query({
|
|
|
16
16
|
}),
|
|
17
17
|
handler: async (ctx, args) => {
|
|
18
18
|
const orgId = requireOrgId(args.orgId);
|
|
19
|
-
const run = await loadRun(ctx, orgId, args.sessionId, "
|
|
19
|
+
const run = await loadRun(ctx, orgId, args.sessionId, "session", "Session not found");
|
|
20
20
|
const agentId = agentIdOfRun(ctx, run);
|
|
21
21
|
const agent = await ctx.db.get(agentId);
|
|
22
22
|
assertSameOrg(orgId, agent, "Agent not found");
|
|
@@ -32,8 +32,8 @@ export declare const appendReasoning: import("convex/server").RegisteredMutation
|
|
|
32
32
|
export declare const requestToolCall: import("convex/server").RegisteredMutation<"public", {
|
|
33
33
|
orgId: string;
|
|
34
34
|
toolName: string;
|
|
35
|
-
sessionId: string;
|
|
36
35
|
argsJson: string;
|
|
36
|
+
sessionId: string;
|
|
37
37
|
}, Promise<{
|
|
38
38
|
authorized: boolean;
|
|
39
39
|
approvalPending: boolean;
|
|
@@ -51,8 +51,8 @@ export declare const completeTurn: import("convex/server").RegisteredMutation<"p
|
|
|
51
51
|
}, Promise<null>>;
|
|
52
52
|
export declare const end: import("convex/server").RegisteredMutation<"public", {
|
|
53
53
|
orgId: string;
|
|
54
|
-
sessionId: string;
|
|
55
54
|
outcome: "completed" | "cancelled";
|
|
55
|
+
sessionId: string;
|
|
56
56
|
}, Promise<null>>;
|
|
57
57
|
export declare const listEvents: import("convex/server").RegisteredQuery<"public", {
|
|
58
58
|
orgId: string;
|
|
@@ -3,7 +3,7 @@ import { api, internal } from "../_generated/api.js";
|
|
|
3
3
|
import { mutation, query, } from "../_generated/server.js";
|
|
4
4
|
import { assertSameOrg, requireOrgId } from "../lib/auth.js";
|
|
5
5
|
import { workflow } from "./workflows.js";
|
|
6
|
-
import { agentIdOfRun, appendEventCore,
|
|
6
|
+
import { agentIdOfRun, appendEventCore, bindWorkflowToRun, loadRun, openRun, payloadOrUndefined, slotToEventId, } from "./world.js";
|
|
7
7
|
const SESSION_EVENT_NAMES = [
|
|
8
8
|
"session.started",
|
|
9
9
|
"turn.started",
|
|
@@ -21,7 +21,7 @@ function isSessionEventName(name) {
|
|
|
21
21
|
}
|
|
22
22
|
const NOT_FOUND = "Session not found";
|
|
23
23
|
export async function loadSessionRun(ctx, orgId, sessionId) {
|
|
24
|
-
return await loadRun(ctx, orgId, sessionId, "
|
|
24
|
+
return await loadRun(ctx, orgId, sessionId, "session", NOT_FOUND);
|
|
25
25
|
}
|
|
26
26
|
export async function insertEvent(ctx, args) {
|
|
27
27
|
await appendEventCore(ctx, {
|
|
@@ -30,16 +30,6 @@ export async function insertEvent(ctx, args) {
|
|
|
30
30
|
eventType: args.name,
|
|
31
31
|
dataJson: args.payload ?? "{}",
|
|
32
32
|
});
|
|
33
|
-
const legacy = await legacySessionOf(ctx, args.run, args.orgId);
|
|
34
|
-
if (legacy !== null) {
|
|
35
|
-
await ctx.db.insert("sessionEvents", {
|
|
36
|
-
orgId: args.orgId,
|
|
37
|
-
sessionId: legacy._id,
|
|
38
|
-
name: args.name,
|
|
39
|
-
payload: args.payload,
|
|
40
|
-
createdAt: Date.now(),
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
33
|
}
|
|
44
34
|
const CURRENT_SESSION_WORKFLOW_VERSION = "v2";
|
|
45
35
|
export const start = mutation({
|
|
@@ -52,20 +42,9 @@ export const start = mutation({
|
|
|
52
42
|
}
|
|
53
43
|
const agent = await ctx.db.get(args.agentId);
|
|
54
44
|
assertSameOrg(orgId, agent, "Agent not found");
|
|
55
|
-
const
|
|
56
|
-
const legacySessionId = await ctx.db.insert("sessions", {
|
|
45
|
+
const runId = await openRun(ctx, {
|
|
57
46
|
orgId,
|
|
58
|
-
|
|
59
|
-
status: "running",
|
|
60
|
-
createdBy: args.createdBy,
|
|
61
|
-
createdAt: now,
|
|
62
|
-
updatedAt: now,
|
|
63
|
-
workflowVersion: CURRENT_SESSION_WORKFLOW_VERSION,
|
|
64
|
-
});
|
|
65
|
-
const runId = await openLegacyRun(ctx, {
|
|
66
|
-
orgId,
|
|
67
|
-
table: "sessions",
|
|
68
|
-
legacyId: legacySessionId,
|
|
47
|
+
kind: "session",
|
|
69
48
|
data: {
|
|
70
49
|
agentId: args.agentId,
|
|
71
50
|
createdBy: args.createdBy,
|
|
@@ -76,7 +55,12 @@ export const start = mutation({
|
|
|
76
55
|
onComplete: internal.engine.workflows.onSessionWorkflowCompleteV2,
|
|
77
56
|
context: { orgId, runId },
|
|
78
57
|
});
|
|
79
|
-
await ctx
|
|
58
|
+
await bindWorkflowToRun(ctx, {
|
|
59
|
+
orgId,
|
|
60
|
+
runId,
|
|
61
|
+
workflowId,
|
|
62
|
+
workflowVersion: CURRENT_SESSION_WORKFLOW_VERSION,
|
|
63
|
+
});
|
|
80
64
|
const run = await loadSessionRun(ctx, orgId, runId);
|
|
81
65
|
await insertEvent(ctx, { orgId, run, name: "session.started" });
|
|
82
66
|
await ctx.runQuery(api.engine.resolvers.resolveManifest, {
|
|
@@ -100,10 +84,6 @@ export const sendTurn = mutation({
|
|
|
100
84
|
orgId,
|
|
101
85
|
sessionId: args.sessionId,
|
|
102
86
|
});
|
|
103
|
-
const legacy = await legacySessionOf(ctx, run, orgId);
|
|
104
|
-
if (legacy !== null) {
|
|
105
|
-
await ctx.db.patch(legacy._id, { updatedAt: Date.now() });
|
|
106
|
-
}
|
|
107
87
|
return null;
|
|
108
88
|
},
|
|
109
89
|
});
|
|
@@ -218,10 +198,6 @@ export const completeTurn = mutation({
|
|
|
218
198
|
const orgId = requireOrgId(args.orgId);
|
|
219
199
|
const run = await loadSessionRun(ctx, orgId, args.sessionId);
|
|
220
200
|
await insertEvent(ctx, { orgId, run, name: "turn.completed" });
|
|
221
|
-
const legacy = await legacySessionOf(ctx, run, orgId);
|
|
222
|
-
if (legacy !== null) {
|
|
223
|
-
await ctx.db.patch(legacy._id, { updatedAt: Date.now() });
|
|
224
|
-
}
|
|
225
201
|
return null;
|
|
226
202
|
},
|
|
227
203
|
});
|
|
@@ -246,10 +222,6 @@ export const end = mutation({
|
|
|
246
222
|
eventType: args.outcome === "completed" ? "run_completed" : "run_cancelled",
|
|
247
223
|
dataJson: JSON.stringify({ outcome: args.outcome }),
|
|
248
224
|
});
|
|
249
|
-
const legacy = await legacySessionOf(ctx, run, orgId);
|
|
250
|
-
if (legacy !== null) {
|
|
251
|
-
await ctx.db.patch(legacy._id, { status: args.outcome, updatedAt: Date.now() });
|
|
252
|
-
}
|
|
253
225
|
return null;
|
|
254
226
|
},
|
|
255
227
|
});
|
|
@@ -24,9 +24,9 @@ export declare const listStreamNames: import("convex/server").RegisteredQuery<"p
|
|
|
24
24
|
export declare const listChunksPage: import("convex/server").RegisteredQuery<"public", {
|
|
25
25
|
name: string;
|
|
26
26
|
orgId: string;
|
|
27
|
-
cursor: string | null;
|
|
28
27
|
runId: string;
|
|
29
28
|
limit: number;
|
|
29
|
+
cursor: string | null;
|
|
30
30
|
}, Promise<{
|
|
31
31
|
page: {
|
|
32
32
|
index: number;
|
|
@@ -3,11 +3,11 @@ export declare const wakeupPool: Workpool;
|
|
|
3
3
|
export declare const enqueue: import("convex/server").RegisteredAction<"public", {
|
|
4
4
|
payload?: string | undefined;
|
|
5
5
|
orgId: string;
|
|
6
|
-
workflowId: string;
|
|
7
6
|
agentId: import("convex/values").GenericId<"agents">;
|
|
8
|
-
awaitedEventName: string;
|
|
9
7
|
scope: string;
|
|
10
8
|
reason: string;
|
|
9
|
+
workflowId: string;
|
|
10
|
+
awaitedEventName: string;
|
|
11
11
|
cost: number;
|
|
12
12
|
workFunctionHandle: string;
|
|
13
13
|
}, Promise<string>>;
|
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
import { WorkflowManager } from "@convex-dev/workflow";
|
|
2
|
-
export declare const resolveManifestInternal: import("convex/server").RegisteredQuery<"internal", {
|
|
3
|
-
sessionId: import("convex/values").GenericId<"sessions">;
|
|
4
|
-
}, Promise<null>>;
|
|
5
|
-
export declare const recordTurnStartedInternal: import("convex/server").RegisteredMutation<"internal", {
|
|
6
|
-
sessionId: import("convex/values").GenericId<"sessions">;
|
|
7
|
-
}, Promise<null>>;
|
|
8
2
|
export declare const workflow: WorkflowManager;
|
|
9
|
-
export declare const sessionWorkflowV1: import("convex/server").RegisteredMutation<"internal", import("@convex-dev/workflow").WorkflowArgs<{
|
|
10
|
-
sessionId: import("convex/values").VId<import("convex/values").GenericId<"sessions">, "required">;
|
|
11
|
-
}>, import("@convex-dev/workflow").WorkflowId>;
|
|
12
3
|
export declare const resolveManifestInternalV2: import("convex/server").RegisteredQuery<"internal", {
|
|
13
4
|
orgId: string;
|
|
14
5
|
runId: string;
|
|
@@ -17,23 +8,12 @@ export declare const sessionWorkflowV2: import("convex/server").RegisteredMutati
|
|
|
17
8
|
orgId: import("convex/values").VString<string, "required">;
|
|
18
9
|
runId: import("convex/values").VString<string, "required">;
|
|
19
10
|
}>, import("@convex-dev/workflow").WorkflowId>;
|
|
20
|
-
export declare const
|
|
11
|
+
export declare const onSessionWorkflowCompleteV2: import("convex/server").RegisteredMutation<"internal", {
|
|
21
12
|
workflowId: import("@convex-dev/workflow").WorkflowId;
|
|
22
|
-
result: {
|
|
23
|
-
kind: "success";
|
|
24
|
-
returnValue: any;
|
|
25
|
-
} | {
|
|
26
|
-
kind: "failed";
|
|
27
|
-
error: string;
|
|
28
|
-
} | {
|
|
29
|
-
kind: "canceled";
|
|
30
|
-
};
|
|
31
13
|
context: {
|
|
32
|
-
|
|
14
|
+
orgId: string;
|
|
15
|
+
runId: string;
|
|
33
16
|
};
|
|
34
|
-
}, Promise<null>>;
|
|
35
|
-
export declare const onSessionWorkflowCompleteV2: import("convex/server").RegisteredMutation<"internal", {
|
|
36
|
-
workflowId: import("@convex-dev/workflow").WorkflowId;
|
|
37
17
|
result: {
|
|
38
18
|
kind: "success";
|
|
39
19
|
returnValue: any;
|
|
@@ -43,10 +23,6 @@ export declare const onSessionWorkflowCompleteV2: import("convex/server").Regist
|
|
|
43
23
|
} | {
|
|
44
24
|
kind: "canceled";
|
|
45
25
|
};
|
|
46
|
-
context: {
|
|
47
|
-
orgId: string;
|
|
48
|
-
runId: string;
|
|
49
|
-
};
|
|
50
26
|
}, Promise<null>>;
|
|
51
27
|
export declare const stopBoxInternal: import("convex/server").RegisteredAction<"internal", {
|
|
52
28
|
boxId: string;
|
|
@@ -3,52 +3,12 @@ import { v } from "convex/values";
|
|
|
3
3
|
import { components, internal } from "../_generated/api.js";
|
|
4
4
|
import { internalAction, internalMutation, internalQuery } from "../_generated/server.js";
|
|
5
5
|
import { agentIdOfRun, loadRun } from "./world.js";
|
|
6
|
-
export const resolveManifestInternal = internalQuery({
|
|
7
|
-
args: { sessionId: v.id("sessions") },
|
|
8
|
-
returns: v.null(),
|
|
9
|
-
handler: async (ctx, args) => {
|
|
10
|
-
const session = await ctx.db.get(args.sessionId);
|
|
11
|
-
if (!session)
|
|
12
|
-
throw new Error("Session not found");
|
|
13
|
-
const agent = await ctx.db.get(session.agentId);
|
|
14
|
-
if (!agent || agent.orgId !== session.orgId) {
|
|
15
|
-
throw new Error("Agent not found");
|
|
16
|
-
}
|
|
17
|
-
return null;
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
export const recordTurnStartedInternal = internalMutation({
|
|
21
|
-
args: { sessionId: v.id("sessions") },
|
|
22
|
-
returns: v.null(),
|
|
23
|
-
handler: async (ctx, args) => {
|
|
24
|
-
const session = await ctx.db.get(args.sessionId);
|
|
25
|
-
if (!session)
|
|
26
|
-
throw new Error("Session not found");
|
|
27
|
-
return null;
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
6
|
export const workflow = new WorkflowManager(components.workflow);
|
|
31
|
-
export const sessionWorkflowV1 = workflow.define({
|
|
32
|
-
args: { sessionId: v.id("sessions") },
|
|
33
|
-
returns: v.null(),
|
|
34
|
-
handler: async (step, args) => {
|
|
35
|
-
await step.runQuery(internal.engine.workflows.resolveManifestInternal, {
|
|
36
|
-
sessionId: args.sessionId,
|
|
37
|
-
});
|
|
38
|
-
await step.runMutation(internal.engine.workflows.recordTurnStartedInternal, {
|
|
39
|
-
sessionId: args.sessionId,
|
|
40
|
-
});
|
|
41
|
-
await step.runQuery(internal.engine.workflows.resolveManifestInternal, {
|
|
42
|
-
sessionId: args.sessionId,
|
|
43
|
-
});
|
|
44
|
-
return null;
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
7
|
export const resolveManifestInternalV2 = internalQuery({
|
|
48
8
|
args: { orgId: v.string(), runId: v.string() },
|
|
49
9
|
returns: v.null(),
|
|
50
10
|
handler: async (ctx, args) => {
|
|
51
|
-
const run = await loadRun(ctx, args.orgId, args.runId, "
|
|
11
|
+
const run = await loadRun(ctx, args.orgId, args.runId, "session", "Session not found");
|
|
52
12
|
const agent = await ctx.db.get(agentIdOfRun(ctx, run));
|
|
53
13
|
if (!agent || agent.orgId !== args.orgId) {
|
|
54
14
|
throw new Error("Agent not found");
|
|
@@ -67,30 +27,6 @@ export const sessionWorkflowV2 = workflow.define({
|
|
|
67
27
|
return null;
|
|
68
28
|
},
|
|
69
29
|
});
|
|
70
|
-
export const onSessionWorkflowComplete = internalMutation({
|
|
71
|
-
args: {
|
|
72
|
-
workflowId: vWorkflowId,
|
|
73
|
-
context: v.object({ sessionId: v.id("sessions") }),
|
|
74
|
-
result: vResultValidator,
|
|
75
|
-
},
|
|
76
|
-
returns: v.null(),
|
|
77
|
-
handler: async (ctx, args) => {
|
|
78
|
-
const session = await ctx.db.get(args.context.sessionId);
|
|
79
|
-
if (!session)
|
|
80
|
-
return null;
|
|
81
|
-
const sandboxConfigRow = await ctx.db
|
|
82
|
-
.query("sandboxConfig")
|
|
83
|
-
.withIndex("by_org_agent", (q) => q.eq("orgId", session.orgId).eq("agentId", session.agentId))
|
|
84
|
-
.unique();
|
|
85
|
-
const boxId = sandboxConfigRow?.boxId;
|
|
86
|
-
if (!boxId)
|
|
87
|
-
return null;
|
|
88
|
-
await ctx.scheduler.runAfter(0, internal.engine.workflows.stopBoxInternal, {
|
|
89
|
-
boxId,
|
|
90
|
-
});
|
|
91
|
-
return null;
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
30
|
export const onSessionWorkflowCompleteV2 = internalMutation({
|
|
95
31
|
args: {
|
|
96
32
|
workflowId: vWorkflowId,
|
|
@@ -102,7 +38,7 @@ export const onSessionWorkflowCompleteV2 = internalMutation({
|
|
|
102
38
|
const { orgId, runId } = args.context;
|
|
103
39
|
let agentId;
|
|
104
40
|
try {
|
|
105
|
-
const run = await loadRun(ctx, orgId, runId, "
|
|
41
|
+
const run = await loadRun(ctx, orgId, runId, "session", "Session not found");
|
|
106
42
|
agentId = agentIdOfRun(ctx, run);
|
|
107
43
|
}
|
|
108
44
|
catch {
|