@mastra/factory 0.29.0-alpha.5 → 0.29.0-alpha.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/README.md +55 -31
- package/dist/boards/semantics.d.ts.map +1 -1
- package/dist/boards/semantics.js +7 -1
- package/dist/boards/semantics.js.map +1 -1
- package/dist/factory.d.ts +10 -0
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +98 -41
- package/dist/factory.js.map +1 -1
- package/dist/integrations/github/issue-intake.d.ts +15 -0
- package/dist/integrations/github/issue-intake.d.ts.map +1 -0
- package/dist/integrations/github/issue-intake.js +50 -0
- package/dist/integrations/github/issue-intake.js.map +1 -0
- package/dist/integrations/github/issue-reconciler.d.ts.map +1 -1
- package/dist/integrations/github/issue-reconciler.js +10 -1
- package/dist/integrations/github/issue-reconciler.js.map +1 -1
- package/dist/integrations/github/rules.d.ts +2 -0
- package/dist/integrations/github/rules.d.ts.map +1 -1
- package/dist/integrations/github/rules.js +3 -0
- package/dist/integrations/github/rules.js.map +1 -1
- package/dist/integrations/platform/connect/routes.d.ts +43 -1
- package/dist/integrations/platform/connect/routes.d.ts.map +1 -1
- package/dist/integrations/platform/connect/routes.js +82 -7
- package/dist/integrations/platform/connect/routes.js.map +1 -1
- package/dist/integrations/platform/connect/session-tools.d.ts +12 -0
- package/dist/integrations/platform/connect/session-tools.d.ts.map +1 -1
- package/dist/integrations/platform/connect/session-tools.js +28 -10
- package/dist/integrations/platform/connect/session-tools.js.map +1 -1
- package/dist/integrations/platform/vanta/integration.d.ts.map +1 -1
- package/dist/integrations/platform/vanta/integration.js +3 -3
- package/dist/integrations/platform/vanta/integration.js.map +1 -1
- package/dist/integrations/vanta/sync.d.ts +9 -0
- package/dist/integrations/vanta/sync.d.ts.map +1 -1
- package/dist/integrations/vanta/sync.js +10 -1
- package/dist/integrations/vanta/sync.js.map +1 -1
- package/dist/routes/board-snapshot.d.ts +5 -5
- package/dist/routes/board-snapshot.d.ts.map +1 -1
- package/dist/routes/board-snapshot.js +10 -14
- package/dist/routes/board-snapshot.js.map +1 -1
- package/dist/routes/config.d.ts +11 -2
- package/dist/routes/config.d.ts.map +1 -1
- package/dist/routes/config.js +242 -75
- package/dist/routes/config.js.map +1 -1
- package/dist/routes/contracts.d.ts +43 -14
- package/dist/routes/contracts.d.ts.map +1 -1
- package/dist/routes/contracts.js +27 -17
- package/dist/routes/contracts.js.map +1 -1
- package/dist/routes/custom-endpoint-url.d.ts +8 -0
- package/dist/routes/custom-endpoint-url.d.ts.map +1 -0
- package/dist/routes/custom-endpoint-url.js +45 -0
- package/dist/routes/custom-endpoint-url.js.map +1 -0
- package/dist/routes/custom-provider-source.d.ts +24 -8
- package/dist/routes/custom-provider-source.d.ts.map +1 -1
- package/dist/routes/custom-provider-source.js +93 -43
- package/dist/routes/custom-provider-source.js.map +1 -1
- package/dist/routes/intake.d.ts +6 -0
- package/dist/routes/intake.d.ts.map +1 -1
- package/dist/routes/intake.js +20 -0
- package/dist/routes/intake.js.map +1 -1
- package/dist/routes/project-sessions.d.ts +15 -3
- package/dist/routes/project-sessions.d.ts.map +1 -1
- package/dist/routes/project-sessions.js +56 -14
- package/dist/routes/project-sessions.js.map +1 -1
- package/dist/routes/projects.d.ts +6 -1
- package/dist/routes/projects.d.ts.map +1 -1
- package/dist/routes/projects.js +19 -3
- package/dist/routes/projects.js.map +1 -1
- package/dist/routes/provider-credentials.d.ts +10 -1
- package/dist/routes/provider-credentials.d.ts.map +1 -1
- package/dist/routes/provider-credentials.js +13 -16
- package/dist/routes/provider-credentials.js.map +1 -1
- package/dist/routes/session-rows.d.ts +1 -1
- package/dist/routes/session-rows.d.ts.map +1 -1
- package/dist/routes/session-rows.js +38 -42
- package/dist/routes/session-rows.js.map +1 -1
- package/dist/routes/source-control-sessions.d.ts.map +1 -1
- package/dist/routes/source-control-sessions.js +4 -4
- package/dist/routes/source-control-sessions.js.map +1 -1
- package/dist/routes/surface.d.ts +3 -0
- package/dist/routes/surface.d.ts.map +1 -1
- package/dist/routes/surface.js +20 -7
- package/dist/routes/surface.js.map +1 -1
- package/dist/routes/tenant-credentials.d.ts.map +1 -1
- package/dist/routes/tenant-credentials.js +4 -4
- package/dist/routes/tenant-credentials.js.map +1 -1
- package/dist/routes/work-items.d.ts +9 -3
- package/dist/routes/work-items.d.ts.map +1 -1
- package/dist/routes/work-items.js +39 -16
- package/dist/routes/work-items.js.map +1 -1
- package/dist/rules/binding-context.d.ts +5 -0
- package/dist/rules/binding-context.d.ts.map +1 -1
- package/dist/rules/binding-context.js +51 -5
- package/dist/rules/binding-context.js.map +1 -1
- package/dist/rules/dispatcher.d.ts.map +1 -1
- package/dist/rules/dispatcher.js +2 -1
- package/dist/rules/dispatcher.js.map +1 -1
- package/dist/rules/start-coordinator.d.ts.map +1 -1
- package/dist/rules/start-coordinator.js +2 -7
- package/dist/rules/start-coordinator.js.map +1 -1
- package/dist/sandbox/session-retirement.d.ts +6 -0
- package/dist/sandbox/session-retirement.d.ts.map +1 -1
- package/dist/sandbox/session-retirement.js +17 -0
- package/dist/sandbox/session-retirement.js.map +1 -1
- package/dist/session/factory-session.d.ts.map +1 -1
- package/dist/session/factory-session.js +2 -1
- package/dist/session/factory-session.js.map +1 -1
- package/dist/session/live-sessions.d.ts +6 -1
- package/dist/session/live-sessions.d.ts.map +1 -1
- package/dist/session/live-sessions.js +4 -0
- package/dist/session/live-sessions.js.map +1 -1
- package/dist/session/run-audit.d.ts +1 -1
- package/dist/session/run-audit.d.ts.map +1 -1
- package/dist/session/run-audit.js.map +1 -1
- package/dist/session/session-row-status.d.ts +3 -3
- package/dist/session/session-row-status.js +4 -4
- package/dist/session/session-row-status.js.map +1 -1
- package/dist/session/session-row-view.d.ts +3 -4
- package/dist/session/session-row-view.d.ts.map +1 -1
- package/dist/session/session-row-view.js +14 -21
- package/dist/session/session-row-view.js.map +1 -1
- package/dist/session/session-rows.d.ts +22 -8
- package/dist/session/session-rows.d.ts.map +1 -1
- package/dist/session/session-rows.js +158 -83
- package/dist/session/session-rows.js.map +1 -1
- package/dist/session/session-unloader.d.ts +101 -0
- package/dist/session/session-unloader.d.ts.map +1 -0
- package/dist/session/session-unloader.js +278 -0
- package/dist/session/session-unloader.js.map +1 -0
- package/dist/session/work-item-repository.d.ts +5 -1
- package/dist/session/work-item-repository.d.ts.map +1 -1
- package/dist/session/work-item-repository.js +39 -15
- package/dist/session/work-item-repository.js.map +1 -1
- package/dist/session-list-changes.d.ts +9 -0
- package/dist/session-list-changes.d.ts.map +1 -0
- package/dist/session-list-changes.js +54 -0
- package/dist/session-list-changes.js.map +1 -0
- package/dist/storage/announced-lists.d.ts +14 -0
- package/dist/storage/announced-lists.d.ts.map +1 -0
- package/dist/storage/announced-lists.js +78 -0
- package/dist/storage/announced-lists.js.map +1 -0
- package/dist/storage/domains/boards/base.d.ts +1 -1
- package/dist/storage/domains/boards/base.d.ts.map +1 -1
- package/dist/storage/domains/boards/base.js +27 -9
- package/dist/storage/domains/boards/base.js.map +1 -1
- package/dist/storage/domains/custom-providers/base.d.ts +62 -19
- package/dist/storage/domains/custom-providers/base.d.ts.map +1 -1
- package/dist/storage/domains/custom-providers/base.js +131 -100
- package/dist/storage/domains/custom-providers/base.js.map +1 -1
- package/dist/storage/domains/intake/base.d.ts +25 -7
- package/dist/storage/domains/intake/base.d.ts.map +1 -1
- package/dist/storage/domains/intake/base.js +83 -19
- package/dist/storage/domains/intake/base.js.map +1 -1
- package/dist/storage/domains/source-control/base.d.ts +8 -0
- package/dist/storage/domains/source-control/base.d.ts.map +1 -1
- package/dist/storage/domains/source-control/base.js +110 -64
- package/dist/storage/domains/source-control/base.js.map +1 -1
- package/dist/storage/domains/source-control/inmemory.d.ts +4 -0
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
- package/dist/storage/domains/source-control/inmemory.js +8 -5
- package/dist/storage/domains/source-control/inmemory.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts +9 -1
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +43 -11
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/tools/read-tools.d.ts +9 -2
- package/dist/tools/read-tools.d.ts.map +1 -1
- package/dist/tools/read-tools.js +57 -12
- package/dist/tools/read-tools.js.map +1 -1
- package/dist/tools/session-tools.d.ts +0 -13
- package/dist/tools/session-tools.d.ts.map +1 -1
- package/dist/tools/session-tools.js +127 -336
- package/dist/tools/session-tools.js.map +1 -1
- package/dist/tools/work-item-tools.d.ts.map +1 -1
- package/dist/tools/work-item-tools.js +91 -23
- package/dist/tools/work-item-tools.js.map +1 -1
- package/dist/work-items/card-reference.d.ts +21 -0
- package/dist/work-items/card-reference.d.ts.map +1 -0
- package/dist/work-items/card-reference.js +58 -0
- package/dist/work-items/card-reference.js.map +1 -0
- package/package.json +7 -7
- package/dist/storage/probed-list.d.ts +0 -11
- package/dist/storage/probed-list.d.ts.map +0 -1
- package/dist/storage/probed-list.js +0 -52
- package/dist/storage/probed-list.js.map +0 -1
|
@@ -1,39 +1,19 @@
|
|
|
1
|
-
import "../
|
|
2
|
-
import { boardForWorkItem } from "../boards/semantics.js";
|
|
3
|
-
import { workLaneForRole } from "../boards/legacy-plan-role.js";
|
|
4
|
-
import { toBoardSource } from "../boards/project-boards.js";
|
|
5
|
-
import { factorySessionUrl, factoryWorkItemUrl } from "../links.js";
|
|
1
|
+
import { factorySessionUrl } from "../links.js";
|
|
6
2
|
import { buildModelCatalog, listSessionScopedCredentials } from "../routes/config.js";
|
|
3
|
+
import { customEndpointUrlProblem } from "../routes/custom-endpoint-url.js";
|
|
4
|
+
import { customProviderOwner } from "../routes/custom-provider-source.js";
|
|
7
5
|
import { primeTenantCredentialsForTenant } from "../routes/tenant-credentials.js";
|
|
8
|
-
import { FactoryStartTransitionError, WorkItemRepositoryError } from "../rules/start-coordinator.js";
|
|
9
6
|
import { resolveFactoryModelPolicy } from "../session/credential-policy.js";
|
|
10
|
-
import { ensureFactorySourceSession, hydrateFactorySession,
|
|
11
|
-
import { recordSessionRunStart } from "../session/run-audit.js";
|
|
12
|
-
import { resolveRunRepository } from "../session/work-item-repository.js";
|
|
13
|
-
import { effectiveModelScope, factoryPrincipalId } from "../storage/domains/projects/base.js";
|
|
7
|
+
import { ensureFactorySourceSession, hydrateFactorySession, resolveFactorySessionModel } from "../session/factory-session.js";
|
|
14
8
|
import { USER_SESSION_BRANCH_PREFIX } from "../work-item-branch.js";
|
|
15
|
-
import { workItemNumber } from "../work-items/card-number.js";
|
|
16
9
|
import { resolveSessionThread } from "./deliver.js";
|
|
17
|
-
import { findWorkItem, workItemNotFound, workItemRefSchema } from "./read-tools.js";
|
|
18
|
-
import { heartbeatOpenRunUntilEnd } from "./run-watch.js";
|
|
19
10
|
import { sessionModel } from "./side-sessions.js";
|
|
20
11
|
import { RequestContext } from "@mastra/core/request-context";
|
|
21
12
|
import { createTool } from "@mastra/core/tools";
|
|
22
13
|
import { z } from "zod";
|
|
23
14
|
// src/tools/session-tools.ts
|
|
24
15
|
const TITLE_LENGTH = 120;
|
|
25
|
-
/** Who renews the open-run entries this process writes; one id per process, like the dispatcher's. */
|
|
26
|
-
const FACTORY_TOOLS_OWNER_ID = `factory-tools:${globalThis.crypto.randomUUID()}`;
|
|
27
16
|
const LISTED_MODELS = 8;
|
|
28
|
-
function startErrorMessage(error) {
|
|
29
|
-
if (error instanceof WorkItemRepositoryError) return error.message;
|
|
30
|
-
if (error instanceof FactoryStartTransitionError) return `The start was refused: ${error.result.reason}`;
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
function workItemLabel(item) {
|
|
34
|
-
const number = workItemNumber(item);
|
|
35
|
-
return number === null ? `"${item.title}"` : `#${number}`;
|
|
36
|
-
}
|
|
37
17
|
function titleFrom(prompt) {
|
|
38
18
|
return (prompt.split("\n").find((line) => line.trim()) ?? prompt).trim().slice(0, TITLE_LENGTH);
|
|
39
19
|
}
|
|
@@ -68,6 +48,11 @@ function createFactorySessionTools(scope, deps) {
|
|
|
68
48
|
const modelScope = policy.tenant.credentialScope ?? "user";
|
|
69
49
|
const orgId = policy.tenant.orgId ?? scope.orgId;
|
|
70
50
|
const customProviders = tenantCatalog.customProviders;
|
|
51
|
+
const customProviderOwnerForSession = customProviderOwner({
|
|
52
|
+
orgId,
|
|
53
|
+
userId: policy.tenant.userId,
|
|
54
|
+
credentialScope: modelScope
|
|
55
|
+
}, true);
|
|
71
56
|
const usable = (await buildModelCatalog({
|
|
72
57
|
controller,
|
|
73
58
|
tenantCredentials: await listSessionScopedCredentials({
|
|
@@ -77,9 +62,9 @@ function createFactorySessionTools(scope, deps) {
|
|
|
77
62
|
modelScope
|
|
78
63
|
}),
|
|
79
64
|
deploymentProviders: tenantCatalog.deploymentProviders,
|
|
80
|
-
listCustomProviders: customProviders ? async () => {
|
|
65
|
+
listCustomProviders: customProviders && customProviderOwnerForSession ? async () => {
|
|
81
66
|
await customProviders.ensureReady();
|
|
82
|
-
return customProviders.list(
|
|
67
|
+
return (await customProviders.list(customProviderOwnerForSession)).filter((provider) => !customEndpointUrlProblem(provider.url));
|
|
83
68
|
} : void 0
|
|
84
69
|
})).map((model) => model.id);
|
|
85
70
|
for (const id of usable) known.add(id);
|
|
@@ -133,322 +118,128 @@ function createFactorySessionTools(scope, deps) {
|
|
|
133
118
|
};
|
|
134
119
|
}
|
|
135
120
|
});
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
role
|
|
121
|
+
return {
|
|
122
|
+
factory_start_session: createTool({
|
|
123
|
+
id: "factory_start_session",
|
|
124
|
+
description: [
|
|
125
|
+
"Start a side session in this Factory with a prompt. It runs on its own sandbox and branch as you, and reports back to this session with `factory-session` notifications.",
|
|
126
|
+
"A side session uses this session’s model. Pass `model` to start it on another model the session credentials can run.",
|
|
127
|
+
"Use it when the person asks for a side session, and for work no card stage does: investigations and research, experiments, pushing fixes to an existing pull request or taking one over, or work that should leave the board alone.",
|
|
128
|
+
"When someone asks for a card's own stage work without saying where, run it on the card instead, as the board's buttons do: \"review this pull request\" moves the pull request's card to its review stage or reruns it there, so the review is recorded on the card and posted on the pull request, and new work that should land as a pull request goes on its issue's card, or a card filed for it.",
|
|
129
|
+
"Find cards with factory_list_work_items (a pasted link works) and move them, or rerun their stage, with factory_transition_work_item."
|
|
130
|
+
].join(" "),
|
|
131
|
+
inputSchema: z.object({
|
|
132
|
+
prompt: z.string().trim().min(1).max(8e3).describe("The first message the new session receives."),
|
|
133
|
+
title: z.string().trim().min(1).max(TITLE_LENGTH).optional(),
|
|
134
|
+
model: z.string().trim().min(1).max(200).optional().describe("Model id for a side session. Omit it to inherit this session’s model. The session credentials must be able to run it.")
|
|
135
|
+
}).strict(),
|
|
136
|
+
execute: async ({ prompt, title, model }) => {
|
|
137
|
+
const parent = await deps.sessions.getBySessionId(scope.sessionId);
|
|
138
|
+
const sourceControl = await deps.sessions.getSourceControlBySessionId(scope.sessionId);
|
|
139
|
+
if (!parent || !sourceControl) throw new Error("This session has no Factory session row to start from.");
|
|
140
|
+
const messaging = await deps.messaging();
|
|
141
|
+
if (!messaging) throw new Error("Starting sessions is not available on this server.");
|
|
142
|
+
const parentThreadId = await resolveSessionThread({
|
|
143
|
+
controller: messaging.controller,
|
|
144
|
+
sessions: deps.sessions,
|
|
145
|
+
workItems: deps.workItems,
|
|
146
|
+
sessionId: scope.sessionId
|
|
163
147
|
});
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
workItems: deps.workItems,
|
|
174
|
-
sessionId: scope.sessionId
|
|
175
|
-
});
|
|
176
|
-
const parentResourceId = parentThreadId ? (await messaging.controller.queryThreadById({ threadId: parentThreadId }))?.resourceId ?? scope.sessionId : scope.sessionId;
|
|
177
|
-
const catalog = await sessionModelCatalog(messaging.controller, scope.sessionId, parentThreadId ?? scope.threadId, parentResourceId);
|
|
178
|
-
const inherited = model ? null : await sessionModel(messaging.controller, deps.sessions, deps.workItems, scope.sessionId);
|
|
179
|
-
const requestedModel = model ?? (inherited && catalog.usable.includes(inherited) ? inherited : void 0);
|
|
180
|
-
if (requestedModel && !catalog.usable.includes(requestedModel)) throw unrunnableModelError(requestedModel, catalog);
|
|
181
|
-
const sessionId = globalThis.crypto.randomUUID();
|
|
182
|
-
const ensured = await ensureFactorySourceSession({
|
|
183
|
-
sourceControl,
|
|
184
|
-
orgId: scope.orgId,
|
|
185
|
-
factoryProjectId: scope.factoryProjectId,
|
|
186
|
-
sessionId,
|
|
187
|
-
branch: `${USER_SESSION_BRANCH_PREFIX}${sessionId}`,
|
|
188
|
-
attributeToUserId: scope.userId,
|
|
189
|
-
...parent.projectRepositoryId ? { projectRepositoryId: parent.projectRepositoryId } : {},
|
|
190
|
-
visibility: parent.visibility,
|
|
191
|
-
origin: {
|
|
192
|
-
sessionId: scope.sessionId,
|
|
193
|
-
threadId: scope.threadId
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
const resolvedModel = await resolveFactorySessionModel({
|
|
197
|
-
...deps.sessionModel,
|
|
198
|
-
factoryProjectId: scope.factoryProjectId,
|
|
199
|
-
orgId: scope.orgId,
|
|
200
|
-
ownerUserId: scope.userId,
|
|
201
|
-
context: "session"
|
|
202
|
-
});
|
|
203
|
-
const kickoffContext = new RequestContext();
|
|
204
|
-
kickoffContext.set("user", {
|
|
205
|
-
workosId: scope.userId,
|
|
206
|
-
organizationId: scope.orgId
|
|
207
|
-
});
|
|
208
|
-
const tags = {
|
|
209
|
-
factoryProjectId: scope.factoryProjectId,
|
|
210
|
-
projectRepositoryId: ensured.projectRepositoryId
|
|
211
|
-
};
|
|
212
|
-
const session = await messaging.controller.createSession({
|
|
213
|
-
id: ensured.sessionId,
|
|
214
|
-
ownerId: scope.userId,
|
|
215
|
-
resourceId: parent.visibility === "private" ? scope.userId : scope.factoryProjectId,
|
|
216
|
-
threadId: ensured.sessionId,
|
|
217
|
-
requestContext: kickoffContext,
|
|
218
|
-
tags
|
|
219
|
-
});
|
|
220
|
-
await session.state.set(tags);
|
|
221
|
-
await hydrateFactorySession(session, {
|
|
222
|
-
orgId: scope.orgId,
|
|
223
|
-
modelScope: resolvedModel.modelScope,
|
|
224
|
-
ownerUserId: scope.userId,
|
|
225
|
-
defaultModelId: requestedModel ?? resolvedModel.modelId
|
|
226
|
-
});
|
|
227
|
-
const threadId = session.thread.requireId();
|
|
228
|
-
const threadTitle = title ?? titleFrom(prompt);
|
|
229
|
-
await session.thread.rename({
|
|
230
|
-
title: threadTitle,
|
|
231
|
-
pin: false
|
|
232
|
-
});
|
|
233
|
-
await sourceControl.sessions.rename({
|
|
234
|
-
sessionId: ensured.sessionId,
|
|
235
|
-
title: threadTitle
|
|
236
|
-
});
|
|
237
|
-
await session.thread.setSetting({
|
|
238
|
-
key: "factorySessionId",
|
|
239
|
-
value: ensured.sessionId
|
|
240
|
-
});
|
|
241
|
-
await primeTenantCredentialsForTenant({
|
|
242
|
-
orgId: scope.orgId,
|
|
243
|
-
userId: scope.userId,
|
|
244
|
-
credentialScope: resolvedModel.modelScope
|
|
245
|
-
});
|
|
246
|
-
await session.sendSignal({
|
|
247
|
-
id: globalThis.crypto.randomUUID(),
|
|
248
|
-
type: "user",
|
|
249
|
-
tagName: "user",
|
|
250
|
-
contents: prompt
|
|
251
|
-
}, {
|
|
252
|
-
requestContext: kickoffContext,
|
|
253
|
-
requireDelivery: true
|
|
254
|
-
}).accepted;
|
|
255
|
-
return {
|
|
256
|
-
mode: "side",
|
|
257
|
-
sessionId: ensured.sessionId,
|
|
258
|
-
threadId,
|
|
259
|
-
title: threadTitle,
|
|
260
|
-
url: factorySessionUrl({
|
|
148
|
+
const parentResourceId = parentThreadId ? (await messaging.controller.queryThreadById({ threadId: parentThreadId }))?.resourceId ?? scope.sessionId : scope.sessionId;
|
|
149
|
+
const catalog = await sessionModelCatalog(messaging.controller, scope.sessionId, parentThreadId ?? scope.threadId, parentResourceId);
|
|
150
|
+
const inherited = model ? null : await sessionModel(messaging.controller, deps.sessions, deps.workItems, scope.sessionId);
|
|
151
|
+
const requestedModel = model ?? (inherited && catalog.usable.includes(inherited) ? inherited : void 0);
|
|
152
|
+
if (requestedModel && !catalog.usable.includes(requestedModel)) throw unrunnableModelError(requestedModel, catalog);
|
|
153
|
+
const sessionId = globalThis.crypto.randomUUID();
|
|
154
|
+
const ensured = await ensureFactorySourceSession({
|
|
155
|
+
sourceControl,
|
|
156
|
+
orgId: scope.orgId,
|
|
261
157
|
factoryProjectId: scope.factoryProjectId,
|
|
262
|
-
sessionId
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const coordinator = deps.startCoordinator();
|
|
271
|
-
if (!coordinator) throw new Error("Factory sessions can't be started yet; try again shortly.");
|
|
272
|
-
const item = await findWorkItem(deps.workItems, scope, workItem);
|
|
273
|
-
if (!item) throw workItemNotFound(workItem);
|
|
274
|
-
const label = workItemLabel(item);
|
|
275
|
-
const board = (await toBoardSource(deps.boards).load(scope)).get(boardForWorkItem(item));
|
|
276
|
-
if (!workLaneForRole(board, role) && !board?.phaseForRole(role)) throw new Error(`${label}'s board has no ${role} role.`);
|
|
277
|
-
const seated = item.sessions[role];
|
|
278
|
-
if (seated) throw new Error(`${label} already has a ${role} session ${seated.sessionId}; message it with factory_signal_session.`);
|
|
279
|
-
const parent = await deps.sessions.getBySessionId(scope.sessionId);
|
|
280
|
-
const sourceControl = await deps.sessions.getSourceControlBySessionId(scope.sessionId);
|
|
281
|
-
if (!parent || !sourceControl) throw new Error("This session has no Factory session row to start from.");
|
|
282
|
-
const messaging = await deps.messaging();
|
|
283
|
-
if (!messaging) throw new Error("Starting sessions is not available on this server.");
|
|
284
|
-
const project = await deps.sessionModel.projects.get({
|
|
285
|
-
orgId: scope.orgId,
|
|
286
|
-
id: scope.factoryProjectId
|
|
287
|
-
});
|
|
288
|
-
if (!project) throw new Error("This Factory no longer exists.");
|
|
289
|
-
const modelScope = effectiveModelScope(project.modelScope, "run");
|
|
290
|
-
const ownerUserId = modelScope === "factory" ? factoryPrincipalId(project.id) : scope.userId;
|
|
291
|
-
if (modelScope === "factory") await requireOrganizationModelKey({
|
|
292
|
-
modelCredentials: deps.sessionModel.tenantCatalog?.credentials,
|
|
293
|
-
customProviders: deps.sessionModel.tenantCatalog?.customProviders,
|
|
294
|
-
deploymentModelProviders: deps.sessionModel.tenantCatalog?.deploymentProviders
|
|
295
|
-
}, project);
|
|
296
|
-
const projectRepositoryId = await resolveRepository(sourceControl, item);
|
|
297
|
-
const sessionId = globalThis.crypto.randomUUID();
|
|
298
|
-
const ensured = await ensureFactorySourceSession({
|
|
299
|
-
sourceControl,
|
|
300
|
-
orgId: scope.orgId,
|
|
301
|
-
factoryProjectId: scope.factoryProjectId,
|
|
302
|
-
sessionId,
|
|
303
|
-
branch: `${USER_SESSION_BRANCH_PREFIX}${sessionId}`,
|
|
304
|
-
projectRepositoryId,
|
|
305
|
-
attributeToUserId: ownerUserId,
|
|
306
|
-
visibility: "org"
|
|
307
|
-
});
|
|
308
|
-
const followScope = {
|
|
309
|
-
orgId: scope.orgId,
|
|
310
|
-
factoryProjectId: scope.factoryProjectId
|
|
311
|
-
};
|
|
312
|
-
const alreadyFollowing = (await deps.workItems.listSessionSubscriptions({
|
|
313
|
-
...followScope,
|
|
314
|
-
sessionId: scope.sessionId
|
|
315
|
-
})).some((subscription) => subscription.workItemId === item.id);
|
|
316
|
-
await deps.workItems.subscribeToWorkItem({
|
|
317
|
-
...followScope,
|
|
318
|
-
workItemId: item.id,
|
|
319
|
-
sessionId: scope.sessionId,
|
|
320
|
-
threadId: scope.threadId,
|
|
321
|
-
resourceId: scope.resourceId,
|
|
322
|
-
userId: scope.userId,
|
|
323
|
-
reason: "started"
|
|
324
|
-
});
|
|
325
|
-
const startedAt = /* @__PURE__ */ new Date();
|
|
326
|
-
let prepared;
|
|
327
|
-
try {
|
|
328
|
-
prepared = await coordinator.prepare({
|
|
329
|
-
orgId: scope.orgId,
|
|
330
|
-
userId: ownerUserId,
|
|
331
|
-
startedBy: {
|
|
332
|
-
type: "human",
|
|
333
|
-
id: scope.userId
|
|
334
|
-
},
|
|
335
|
-
factoryProjectId: scope.factoryProjectId,
|
|
336
|
-
sessionId: ensured.sessionId,
|
|
337
|
-
threadTitle: title ?? `${label} ${role}`,
|
|
338
|
-
kickoffKey: globalThis.crypto.randomUUID(),
|
|
339
|
-
modelScope,
|
|
340
|
-
defaultModelId: modelScope === "factory" ? project.defaultModelId ?? void 0 : void 0,
|
|
341
|
-
workItem: {
|
|
342
|
-
id: item.id,
|
|
343
|
-
role,
|
|
344
|
-
input: {
|
|
345
|
-
externalSource: item.externalSource,
|
|
346
|
-
parentWorkItemId: item.parentWorkItemId,
|
|
347
|
-
title: item.title,
|
|
348
|
-
stages: item.stages,
|
|
349
|
-
sessions: item.sessions,
|
|
350
|
-
metadata: item.metadata
|
|
158
|
+
sessionId,
|
|
159
|
+
branch: `${USER_SESSION_BRANCH_PREFIX}${sessionId}`,
|
|
160
|
+
attributeToUserId: scope.userId,
|
|
161
|
+
...parent.projectRepositoryId ? { projectRepositoryId: parent.projectRepositoryId } : {},
|
|
162
|
+
visibility: parent.visibility,
|
|
163
|
+
origin: {
|
|
164
|
+
sessionId: scope.sessionId,
|
|
165
|
+
threadId: scope.threadId
|
|
351
166
|
}
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
if (!alreadyFollowing) await deps.workItems.unsubscribeFromWorkItem({
|
|
356
|
-
orgId: scope.orgId,
|
|
357
|
-
workItemId: item.id,
|
|
358
|
-
sessionId: scope.sessionId
|
|
359
|
-
});
|
|
360
|
-
const message = startErrorMessage(error);
|
|
361
|
-
if (message) throw new Error(message);
|
|
362
|
-
throw error;
|
|
363
|
-
}
|
|
364
|
-
const replaced = (await deps.workItems.listRunBindings(scope.orgId, scope.factoryProjectId, item.id)).find((binding) => binding.role === role && binding.id !== prepared.bindingId && binding.revokedAt !== null && binding.revokedAt >= startedAt);
|
|
365
|
-
const session = await messaging.controller.getSession(prepared.sessionId);
|
|
366
|
-
if (!session) throw new Error(`The ${role} session ${prepared.sessionId} was bound but is not running; message it with factory_signal_session to wake it.`);
|
|
367
|
-
const kickoffId = globalThis.crypto.randomUUID();
|
|
368
|
-
const heartbeat = heartbeatOpenRunUntilEnd(session, kickoffId);
|
|
369
|
-
const kickoffContext = new RequestContext();
|
|
370
|
-
kickoffContext.set("user", {
|
|
371
|
-
workosId: ownerUserId,
|
|
372
|
-
organizationId: scope.orgId
|
|
373
|
-
});
|
|
374
|
-
try {
|
|
375
|
-
await session.sendSignal({
|
|
376
|
-
id: kickoffId,
|
|
377
|
-
type: "user",
|
|
378
|
-
tagName: "user",
|
|
379
|
-
contents: prompt
|
|
380
|
-
}, {
|
|
381
|
-
requestContext: kickoffContext,
|
|
382
|
-
requireDelivery: true
|
|
383
|
-
}).accepted;
|
|
384
|
-
} catch (error) {
|
|
385
|
-
heartbeat.stop();
|
|
386
|
-
throw error;
|
|
387
|
-
}
|
|
388
|
-
await recordSessionRunStart(session, {
|
|
389
|
-
audit: deps.auditRecorder,
|
|
390
|
-
actorType: "human",
|
|
391
|
-
observedEnd: heartbeat.endReason,
|
|
392
|
-
run: {
|
|
393
|
-
kickoffId,
|
|
394
|
-
ownerId: FACTORY_TOOLS_OWNER_ID,
|
|
395
|
-
heartbeatAt: Date.now(),
|
|
396
|
-
bindingId: prepared.bindingId,
|
|
397
|
-
role,
|
|
398
|
-
startedBy: scope.userId,
|
|
399
|
-
orgId: scope.orgId,
|
|
400
|
-
factoryProjectId: scope.factoryProjectId,
|
|
401
|
-
workItemId: item.id,
|
|
402
|
-
workItemName: item.title,
|
|
403
|
-
sessionId: prepared.sessionId,
|
|
404
|
-
threadId: prepared.threadId,
|
|
405
|
-
branch: prepared.branch
|
|
406
|
-
}
|
|
407
|
-
});
|
|
408
|
-
const number = workItemNumber(item);
|
|
409
|
-
return {
|
|
410
|
-
mode: "work-item",
|
|
411
|
-
workItem: {
|
|
412
|
-
id: item.id,
|
|
413
|
-
...number === null ? {} : { number },
|
|
414
|
-
url: factoryWorkItemUrl({
|
|
167
|
+
});
|
|
168
|
+
const resolvedModel = await resolveFactorySessionModel({
|
|
169
|
+
...deps.sessionModel,
|
|
415
170
|
factoryProjectId: scope.factoryProjectId,
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
171
|
+
orgId: scope.orgId,
|
|
172
|
+
ownerUserId: scope.userId,
|
|
173
|
+
context: "session"
|
|
174
|
+
});
|
|
175
|
+
const kickoffContext = new RequestContext();
|
|
176
|
+
kickoffContext.set("user", {
|
|
177
|
+
workosId: scope.userId,
|
|
178
|
+
organizationId: scope.orgId
|
|
179
|
+
});
|
|
180
|
+
const tags = {
|
|
181
|
+
factoryProjectId: scope.factoryProjectId,
|
|
182
|
+
projectRepositoryId: ensured.projectRepositoryId
|
|
183
|
+
};
|
|
184
|
+
const session = await messaging.controller.createSession({
|
|
185
|
+
id: ensured.sessionId,
|
|
186
|
+
ownerId: scope.userId,
|
|
187
|
+
resourceId: parent.visibility === "private" ? scope.userId : scope.factoryProjectId,
|
|
188
|
+
threadId: ensured.sessionId,
|
|
189
|
+
requestContext: kickoffContext,
|
|
190
|
+
tags
|
|
191
|
+
});
|
|
192
|
+
await session.state.set(tags);
|
|
193
|
+
await hydrateFactorySession(session, {
|
|
194
|
+
orgId: scope.orgId,
|
|
195
|
+
modelScope: resolvedModel.modelScope,
|
|
196
|
+
ownerUserId: scope.userId,
|
|
197
|
+
defaultModelId: requestedModel ?? resolvedModel.modelId
|
|
198
|
+
});
|
|
199
|
+
const threadId = session.thread.requireId();
|
|
200
|
+
const threadTitle = title ?? titleFrom(prompt);
|
|
201
|
+
await session.thread.rename({
|
|
202
|
+
title: threadTitle,
|
|
203
|
+
pin: false
|
|
204
|
+
});
|
|
205
|
+
await sourceControl.sessions.rename({
|
|
206
|
+
sessionId: ensured.sessionId,
|
|
207
|
+
title: threadTitle
|
|
208
|
+
});
|
|
209
|
+
await session.thread.setSetting({
|
|
210
|
+
key: "factorySessionId",
|
|
211
|
+
value: ensured.sessionId
|
|
212
|
+
});
|
|
213
|
+
await primeTenantCredentialsForTenant({
|
|
214
|
+
orgId: scope.orgId,
|
|
215
|
+
userId: scope.userId,
|
|
216
|
+
credentialScope: resolvedModel.modelScope
|
|
217
|
+
});
|
|
218
|
+
await session.sendSignal({
|
|
219
|
+
id: globalThis.crypto.randomUUID(),
|
|
220
|
+
type: "user",
|
|
221
|
+
tagName: "user",
|
|
222
|
+
contents: prompt
|
|
223
|
+
}, {
|
|
224
|
+
requestContext: kickoffContext,
|
|
225
|
+
requireDelivery: true
|
|
226
|
+
}).accepted;
|
|
227
|
+
return {
|
|
228
|
+
mode: "side",
|
|
229
|
+
sessionId: ensured.sessionId,
|
|
230
|
+
threadId,
|
|
231
|
+
title: threadTitle,
|
|
232
|
+
url: factorySessionUrl({
|
|
233
|
+
factoryProjectId: scope.factoryProjectId,
|
|
234
|
+
sessionId: ensured.sessionId,
|
|
235
|
+
threadId
|
|
236
|
+
}) ?? null
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}),
|
|
449
240
|
factory_switch_session_model: switchModel
|
|
450
241
|
};
|
|
451
242
|
}
|
|
452
|
-
export {
|
|
243
|
+
export { createFactorySessionTools };
|
|
453
244
|
|
|
454
245
|
//# sourceMappingURL=session-tools.js.map
|