@mastra/factory 0.8.0-alpha.9 → 0.8.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/CHANGELOG.md +630 -0
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +41 -3
- package/dist/factory.js.map +1 -1
- package/dist/integrations/base.d.ts +8 -0
- package/dist/integrations/base.d.ts.map +1 -1
- package/dist/integrations/github/app-identity.d.ts +43 -0
- package/dist/integrations/github/app-identity.d.ts.map +1 -0
- package/dist/integrations/github/app-identity.js +62 -0
- package/dist/integrations/github/app-identity.js.map +1 -0
- package/dist/integrations/github/integration.d.ts.map +1 -1
- package/dist/integrations/github/integration.js +3 -1
- package/dist/integrations/github/integration.js.map +1 -1
- package/dist/integrations/github/reconcile-worker.d.ts +5 -0
- package/dist/integrations/github/reconcile-worker.d.ts.map +1 -1
- package/dist/integrations/github/reconcile-worker.js +7 -0
- package/dist/integrations/github/reconcile-worker.js.map +1 -1
- package/dist/integrations/github/routes.d.ts.map +1 -1
- package/dist/integrations/github/routes.js +11 -6
- package/dist/integrations/github/routes.js.map +1 -1
- package/dist/integrations/github/rules.d.ts +8 -0
- package/dist/integrations/github/rules.d.ts.map +1 -1
- package/dist/integrations/github/rules.js +30 -8
- package/dist/integrations/github/rules.js.map +1 -1
- package/dist/integrations/github/sandbox.d.ts +39 -6
- package/dist/integrations/github/sandbox.d.ts.map +1 -1
- package/dist/integrations/github/sandbox.js +48 -14
- package/dist/integrations/github/sandbox.js.map +1 -1
- package/dist/integrations/github/webhook.d.ts +20 -0
- package/dist/integrations/github/webhook.d.ts.map +1 -1
- package/dist/integrations/github/webhook.js +32 -5
- package/dist/integrations/github/webhook.js.map +1 -1
- package/dist/integrations/linear/rules.d.ts.map +1 -1
- package/dist/integrations/linear/rules.js +2 -1
- package/dist/integrations/linear/rules.js.map +1 -1
- package/dist/integrations/platform/github/event-worker.d.ts +11 -0
- package/dist/integrations/platform/github/event-worker.d.ts.map +1 -1
- package/dist/integrations/platform/github/event-worker.js +53 -14
- package/dist/integrations/platform/github/event-worker.js.map +1 -1
- package/dist/integrations/platform/github/integration.d.ts +7 -0
- package/dist/integrations/platform/github/integration.d.ts.map +1 -1
- package/dist/integrations/platform/github/integration.js +40 -4
- package/dist/integrations/platform/github/integration.js.map +1 -1
- package/dist/integrations/slack/slack.d.ts.map +1 -1
- package/dist/integrations/slack/slack.js +2 -1
- package/dist/integrations/slack/slack.js.map +1 -1
- package/dist/routes/config.d.ts +12 -6
- package/dist/routes/config.d.ts.map +1 -1
- package/dist/routes/config.js +97 -56
- package/dist/routes/config.js.map +1 -1
- package/dist/routes/knowledge.d.ts +152 -0
- package/dist/routes/knowledge.d.ts.map +1 -0
- package/dist/routes/knowledge.js +442 -0
- package/dist/routes/knowledge.js.map +1 -0
- package/dist/routes/projects.d.ts +9 -1
- package/dist/routes/projects.d.ts.map +1 -1
- package/dist/routes/projects.js +8 -0
- package/dist/routes/projects.js.map +1 -1
- package/dist/routes/surface.d.ts +6 -0
- package/dist/routes/surface.d.ts.map +1 -1
- package/dist/routes/surface.js +9 -0
- package/dist/routes/surface.js.map +1 -1
- package/dist/routes/work-items.d.ts.map +1 -1
- package/dist/routes/work-items.js +3 -1
- package/dist/routes/work-items.js.map +1 -1
- package/dist/rules/binding-context.d.ts.map +1 -1
- package/dist/rules/binding-context.js +4 -1
- package/dist/rules/binding-context.js.map +1 -1
- package/dist/rules/defaults.d.ts.map +1 -1
- package/dist/rules/defaults.js +87 -4
- package/dist/rules/defaults.js.map +1 -1
- package/dist/rules/dispatcher.d.ts +6 -0
- package/dist/rules/dispatcher.d.ts.map +1 -1
- package/dist/rules/dispatcher.js +118 -16
- package/dist/rules/dispatcher.js.map +1 -1
- package/dist/rules/processor.d.ts.map +1 -1
- package/dist/rules/processor.js +20 -7
- package/dist/rules/processor.js.map +1 -1
- package/dist/rules/resolve.d.ts +1 -0
- package/dist/rules/resolve.d.ts.map +1 -1
- package/dist/rules/resolve.js +3 -2
- package/dist/rules/resolve.js.map +1 -1
- package/dist/rules/start-coordinator.d.ts +2 -0
- package/dist/rules/start-coordinator.d.ts.map +1 -1
- package/dist/rules/start-coordinator.js +3 -1
- package/dist/rules/start-coordinator.js.map +1 -1
- package/dist/rules/terminal-cleanup.d.ts +23 -0
- package/dist/rules/terminal-cleanup.d.ts.map +1 -0
- package/dist/rules/terminal-cleanup.js +43 -0
- package/dist/rules/terminal-cleanup.js.map +1 -0
- package/dist/rules/tools.d.ts.map +1 -1
- package/dist/rules/tools.js +24 -2
- package/dist/rules/tools.js.map +1 -1
- package/dist/rules/transition-service.d.ts +3 -0
- package/dist/rules/transition-service.d.ts.map +1 -1
- package/dist/rules/transition-service.js +10 -6
- package/dist/rules/transition-service.js.map +1 -1
- package/dist/rules/types.d.ts +32 -3
- package/dist/rules/types.d.ts.map +1 -1
- package/dist/rules/types.js +2 -0
- package/dist/rules/types.js.map +1 -1
- package/dist/rules/validation.d.ts.map +1 -1
- package/dist/rules/validation.js +8 -3
- package/dist/rules/validation.js.map +1 -1
- package/dist/sandbox/base-checkpoint-triggers.d.ts +77 -0
- package/dist/sandbox/base-checkpoint-triggers.d.ts.map +1 -0
- package/dist/sandbox/base-checkpoint-triggers.js +137 -0
- package/dist/sandbox/base-checkpoint-triggers.js.map +1 -0
- package/dist/sandbox/base-checkpoint.d.ts +59 -0
- package/dist/sandbox/base-checkpoint.d.ts.map +1 -0
- package/dist/sandbox/base-checkpoint.js +145 -0
- package/dist/sandbox/base-checkpoint.js.map +1 -0
- package/dist/sandbox/fleet.d.ts +32 -0
- package/dist/sandbox/fleet.d.ts.map +1 -1
- package/dist/sandbox/fleet.js +39 -10
- package/dist/sandbox/fleet.js.map +1 -1
- package/dist/session/checkpoint-capture.d.ts +3 -2
- package/dist/session/checkpoint-capture.d.ts.map +1 -1
- package/dist/session/checkpoint-capture.js +3 -3
- package/dist/session/checkpoint-capture.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/model-pack-hydration.d.ts +55 -0
- package/dist/session/model-pack-hydration.d.ts.map +1 -0
- package/dist/session/model-pack-hydration.js +56 -0
- package/dist/session/model-pack-hydration.js.map +1 -0
- package/dist/skills/service.d.ts +9 -0
- package/dist/skills/service.d.ts.map +1 -1
- package/dist/skills/service.js +10 -1
- package/dist/skills/service.js.map +1 -1
- package/dist/storage/domains/model-packs/base.d.ts +26 -0
- package/dist/storage/domains/model-packs/base.d.ts.map +1 -1
- package/dist/storage/domains/model-packs/base.js +102 -13
- package/dist/storage/domains/model-packs/base.js.map +1 -1
- package/dist/storage/domains/source-control/base.d.ts +47 -1
- package/dist/storage/domains/source-control/base.d.ts.map +1 -1
- package/dist/storage/domains/source-control/base.js +78 -19
- package/dist/storage/domains/source-control/base.js.map +1 -1
- package/dist/storage/domains/source-control/inmemory.d.ts +11 -3
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
- package/dist/storage/domains/source-control/inmemory.js +10 -1
- package/dist/storage/domains/source-control/inmemory.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts +67 -1
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +140 -12
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/workspace.d.ts +31 -2
- package/dist/workspace.d.ts.map +1 -1
- package/dist/workspace.js +225 -54
- package/dist/workspace.js.map +1 -1
- package/factory-skills/factory-plan/SKILL.md +1 -1
- package/factory-skills/factory-rereview/SKILL.md +3 -3
- package/factory-skills/factory-review/SKILL.md +3 -3
- package/factory-skills/factory-triage/SKILL.md +2 -0
- package/package.json +9 -9
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factory-session.js","names":[],"sources":["../../src/session/factory-session.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\n\nimport type { MastraCodeState } from '@mastra/code-sdk/schema';\nimport type { AgentController } from '@mastra/core/agent-controller';\n\nimport type { MemorySettingsStorage } from '../storage/domains/memory-settings/base.js';\nimport type { FactoryProjectsStorage } from '../storage/domains/projects/base.js';\nimport type { SourceControlStorageHandle } from '../storage/domains/source-control/base.js';\nimport { applyStoredMemorySettings } from './memory-settings-hydration.js';\n\ntype FactorySession = Awaited<ReturnType<AgentController<MastraCodeState>['createSession']>>;\n\n/**\n * Read the factory project's default model. Best-effort: a missing project or an\n * uninitialized storage domain means \"no default\", never a failed run.\n */\nexport async function resolveFactoryDefaultModelId(\n projects: FactoryProjectsStorage | undefined,\n factoryProjectId: string | undefined,\n): Promise<string | undefined> {\n if (!projects || !factoryProjectId) return undefined;\n try {\n const project = await projects.getById({ id: factoryProjectId });\n return project?.defaultModelId ?? undefined;\n } catch {\n return undefined;\n }\n}\n\nexport interface EnsureFactorySourceSessionArgs {\n /**\n * Storage handle of the integration that owns source control. Nothing here is\n * provider-specific: the connection is matched by the handle's own\n * `integrationId`, so GitHub, Slack-on-behalf-of-GitHub, or any future owner\n * all resolve through the same traversal.\n */\n sourceControl: SourceControlStorageHandle;\n orgId: string;\n factoryProjectId: string;\n branch: string;\n /** Pick a specific linked repository by slug. Defaults to the first linked repository. */\n repositorySlug?: string;\n}\n\nexport interface EnsuredFactorySourceSession {\n sessionId: string;\n userId: string;\n projectRepositoryId: string;\n branch: string;\n baseBranch: string;\n}\n\nexport interface ResolvedFactorySourceRepository {\n projectRepositoryId: string;\n /** The repository's pinned branch, else its default branch. */\n baseBranch: string;\n /** Who connected the repository. The attribution for runs with no interactive user. */\n connectedByUserId: string;\n}\n\n/**\n * Outcome of {@link resolveFactorySourceRepository}. A miss carries which step\n * failed: callers differ on whether that is an error (an autonomous run cannot\n * proceed) or a routine fallback (a chat integration drops to a chat-only\n * session), and the two steps fail for different reasons worth reporting apart.\n */\nexport type FactorySourceRepositoryResult =\n | ({ found: true } & ResolvedFactorySourceRepository)\n | { found: false; reason: 'connection' | 'repository' };\n\n/**\n * Resolve which repository a factory project's source-control runs act on: the\n * owner's connection on the project, then one of its linked repositories.\n *\n * The owner is whichever integration owns source control, matched by the\n * handle's own `integrationId` — nothing here is provider-specific.\n */\nexport async function resolveFactorySourceRepository(args: {\n sourceControl: SourceControlStorageHandle;\n orgId: string;\n factoryProjectId: string;\n /** Pick a specific linked repository by slug. Defaults to the first linked repository. */\n repositorySlug?: string;\n}): Promise<FactorySourceRepositoryResult> {\n const { sourceControl, orgId, factoryProjectId, repositorySlug } = args;\n\n const connections = await sourceControl.connections.list({ orgId, factoryProjectId });\n const candidates = connections.filter(candidate => candidate.integrationId === sourceControl.integrationId);\n if (candidates.length === 0) return { found: false, reason: 'connection' };\n\n // A project can carry stale connections: a provider-app reinstall leaves the\n // old connection pointing at an installation that no longer exists, and that\n // row can sit ahead of the healthy one. Try every candidate and skip the ones\n // that no longer resolve rather than failing on the first.\n for (const connection of candidates) {\n let resolved;\n try {\n const projectRepositories = await sourceControl.projectRepositories.list({ orgId, connectionId: connection.id });\n const resolvedRepositories = await Promise.all(\n projectRepositories.map(async projectRepository => ({\n projectRepository,\n repository: await sourceControl.repositories.get({ orgId, id: projectRepository.repositoryId }),\n })),\n );\n resolved = resolvedRepositories.find(\n candidate => candidate.repository && (!repositorySlug || candidate.repository.slug === repositorySlug),\n );\n } catch {\n // The connection no longer resolves (e.g. its installation was deleted).\n continue;\n }\n if (!resolved?.repository) continue;\n\n return {\n found: true,\n projectRepositoryId: resolved.projectRepository.id,\n baseBranch: resolved.projectRepository.branch ?? resolved.repository.defaultBranch,\n connectedByUserId: connection.createdByUserId,\n };\n }\n\n return { found: false, reason: 'repository' };\n}\n\n/**\n * Walk a Factory user-session id back to the project it belongs to.\n *\n * Repo-backed channel threads are keyed by their Factory session id, which is\n * the only handle a session-start hook gets. This turns that id back into the\n * project whose configuration the session should adopt. Durable by\n * construction — it reads the same rows the session was created from, so it\n * survives restarts without any in-memory mapping.\n */\nexport async function resolveFactoryProjectForSession(args: {\n sourceControl: SourceControlStorageHandle;\n sessionId: string;\n}): Promise<{ factoryProjectId: string; orgId: string; userId: string } | null> {\n const { sourceControl, sessionId } = args;\n\n const session = await sourceControl.sessions.getBySessionId(sessionId);\n if (!session) return null;\n const projectRepository = await sourceControl.projectRepositories.get({\n orgId: session.orgId,\n id: session.projectRepositoryId,\n });\n if (!projectRepository) return null;\n const connection = await sourceControl.connections.get({ orgId: session.orgId, id: projectRepository.connectionId });\n if (!connection) return null;\n\n return { factoryProjectId: connection.factoryProjectId, orgId: session.orgId, userId: session.userId };\n}\n\n/**\n * Create the source-control session a repo-backed factory run needs.\n *\n * `FactoryStartCoordinator.prepare` requires this record to already exist —\n * `resolveSourceSession` throws `Factory session not found` otherwise — so every\n * autonomous entry point has to produce one before it can start a run. This is\n * that step, in one place: the owner's connection on the factory project, one of\n * its linked repositories, and a session on the requested branch with the\n * repository's pinned or default branch as the base.\n *\n * The run is attributed to whoever connected the repository\n * (`connection.createdByUserId`), because an autonomous run has no interactive\n * user of its own.\n */\nexport async function ensureFactorySourceSession(\n args: EnsureFactorySourceSessionArgs,\n): Promise<EnsuredFactorySourceSession> {\n const { sourceControl, orgId, factoryProjectId, branch, repositorySlug } = args;\n\n const resolved = await resolveFactorySourceRepository({ sourceControl, orgId, factoryProjectId, repositorySlug });\n if (!resolved.found) {\n throw new Error(\n resolved.reason === 'connection'\n ? 'Factory source-control connection not found.'\n : 'Factory source-control repository not found.',\n );\n }\n\n const userId = resolved.connectedByUserId;\n const session = await sourceControl.sessions.create({\n sessionId: randomUUID(),\n projectRepositoryId: resolved.projectRepositoryId,\n orgId,\n userId,\n branch,\n baseBranch: resolved.baseBranch,\n });\n return {\n sessionId: session.sessionId,\n userId,\n projectRepositoryId: resolved.projectRepositoryId,\n branch: session.branch,\n baseBranch: resolved.baseBranch,\n };\n}\n\nexport interface HydrateFactorySessionArgs {\n orgId: string;\n userId: string;\n /** The factory project's default model. Without it the session keeps the SDK's built-in mode default. */\n defaultModelId?: string;\n /** Omitted when the storage domain is unavailable, in which case the session runs on memory defaults. */\n memorySettings?: MemorySettingsStorage;\n}\n\n/**\n * Apply a factory project's configuration to a freshly created session:\n * observational-memory settings, then the project's default model.\n *\n * Both steps are best-effort. A retired model id or an unreachable settings row\n * must not sink a run that is otherwise ready — the session simply keeps the\n * default it was created with, and the reason is logged.\n */\nexport async function hydrateFactorySession(session: FactorySession, args: HydrateFactorySessionArgs): Promise<void> {\n if (args.memorySettings) {\n try {\n const record = await args.memorySettings.get({ orgId: args.orgId, userId: args.userId });\n await applyStoredMemorySettings(session, record);\n } catch (error) {\n console.warn('[Factory Start] Failed to apply observational-memory settings', {\n error: error instanceof Error ? error.message : String(error),\n });\n }\n }\n if (args.defaultModelId) {\n try {\n await session.model.switch({ modelId: args.defaultModelId });\n } catch (error) {\n console.warn('[Factory Start] Failed to apply factory default model', {\n modelId: args.defaultModelId,\n error: error instanceof Error ? error.message : String(error),\n });\n }\n }\n}\n"],"mappings":";;;;;;;AAgBA,eAAsB,6BACpB,UACA,kBAC6B;CAC7B,IAAI,CAAC,YAAY,CAAC,kBAAkB,OAAO,KAAA;CAC3C,IAAI;EAEF,QAAO,MADe,SAAS,QAAQ,EAAE,IAAI,iBAAiB,CAAC,EAAA,EAC/C,kBAAkB,KAAA;CACpC,QAAQ;EACN;CACF;AACF;;;;;;;;AAkDA,eAAsB,+BAA+B,MAMV;CACzC,MAAM,EAAE,eAAe,OAAO,kBAAkB,mBAAmB;CAGnE,MAAM,cAAa,MADO,cAAc,YAAY,KAAK;EAAE;EAAO;CAAiB,CAAC,EAAA,CACrD,QAAO,cAAa,UAAU,kBAAkB,cAAc,aAAa;CAC1G,IAAI,WAAW,WAAW,GAAG,OAAO;EAAE,OAAO;EAAO,QAAQ;CAAa;CAMzE,KAAK,MAAM,cAAc,YAAY;EACnC,IAAI;EACJ,IAAI;GACF,MAAM,sBAAsB,MAAM,cAAc,oBAAoB,KAAK;IAAE;IAAO,cAAc,WAAW;GAAG,CAAC;GAO/G,YAAW,MANwB,QAAQ,IACzC,oBAAoB,IAA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AAW;EACtB,qBAAqB,SAAS;EAC9B;EACA;EACA;EACA,YAAY,SAAS;CACvB,CAAC;CACD,OAAO;EACL,WAAW,QAAQ;EACnB;EACA,qBAAqB,SAAS;EAC9B,QAAQ,QAAQ;EAChB,YAAY,SAAS;CACvB;AACF;;;;;;;;;AAmBA,eAAsB,sBAAsB,SAAyB,MAAgD;CACnH,IAAI,KAAK,gBACP,IAAI;EAEF,MAAM,0BAA0B,SAAS,MADpB,KAAK,eAAe,IAAI;GAAE,OAAO,KAAK;GAAO,QAAQ,KAAK;EAAO,CAAC,CACxC;CACjD,SAAS,OAAO;EACd,QAAQ,KAAK,iEAAiE,EAC5E,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,EAC9D,CAAC;CACH;CAEF,IAAI,KAAK,gBACP,IAAI;EACF,MAAM,QAAQ,MAAM,OAAO,EAAE,SAAS,KAAK,eAAe,CAAC;CAC7D,SAAS,OAAO;EACd,QAAQ,KAAK,yDAAyD;GACpE,SAAS,KAAK;GACd,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;EAC9D,CAAC;CACH;AAEJ"}
|
|
1
|
+
{"version":3,"file":"factory-session.js","names":[],"sources":["../../src/session/factory-session.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\n\nimport type { MastraCodeState } from '@mastra/code-sdk/schema';\nimport type { AgentController } from '@mastra/core/agent-controller';\n\nimport type { MemorySettingsStorage } from '../storage/domains/memory-settings/base.js';\nimport type { FactoryProjectsStorage } from '../storage/domains/projects/base.js';\nimport type { SourceControlStorageHandle } from '../storage/domains/source-control/base.js';\nimport { applyStoredMemorySettings } from './memory-settings-hydration.js';\n\ntype FactorySession = Awaited<ReturnType<AgentController<MastraCodeState>['createSession']>>;\n\n/**\n * Read the factory project's default model. Best-effort: a missing project or an\n * uninitialized storage domain means \"no default\", never a failed run.\n */\nexport async function resolveFactoryDefaultModelId(\n projects: FactoryProjectsStorage | undefined,\n factoryProjectId: string | undefined,\n): Promise<string | undefined> {\n if (!projects || !factoryProjectId) return undefined;\n try {\n const project = await projects.getById({ id: factoryProjectId });\n return project?.defaultModelId ?? undefined;\n } catch {\n return undefined;\n }\n}\n\nexport interface EnsureFactorySourceSessionArgs {\n /**\n * Storage handle of the integration that owns source control. Nothing here is\n * provider-specific: the connection is matched by the handle's own\n * `integrationId`, so GitHub, Slack-on-behalf-of-GitHub, or any future owner\n * all resolve through the same traversal.\n */\n sourceControl: SourceControlStorageHandle;\n orgId: string;\n factoryProjectId: string;\n branch: string;\n /** Pick a specific linked repository by slug. Defaults to the first linked repository. */\n repositorySlug?: string;\n}\n\nexport interface EnsuredFactorySourceSession {\n sessionId: string;\n userId: string;\n projectRepositoryId: string;\n branch: string;\n baseBranch: string;\n}\n\nexport interface ResolvedFactorySourceRepository {\n projectRepositoryId: string;\n /** The repository's pinned branch, else its default branch. */\n baseBranch: string;\n /** Who connected the repository. The attribution for runs with no interactive user. */\n connectedByUserId: string;\n}\n\n/**\n * Outcome of {@link resolveFactorySourceRepository}. A miss carries which step\n * failed: callers differ on whether that is an error (an autonomous run cannot\n * proceed) or a routine fallback (a chat integration drops to a chat-only\n * session), and the two steps fail for different reasons worth reporting apart.\n */\nexport type FactorySourceRepositoryResult =\n | ({ found: true } & ResolvedFactorySourceRepository)\n | { found: false; reason: 'connection' | 'repository' };\n\n/**\n * Resolve which repository a factory project's source-control runs act on: the\n * owner's connection on the project, then one of its linked repositories.\n *\n * The owner is whichever integration owns source control, matched by the\n * handle's own `integrationId` — nothing here is provider-specific.\n */\nexport async function resolveFactorySourceRepository(args: {\n sourceControl: SourceControlStorageHandle;\n orgId: string;\n factoryProjectId: string;\n /** Pick a specific linked repository by slug. Defaults to the first linked repository. */\n repositorySlug?: string;\n}): Promise<FactorySourceRepositoryResult> {\n const { sourceControl, orgId, factoryProjectId, repositorySlug } = args;\n\n const connections = await sourceControl.connections.list({ orgId, factoryProjectId });\n const candidates = connections.filter(candidate => candidate.integrationId === sourceControl.integrationId);\n if (candidates.length === 0) return { found: false, reason: 'connection' };\n\n // A project can carry stale connections: a provider-app reinstall leaves the\n // old connection pointing at an installation that no longer exists, and that\n // row can sit ahead of the healthy one. Try every candidate and skip the ones\n // that no longer resolve rather than failing on the first.\n for (const connection of candidates) {\n let resolved;\n try {\n const projectRepositories = await sourceControl.projectRepositories.list({ orgId, connectionId: connection.id });\n const resolvedRepositories = await Promise.all(\n projectRepositories.map(async projectRepository => ({\n projectRepository,\n repository: await sourceControl.repositories.get({ orgId, id: projectRepository.repositoryId }),\n })),\n );\n resolved = resolvedRepositories.find(\n candidate => candidate.repository && (!repositorySlug || candidate.repository.slug === repositorySlug),\n );\n } catch {\n // The connection no longer resolves (e.g. its installation was deleted).\n continue;\n }\n if (!resolved?.repository) continue;\n\n return {\n found: true,\n projectRepositoryId: resolved.projectRepository.id,\n baseBranch: resolved.projectRepository.branch ?? resolved.repository.defaultBranch,\n connectedByUserId: connection.createdByUserId,\n };\n }\n\n return { found: false, reason: 'repository' };\n}\n\n/**\n * Walk a Factory user-session id back to the project it belongs to.\n *\n * Repo-backed channel threads are keyed by their Factory session id, which is\n * the only handle a session-start hook gets. This turns that id back into the\n * project whose configuration the session should adopt. Durable by\n * construction — it reads the same rows the session was created from, so it\n * survives restarts without any in-memory mapping.\n */\nexport async function resolveFactoryProjectForSession(args: {\n sourceControl: SourceControlStorageHandle;\n sessionId: string;\n}): Promise<{ factoryProjectId: string; orgId: string; userId: string } | null> {\n const { sourceControl, sessionId } = args;\n\n const session = await sourceControl.sessions.getBySessionId(sessionId);\n if (!session) return null;\n const projectRepository = await sourceControl.projectRepositories.get({\n orgId: session.orgId,\n id: session.projectRepositoryId,\n });\n if (!projectRepository) return null;\n const connection = await sourceControl.connections.get({ orgId: session.orgId, id: projectRepository.connectionId });\n if (!connection) return null;\n\n return { factoryProjectId: connection.factoryProjectId, orgId: session.orgId, userId: session.userId };\n}\n\n/**\n * Create the source-control session a repo-backed factory run needs.\n *\n * `FactoryStartCoordinator.prepare` requires this record to already exist —\n * `resolveSourceSession` throws `Factory session not found` otherwise — so every\n * autonomous entry point has to produce one before it can start a run. This is\n * that step, in one place: the owner's connection on the factory project, one of\n * its linked repositories, and a session on the requested branch with the\n * repository's pinned or default branch as the base.\n *\n * The run is attributed to whoever connected the repository\n * (`connection.createdByUserId`), because an autonomous run has no interactive\n * user of its own.\n */\nexport async function ensureFactorySourceSession(\n args: EnsureFactorySourceSessionArgs,\n): Promise<EnsuredFactorySourceSession> {\n const { sourceControl, orgId, factoryProjectId, branch, repositorySlug } = args;\n\n const resolved = await resolveFactorySourceRepository({ sourceControl, orgId, factoryProjectId, repositorySlug });\n if (!resolved.found) {\n throw new Error(\n resolved.reason === 'connection'\n ? 'Factory source-control connection not found.'\n : 'Factory source-control repository not found.',\n );\n }\n\n const userId = resolved.connectedByUserId;\n const session = await sourceControl.sessions.create({\n sessionId: randomUUID(),\n projectRepositoryId: resolved.projectRepositoryId,\n orgId,\n userId,\n branch,\n baseBranch: resolved.baseBranch,\n visibility: 'org',\n });\n return {\n sessionId: session.sessionId,\n userId,\n projectRepositoryId: resolved.projectRepositoryId,\n branch: session.branch,\n baseBranch: resolved.baseBranch,\n };\n}\n\nexport interface HydrateFactorySessionArgs {\n orgId: string;\n userId: string;\n /** The factory project's default model. Without it the session keeps the SDK's built-in mode default. */\n defaultModelId?: string;\n /** Omitted when the storage domain is unavailable, in which case the session runs on memory defaults. */\n memorySettings?: MemorySettingsStorage;\n}\n\n/**\n * Apply a factory project's configuration to a freshly created session:\n * observational-memory settings, then the project's default model.\n *\n * Both steps are best-effort. A retired model id or an unreachable settings row\n * must not sink a run that is otherwise ready — the session simply keeps the\n * default it was created with, and the reason is logged.\n */\nexport async function hydrateFactorySession(session: FactorySession, args: HydrateFactorySessionArgs): Promise<void> {\n if (args.memorySettings) {\n try {\n const record = await args.memorySettings.get({ orgId: args.orgId, userId: args.userId });\n await applyStoredMemorySettings(session, record);\n } catch (error) {\n console.warn('[Factory Start] Failed to apply observational-memory settings', {\n error: error instanceof Error ? error.message : String(error),\n });\n }\n }\n if (args.defaultModelId) {\n try {\n await session.model.switch({ modelId: args.defaultModelId });\n } catch (error) {\n console.warn('[Factory Start] Failed to apply factory default model', {\n modelId: args.defaultModelId,\n error: error instanceof Error ? error.message : String(error),\n });\n }\n }\n}\n"],"mappings":";;;;;;;AAgBA,eAAsB,6BACpB,UACA,kBAC6B;CAC7B,IAAI,CAAC,YAAY,CAAC,kBAAkB,OAAO,KAAA;CAC3C,IAAI;EAEF,QAAO,MADe,SAAS,QAAQ,EAAE,IAAI,iBAAiB,CAAC,EAAA,EAC/C,kBAAkB,KAAA;CACpC,QAAQ;EACN;CACF;AACF;;;;;;;;AAkDA,eAAsB,+BAA+B,MAMV;CACzC,MAAM,EAAE,eAAe,OAAO,kBAAkB,mBAAmB;CAGnE,MAAM,cAAa,MADO,cAAc,YAAY,KAAK;EAAE;EAAO;CAAiB,CAAC,EAAA,CACrD,QAAO,cAAa,UAAU,kBAAkB,cAAc,aAAa;CAC1G,IAAI,WAAW,WAAW,GAAG,OAAO;EAAE,OAAO;EAAO,QAAQ;CAAa;CAMzE,KAAK,MAAM,cAAc,YAAY;EACnC,IAAI;EACJ,IAAI;GACF,MAAM,sBAAsB,MAAM,cAAc,oBAAoB,KAAK;IAAE;IAAO,cAAc,WAAW;GAAG,CAAC;GAO/G,YAAW,MANwB,QAAQ,IACzC,oBAAoB,IAAI,OAAM,uBAAsB;IAClD;IACA,YAAY,MAAM,cAAc,aAAa,IAAI;KAAE;KAAO,IAAI,kBAAkB;IAAa,CAAC;GAChG,EAAE,CACJ,EAAA,CACgC,MAC9B,cAAa,UAAU,eAAe,CAAC,kBAAkB,UAAU,WAAW,SAAS,eACzF;EACF,QAAQ;GAEN;EACF;EACA,IAAI,CAAC,UAAU,YAAY;EAE3B,OAAO;GACL,OAAO;GACP,qBAAqB,SAAS,kBAAkB;GAChD,YAAY,SAAS,kBAAkB,UAAU,SAAS,WAAW;GACrE,mBAAmB,WAA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,CAAC,CACxC;CACjD,SAAS,OAAO;EACd,QAAQ,KAAK,iEAAiE,EAC5E,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,EAC9D,CAAC;CACH;CAEF,IAAI,KAAK,gBACP,IAAI;EACF,MAAM,QAAQ,MAAM,OAAO,EAAE,SAAS,KAAK,eAAe,CAAC;CAC7D,SAAS,OAAO;EACd,QAAQ,KAAK,yDAAyD;GACpE,SAAS,KAAK;GACd,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;EAC9D,CAAC;CACH;AAEJ"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ActiveModelPackRecord, ModelPacksStorage } from '../storage/domains/model-packs/base.js';
|
|
2
|
+
import type { SourceControlStorageHandle } from '../storage/domains/source-control/base.js';
|
|
3
|
+
import type { WorkItemsStorage } from '../storage/domains/work-items/base.js';
|
|
4
|
+
export interface ModelPackApplicableSession {
|
|
5
|
+
mode: {
|
|
6
|
+
get(): string;
|
|
7
|
+
};
|
|
8
|
+
model: {
|
|
9
|
+
switch(args: {
|
|
10
|
+
modelId: string;
|
|
11
|
+
}): Promise<unknown>;
|
|
12
|
+
};
|
|
13
|
+
subagents: {
|
|
14
|
+
model: {
|
|
15
|
+
set(args: {
|
|
16
|
+
modelId: string;
|
|
17
|
+
agentType: string;
|
|
18
|
+
}): Promise<unknown>;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
thread: {
|
|
22
|
+
getSetting?(args: {
|
|
23
|
+
key: string;
|
|
24
|
+
}): Promise<unknown>;
|
|
25
|
+
setSetting(args: {
|
|
26
|
+
key: string;
|
|
27
|
+
value: unknown;
|
|
28
|
+
}): Promise<unknown>;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export interface ModelPackHydrationSession extends ModelPackApplicableSession {
|
|
32
|
+
readonly identity: {
|
|
33
|
+
getResourceId(): string;
|
|
34
|
+
};
|
|
35
|
+
state: {
|
|
36
|
+
get(): Record<string, unknown> | undefined;
|
|
37
|
+
};
|
|
38
|
+
thread: ModelPackApplicableSession['thread'] & {
|
|
39
|
+
getId(): string | null | undefined;
|
|
40
|
+
getSetting(args: {
|
|
41
|
+
key: string;
|
|
42
|
+
}): Promise<unknown>;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export declare function applyActiveModelPack(session: ModelPackApplicableSession, activePack: Pick<ActiveModelPackRecord, 'packId' | 'models'>): Promise<void>;
|
|
46
|
+
export interface ModelPackHydrationDependencies {
|
|
47
|
+
sourceControl: {
|
|
48
|
+
sessions: Pick<SourceControlStorageHandle['sessions'], 'getBySessionId'>;
|
|
49
|
+
};
|
|
50
|
+
workItems: Pick<WorkItemsStorage, 'findActiveRunBindingByThread'>;
|
|
51
|
+
modelPacks: Pick<ModelPacksStorage, 'getActive'>;
|
|
52
|
+
}
|
|
53
|
+
/** Seed the user's default pack unless the interactive thread already selected its own pack. */
|
|
54
|
+
export declare function hydrateSessionModelPack(session: ModelPackHydrationSession, { sourceControl, workItems, modelPacks }: ModelPackHydrationDependencies): Promise<void>;
|
|
55
|
+
//# sourceMappingURL=model-pack-hydration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-pack-hydration.d.ts","sourceRoot":"","sources":["../../src/session/model-pack-hydration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;AACvG,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,2CAA2C,CAAC;AAC5F,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uCAAuC,CAAC;AAE9E,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE;QAAE,GAAG,IAAI,MAAM,CAAA;KAAE,CAAC;IACxB,KAAK,EAAE;QAAE,MAAM,CAAC,IAAI,EAAE;YAAE,OAAO,EAAE,MAAM,CAAA;SAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAA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ACF,SAAS,EAAE,IAAI,CAAC,gBAAgB,EAAE,8BAA8B,CAAC,CAAC;IAClE,UAAU,EAAE,IAAI,CAAC,iBAAiB,EAAE,WAAW,CAAC,CAAC;CAClD;AAED,gGAAgG;AAChG,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,yBAAyB,EAClC,EAAE,aAAa,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,8BAA8B,GACvE,OAAO,CAAC,IAAI,CAAC,CA+Bf"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//#region src/session/model-pack-hydration.ts
|
|
2
|
+
async function applyActiveModelPack(session, activePack) {
|
|
3
|
+
for (const [modeId, modelId] of Object.entries(activePack.models)) await session.thread.setSetting({
|
|
4
|
+
key: `modeModelId_${modeId}`,
|
|
5
|
+
value: modelId
|
|
6
|
+
});
|
|
7
|
+
const currentMode = session.mode.get();
|
|
8
|
+
const currentModeModel = currentMode === "build" || currentMode === "plan" || currentMode === "fast" ? activePack.models[currentMode] : void 0;
|
|
9
|
+
if (currentModeModel) await session.model.switch({ modelId: currentModeModel });
|
|
10
|
+
const subagentModels = [
|
|
11
|
+
["explore", activePack.models.fast],
|
|
12
|
+
["plan", activePack.models.plan],
|
|
13
|
+
["execute", activePack.models.build]
|
|
14
|
+
];
|
|
15
|
+
for (const [agentType, modelId] of subagentModels) await session.subagents.model.set({
|
|
16
|
+
modelId,
|
|
17
|
+
agentType
|
|
18
|
+
});
|
|
19
|
+
await session.thread.setSetting({
|
|
20
|
+
key: "activeModelPackId",
|
|
21
|
+
value: activePack.packId
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** Seed the user's default pack unless the interactive thread already selected its own pack. */
|
|
25
|
+
async function hydrateSessionModelPack(session, { sourceControl, workItems, modelPacks }) {
|
|
26
|
+
const resourceId = session.identity.getResourceId();
|
|
27
|
+
if (session.state.get()?.factoryProjectId || typeof session.thread.getSetting !== "function") return;
|
|
28
|
+
try {
|
|
29
|
+
const sourceSession = await sourceControl.sessions.getBySessionId(resourceId);
|
|
30
|
+
if (!sourceSession) return;
|
|
31
|
+
const threadId = session.thread.getId();
|
|
32
|
+
if (threadId && await workItems.findActiveRunBindingByThread({
|
|
33
|
+
orgId: sourceSession.orgId,
|
|
34
|
+
threadId,
|
|
35
|
+
resourceId,
|
|
36
|
+
sessionId: resourceId
|
|
37
|
+
})) return;
|
|
38
|
+
if ((await Promise.all([
|
|
39
|
+
session.thread.getSetting({ key: "activeModelPackId" }),
|
|
40
|
+
session.thread.getSetting({ key: "modeModelId_build" }),
|
|
41
|
+
session.thread.getSetting({ key: "modeModelId_plan" }),
|
|
42
|
+
session.thread.getSetting({ key: "modeModelId_fast" })
|
|
43
|
+
])).some((setting) => typeof setting === "string")) return;
|
|
44
|
+
const activePack = await modelPacks.getActive({
|
|
45
|
+
orgId: sourceSession.orgId,
|
|
46
|
+
userId: sourceSession.userId
|
|
47
|
+
});
|
|
48
|
+
if (activePack) await applyActiveModelPack(session, activePack);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.warn("[Factory model-pack hydration] Unable to apply the active model pack.", error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { applyActiveModelPack, hydrateSessionModelPack };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=model-pack-hydration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-pack-hydration.js","names":[],"sources":["../../src/session/model-pack-hydration.ts"],"sourcesContent":["import type { ActiveModelPackRecord, ModelPacksStorage } from '../storage/domains/model-packs/base.js';\nimport type { SourceControlStorageHandle } from '../storage/domains/source-control/base.js';\nimport type { WorkItemsStorage } from '../storage/domains/work-items/base.js';\n\nexport interface ModelPackApplicableSession {\n mode: { get(): string };\n model: { switch(args: { modelId: string }): Promise<unknown> };\n subagents: { model: { set(args: { modelId: string; agentType: string }): Promise<unknown> } };\n thread: {\n getSetting?(args: { key: string }): Promise<unknown>;\n setSetting(args: { key: string; value: unknown }): Promise<unknown>;\n };\n}\n\nexport interface ModelPackHydrationSession extends ModelPackApplicableSession {\n readonly identity: { getResourceId(): string };\n state: { get(): Record<string, unknown> | undefined };\n thread: ModelPackApplicableSession['thread'] & {\n getId(): string | null | undefined;\n getSetting(args: { key: string }): Promise<unknown>;\n };\n}\n\nexport async function applyActiveModelPack(\n session: ModelPackApplicableSession,\n activePack: Pick<ActiveModelPackRecord, 'packId' | 'models'>,\n): Promise<void> {\n for (const [modeId, modelId] of Object.entries(activePack.models)) {\n await session.thread.setSetting({ key: `modeModelId_${modeId}`, value: modelId });\n }\n\n const currentMode = session.mode.get();\n const currentModeModel =\n currentMode === 'build' || currentMode === 'plan' || currentMode === 'fast'\n ? activePack.models[currentMode]\n : undefined;\n if (currentModeModel) {\n await session.model.switch({ modelId: currentModeModel });\n }\n\n const subagentModels = [\n ['explore', activePack.models.fast],\n ['plan', activePack.models.plan],\n ['execute', activePack.models.build],\n ] as const;\n for (const [agentType, modelId] of subagentModels) {\n await session.subagents.model.set({ modelId, agentType });\n }\n\n await session.thread.setSetting({ key: 'activeModelPackId', value: activePack.packId });\n}\n\nexport interface ModelPackHydrationDependencies {\n sourceControl: {\n sessions: Pick<SourceControlStorageHandle['sessions'], 'getBySessionId'>;\n };\n workItems: Pick<WorkItemsStorage, 'findActiveRunBindingByThread'>;\n modelPacks: Pick<ModelPacksStorage, 'getActive'>;\n}\n\n/** Seed the user's default pack unless the interactive thread already selected its own pack. */\nexport async function hydrateSessionModelPack(\n session: ModelPackHydrationSession,\n { sourceControl, workItems, modelPacks }: ModelPackHydrationDependencies,\n): Promise<void> {\n const resourceId = session.identity.getResourceId();\n if (session.state.get()?.factoryProjectId || typeof session.thread.getSetting !== 'function') return;\n try {\n const sourceSession = await sourceControl.sessions.getBySessionId(resourceId);\n if (!sourceSession) return;\n const threadId = session.thread.getId();\n if (\n threadId &&\n (await workItems.findActiveRunBindingByThread({\n orgId: sourceSession.orgId,\n threadId,\n resourceId,\n sessionId: resourceId,\n }))\n ) {\n return;\n }\n const existingThreadSettings = await Promise.all([\n session.thread.getSetting({ key: 'activeModelPackId' }),\n session.thread.getSetting({ key: 'modeModelId_build' }),\n session.thread.getSetting({ key: 'modeModelId_plan' }),\n session.thread.getSetting({ key: 'modeModelId_fast' }),\n ]);\n if (existingThreadSettings.some(setting => typeof setting === 'string')) return;\n\n const activePack = await modelPacks.getActive({ orgId: sourceSession.orgId, userId: sourceSession.userId });\n if (activePack) await applyActiveModelPack(session, activePack);\n } catch (error) {\n console.warn('[Factory model-pack hydration] Unable to apply the active model pack.', error);\n }\n}\n"],"mappings":";AAuBA,eAAsB,qBACpB,SACA,YACe;CACf,KAAK,MAAM,CAAC,QAAQ,YAAY,OAAO,QAAQ,WAAW,MAAM,GAC9D,MAAM,QAAQ,OAAO,WAAW;EAAE,KAAK,eAAe;EAAU,OAAO;CAAQ,CAAC;CAGlF,MAAM,cAAc,QAAQ,KAAK,IAAI;CACrC,MAAM,mBACJ,gBAAgB,WAAW,gBAAgB,UAAU,gBAAgB,SACjE,WAAW,OAAO,eAClB,KAAA;CACN,IAAI,kBACF,MAAM,QAAQ,MAAM,OAAO,EAAE,SAAS,iBAAiB,CAAC;CAG1D,MAAM,iBAAiB;EACrB,CAAC,WAAW,WAAW,OAAO,IAAI;EAClC,CAAC,QAAQ,WAAW,OAAO,IAAI;EAC/B,CAAC,WAAW,WAAW,OAAO,KAAK;CACrC;CACA,KAAK,MAAM,CAAC,WAAW,YAAY,gBACjC,MAAM,QAAQ,UAAU,MAAM,IAAI;EAAE;EAAS;CAAU,CAAC;CAG1D,MAAM,QAAQ,OAAO,WAAW;EAAE,KAAK;EAAqB,OAAO,WAAW;CAAO,CAAC;AACxF;;AAWA,eAAsB,wBACpB,SACA,EAAE,eAAe,WAAW,cACb;CACf,MAAM,aAAa,QAAQ,SAAS,cAAc;CAClD,IAAI,QAAQ,MAAM,IAAI,CAAC,EAAE,oBAAoB,OAAO,QAAQ,OAAO,eAAe,YAAY;CAC9F,IAAI;EACF,MAAM,gBAAgB,MAAM,cAAc,SAAS,eAAe,UAAU;EAC5E,IAAI,CAAC,eAAe;EACpB,MAAM,WAAW,QAAQ,OAAO,MAAM;EACtC,IACE,YACC,MAAM,UAAU,6BAA6B;GAC5C,OAAO,cAAc;GACrB;GACA;GACA,WAAW;EACb,CAAC,GAED;EAQF,KAAI,MANiC,QAAQ,IAAI;GAC/C,QAAQ,OAAO,WAAW,EAAE,KAAK,oBAAoB,CAAC;GACtD,QAAQ,OAAO,WAAW,EAAE,KAAK,oBAAoB,CAAC;GACtD,QAAQ,OAAO,WAAW,EAAE,KAAK,mBAAmB,CAAC;GACrD,QAAQ,OAAO,WAAW,EAAE,KAAK,mBAAmB,CAAC;EACvD,CAAC,EAAA,CAC0B,MAAK,YAAW,OAAO,YAAY,QAAQ,GAAG;EAEzE,MAAM,aAAa,MAAM,WAAW,UAAU;GAAE,OAAO,cAAc;GAAO,QAAQ,cAAc;EAAO,CAAC;EAC1G,IAAI,YAAY,MAAM,qBAAqB,SAAS,UAAU;CAChE,SAAS,OAAO;EACd,QAAQ,KAAK,yEAAyE,KAAK;CAC7F;AACF"}
|
package/dist/skills/service.d.ts
CHANGED
|
@@ -36,6 +36,15 @@ export declare class SkillInvocationError extends Error {
|
|
|
36
36
|
readonly code: 'session_not_found' | 'skill_not_found';
|
|
37
37
|
constructor(code: SkillInvocationError['code'], message: string);
|
|
38
38
|
}
|
|
39
|
+
/** Kicks a run off from a plain prompt, for runs that activate no skill. */
|
|
40
|
+
export declare function resolvePromptInvocation(controller: Pick<AgentController<MastraCodeState>, 'getSessionByResource'>, input: {
|
|
41
|
+
resourceId: string;
|
|
42
|
+
scope?: SkillInvocationInput['scope'];
|
|
43
|
+
prompt: string;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
session: SkillSession;
|
|
46
|
+
message: string;
|
|
47
|
+
}>;
|
|
39
48
|
export declare function resolveSkillInvocation(controller: Pick<AgentController<MastraCodeState>, 'getSessionByResource'>, input: SkillInvocationInput): Promise<{
|
|
40
49
|
session: SkillSession;
|
|
41
50
|
skillName: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../src/skills/service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAEnE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAExD,MAAM,WAAW,oBAAoB;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,YAAY;IAC3B,YAAY,IAAI,SAAS,CAAC;IAC1B,WAAW,CAAC,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1D,sBAAsB,CACpB,KAAK,EAAE;QACL,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,CAAC;QAChD,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,EACD,OAAO,CAAC,EAAE;QACR,QAAQ,CAAC,EAAE;YAAE,QAAQ,CAAC,EAAE,SAAS,GAAG,SAAS,CAAA;SAAE,CAAC;QAChD,MAAM,CAAC,EAAE;YAAE,QAAQ,C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AAU,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACnF,OAAO,CAAC;IAAE,OAAO,EAAE,YAAY,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAIrD;AAED,wBAAsB,sBAAsB,CAC1C,UAAU,EAAE,IAAI,CAAC,eAAe,CAAC,eAAe,CAAC,EAAE,sBAAsB,CAAC,EAC1E,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC;IAAE,OAAO,EAAE,YAAY,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAkBxE;AAED,wBAAsB,uBAAuB,CAC3C,UAAU,EAAE,IAAI,CAAC,eAAe,CAAC,eAAe,CAAC,EAAE,sBAAsB,CAAC,EAC1E,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAIjD"}
|
package/dist/skills/service.js
CHANGED
|
@@ -11,6 +11,15 @@ var SkillInvocationError = class extends Error {
|
|
|
11
11
|
function escapeSkillBoundary(value) {
|
|
12
12
|
return value.replaceAll("</skill>", "</skill>");
|
|
13
13
|
}
|
|
14
|
+
/** Kicks a run off from a plain prompt, for runs that activate no skill. */
|
|
15
|
+
async function resolvePromptInvocation(controller, input) {
|
|
16
|
+
const session = await controller.getSessionByResource(input.resourceId, input.scope);
|
|
17
|
+
if (!session) throw new SkillInvocationError("session_not_found", "Agent controller session not found.");
|
|
18
|
+
return {
|
|
19
|
+
session,
|
|
20
|
+
message: input.prompt
|
|
21
|
+
};
|
|
22
|
+
}
|
|
14
23
|
async function resolveSkillInvocation(controller, input) {
|
|
15
24
|
const session = await controller.getSessionByResource(input.resourceId, input.scope);
|
|
16
25
|
if (!session) throw new SkillInvocationError("session_not_found", "Agent controller session not found.");
|
|
@@ -35,6 +44,6 @@ async function dispatchSkillInvocation(controller, input) {
|
|
|
35
44
|
};
|
|
36
45
|
}
|
|
37
46
|
//#endregion
|
|
38
|
-
export { SkillInvocationError, dispatchSkillInvocation, resolveSkillInvocation };
|
|
47
|
+
export { SkillInvocationError, dispatchSkillInvocation, resolvePromptInvocation, resolveSkillInvocation };
|
|
39
48
|
|
|
40
49
|
//# sourceMappingURL=service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","names":[],"sources":["../../src/skills/service.ts"],"sourcesContent":["import type { MastraCodeState } from '@mastra/code-sdk/schema';\nimport type { SendAgentSignalResult } from '@mastra/core/agent';\nimport type { AgentController } from '@mastra/core/agent-controller';\nimport type { RequestContext } from '@mastra/core/request-context';\nimport { formatSkillActivation } from '@mastra/core/workspace';\nimport type { Workspace } from '@mastra/core/workspace';\n\nexport interface SkillInvocationInput {\n resourceId: string;\n scope?: string;\n name: string;\n arguments?: string;\n}\n\nexport interface SkillSession {\n getWorkspace(): Workspace;\n sendMessage(input: { content: string }): Promise<unknown>;\n sendNotificationSignal(\n input: {\n source: string;\n kind: string;\n summary: string;\n priority?: 'low' | 'medium' | 'high' | 'urgent';\n payload?: unknown;\n dedupeKey?: string;\n sourceId?: string;\n },\n options?: {\n ifActive?: { behavior?: 'deliver' | 'persist' };\n ifIdle?: { behavior?: 'persist' | 'wake' };\n requestContext?: RequestContext;\n },\n ): Promise<Pick<SendAgentSignalResult, 'persisted' | 'accepted'>>;\n}\n\nexport class SkillInvocationError extends Error {\n readonly code: 'session_not_found' | 'skill_not_found';\n\n constructor(code: SkillInvocationError['code'], message: string) {\n super(message);\n this.name = 'SkillInvocationError';\n this.code = code;\n }\n}\n\nfunction escapeSkillBoundary(value: string): string {\n return value.replaceAll('</skill>', '</skill>');\n}\n\nexport async function resolveSkillInvocation(\n controller: Pick<AgentController<MastraCodeState>, 'getSessionByResource'>,\n input: SkillInvocationInput,\n): Promise<{ session: SkillSession; skillName: string; message: string }> {\n const session = (await controller.getSessionByResource(input.resourceId, input.scope)) as SkillSession | undefined;\n if (!session) throw new SkillInvocationError('session_not_found', 'Agent controller session not found.');\n\n const skills = session.getWorkspace().skills;\n await skills?.maybeRefresh();\n const skill = await skills?.get(input.name);\n if (!skill || skill['user-invocable'] === false) {\n throw new SkillInvocationError('skill_not_found', `Skill not found: ${input.name}.`);\n }\n\n const args = input.arguments?.trim();\n const content = `${formatSkillActivation(skill)}${args ? `\\n\\nARGUMENTS: ${args}` : ''}`.trim();\n return {\n session,\n skillName: skill.name,\n message: `<skill name=\"${skill.name}\">\\n${escapeSkillBoundary(content)}\\n</skill>`,\n };\n}\n\nexport async function dispatchSkillInvocation(\n controller: Pick<AgentController<MastraCodeState>, 'getSessionByResource'>,\n input: SkillInvocationInput,\n): Promise<{ skillName: string; message: string }> {\n const resolved = await resolveSkillInvocation(controller, input);\n await resolved.session.sendMessage({ content: resolved.message });\n return { skillName: resolved.skillName, message: resolved.message };\n}\n"],"mappings":";;AAmCA,IAAa,uBAAb,cAA0C,MAAM;CAC9C;CAEA,YAAY,MAAoC,SAAiB;EAC/D,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;CACd;AACF;AAEA,SAAS,oBAAoB,OAAuB;CAClD,OAAO,MAAM,WAAW,YAAY,gBAAgB;AACtD;AAEA,eAAsB,uBACpB,YACA,OACwE;CACxE,MAAM,UAAW,MAAM,WAAW,qBAAqB,MAAM,YAAY,MAAM,KAAK;CACpF,IAAI,CAAC,SAAS,MAAM,IAAI,qBAAqB,qBAAqB,qCAAqC;CAEvG,MAAM,SAAS,QAAQ,aAAa,CAAC,CAAC;CACtC,MAAM,QAAQ,aAAa;CAC3B,MAAM,QAAQ,MAAM,QAAQ,IAAI,MAAM,IAAI;CAC1C,IAAI,CAAC,SAAS,MAAM,sBAAsB,OACxC,MAAM,IAAI,qBAAqB,mBAAmB,oBAAoB,MAAM,KAAK,EAAE;CAGrF,MAAM,OAAO,MAAM,WAAW,KAAK;CACnC,MAAM,UAAU,GAAG,sBAAsB,KAAK,IAAI,OAAO,kBAAkB,SAAS,KAAK,KAAK;CAC9F,OAAO;EACL;EACA,WAAW,MAAM;EACjB,SAAS,gBAAgB,MAAM,KAAK,MAAM,oBAAoB,OAAO,EAAE;CACzE;AACF;AAEA,eAAsB,wBACpB,YACA,OACiD;CACjD,MAAM,WAAW,MAAM,uBAAuB,YAAY,KAAK;CAC/D,MAAM,SAAS,QAAQ,YAAY,EAAE,SAAS,SAAS,QAAQ,CAAC;CAChE,OAAO;EAAE,WAAW,SAAS;EAAW,SAAS,SAAS;CAAQ;AACpE"}
|
|
1
|
+
{"version":3,"file":"service.js","names":[],"sources":["../../src/skills/service.ts"],"sourcesContent":["import type { MastraCodeState } from '@mastra/code-sdk/schema';\nimport type { SendAgentSignalResult } from '@mastra/core/agent';\nimport type { AgentController } from '@mastra/core/agent-controller';\nimport type { RequestContext } from '@mastra/core/request-context';\nimport { formatSkillActivation } from '@mastra/core/workspace';\nimport type { Workspace } from '@mastra/core/workspace';\n\nexport interface SkillInvocationInput {\n resourceId: string;\n scope?: string;\n name: string;\n arguments?: string;\n}\n\nexport interface SkillSession {\n getWorkspace(): Workspace;\n sendMessage(input: { content: string }): Promise<unknown>;\n sendNotificationSignal(\n input: {\n source: string;\n kind: string;\n summary: string;\n priority?: 'low' | 'medium' | 'high' | 'urgent';\n payload?: unknown;\n dedupeKey?: string;\n sourceId?: string;\n },\n options?: {\n ifActive?: { behavior?: 'deliver' | 'persist' };\n ifIdle?: { behavior?: 'persist' | 'wake' };\n requestContext?: RequestContext;\n },\n ): Promise<Pick<SendAgentSignalResult, 'persisted' | 'accepted'>>;\n}\n\nexport class SkillInvocationError extends Error {\n readonly code: 'session_not_found' | 'skill_not_found';\n\n constructor(code: SkillInvocationError['code'], message: string) {\n super(message);\n this.name = 'SkillInvocationError';\n this.code = code;\n }\n}\n\nfunction escapeSkillBoundary(value: string): string {\n return value.replaceAll('</skill>', '</skill>');\n}\n\n/** Kicks a run off from a plain prompt, for runs that activate no skill. */\nexport async function resolvePromptInvocation(\n controller: Pick<AgentController<MastraCodeState>, 'getSessionByResource'>,\n input: { resourceId: string; scope?: SkillInvocationInput['scope']; prompt: string },\n): Promise<{ session: SkillSession; message: string }> {\n const session = (await controller.getSessionByResource(input.resourceId, input.scope)) as SkillSession | undefined;\n if (!session) throw new SkillInvocationError('session_not_found', 'Agent controller session not found.');\n return { session, message: input.prompt };\n}\n\nexport async function resolveSkillInvocation(\n controller: Pick<AgentController<MastraCodeState>, 'getSessionByResource'>,\n input: SkillInvocationInput,\n): Promise<{ session: SkillSession; skillName: string; message: string }> {\n const session = (await controller.getSessionByResource(input.resourceId, input.scope)) as SkillSession | undefined;\n if (!session) throw new SkillInvocationError('session_not_found', 'Agent controller session not found.');\n\n const skills = session.getWorkspace().skills;\n await skills?.maybeRefresh();\n const skill = await skills?.get(input.name);\n if (!skill || skill['user-invocable'] === false) {\n throw new SkillInvocationError('skill_not_found', `Skill not found: ${input.name}.`);\n }\n\n const args = input.arguments?.trim();\n const content = `${formatSkillActivation(skill)}${args ? `\\n\\nARGUMENTS: ${args}` : ''}`.trim();\n return {\n session,\n skillName: skill.name,\n message: `<skill name=\"${skill.name}\">\\n${escapeSkillBoundary(content)}\\n</skill>`,\n };\n}\n\nexport async function dispatchSkillInvocation(\n controller: Pick<AgentController<MastraCodeState>, 'getSessionByResource'>,\n input: SkillInvocationInput,\n): Promise<{ skillName: string; message: string }> {\n const resolved = await resolveSkillInvocation(controller, input);\n await resolved.session.sendMessage({ content: resolved.message });\n return { skillName: resolved.skillName, message: resolved.message };\n}\n"],"mappings":";;AAmCA,IAAa,uBAAb,cAA0C,MAAM;CAC9C;CAEA,YAAY,MAAoC,SAAiB;EAC/D,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;CACd;AACF;AAEA,SAAS,oBAAoB,OAAuB;CAClD,OAAO,MAAM,WAAW,YAAY,gBAAgB;AACtD;;AAGA,eAAsB,wBACpB,YACA,OACqD;CACrD,MAAM,UAAW,MAAM,WAAW,qBAAqB,MAAM,YAAY,MAAM,KAAK;CACpF,IAAI,CAAC,SAAS,MAAM,IAAI,qBAAqB,qBAAqB,qCAAqC;CACvG,OAAO;EAAE;EAAS,SAAS,MAAM;CAAO;AAC1C;AAEA,eAAsB,uBACpB,YACA,OACwE;CACxE,MAAM,UAAW,MAAM,WAAW,qBAAqB,MAAM,YAAY,MAAM,KAAK;CACpF,IAAI,CAAC,SAAS,MAAM,IAAI,qBAAqB,qBAAqB,qCAAqC;CAEvG,MAAM,SAAS,QAAQ,aAAa,CAAC,CAAC;CACtC,MAAM,QAAQ,aAAa;CAC3B,MAAM,QAAQ,MAAM,QAAQ,IAAI,MAAM,IAAI;CAC1C,IAAI,CAAC,SAAS,MAAM,sBAAsB,OACxC,MAAM,IAAI,qBAAqB,mBAAmB,oBAAoB,MAAM,KAAK,EAAE;CAGrF,MAAM,OAAO,MAAM,WAAW,KAAK;CACnC,MAAM,UAAU,GAAG,sBAAsB,KAAK,IAAI,OAAO,kBAAkB,SAAS,KAAK,KAAK;CAC9F,OAAO;EACL;EACA,WAAW,MAAM;EACjB,SAAS,gBAAgB,MAAM,KAAK,MAAM,oBAAoB,OAAO,EAAE;CACzE;AACF;AAEA,eAAsB,wBACpB,YACA,OACiD;CACjD,MAAM,WAAW,MAAM,uBAAuB,YAAY,KAAK;CAC/D,MAAM,SAAS,QAAQ,YAAY,EAAE,SAAS,SAAS,QAAQ,CAAC;CAChE,OAAO;EAAE,WAAW,SAAS;EAAW,SAAS,SAAS;CAAQ;AACpE"}
|
|
@@ -22,7 +22,19 @@ export interface UpsertModelPackInput {
|
|
|
22
22
|
fast: string;
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
|
+
export interface ActiveModelPackRecord {
|
|
26
|
+
orgId: string;
|
|
27
|
+
userId: string;
|
|
28
|
+
packId: string;
|
|
29
|
+
models: {
|
|
30
|
+
build: string;
|
|
31
|
+
plan: string;
|
|
32
|
+
fast: string;
|
|
33
|
+
};
|
|
34
|
+
updatedAt: Date;
|
|
35
|
+
}
|
|
25
36
|
export declare const MODEL_PACKS_SCHEMA: CollectionSchema;
|
|
37
|
+
export declare const ACTIVE_MODEL_PACKS_SCHEMA: CollectionSchema;
|
|
26
38
|
export declare class ModelPacksStorage extends FactoryStorageDomain {
|
|
27
39
|
#private;
|
|
28
40
|
constructor();
|
|
@@ -41,6 +53,20 @@ export declare class ModelPacksStorage extends FactoryStorageDomain {
|
|
|
41
53
|
orgId: string;
|
|
42
54
|
id: string;
|
|
43
55
|
}): Promise<ModelPackRecord | null>;
|
|
56
|
+
setActive({ orgId, userId, packId, models, }: {
|
|
57
|
+
orgId: string;
|
|
58
|
+
userId: string;
|
|
59
|
+
packId: string;
|
|
60
|
+
models: ActiveModelPackRecord['models'];
|
|
61
|
+
}): Promise<ActiveModelPackRecord>;
|
|
62
|
+
getActive({ orgId, userId }: {
|
|
63
|
+
orgId: string;
|
|
64
|
+
userId: string;
|
|
65
|
+
}): Promise<ActiveModelPackRecord | null>;
|
|
66
|
+
clearActive({ orgId, userId }: {
|
|
67
|
+
orgId: string;
|
|
68
|
+
userId: string;
|
|
69
|
+
}): Promise<boolean>;
|
|
44
70
|
delete({ orgId, id }: {
|
|
45
71
|
orgId: string;
|
|
46
72
|
id: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../src/storage/domains/model-packs/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../src/storage/domains/model-packs/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAwB,MAAM,sBAAsB,CAAC;AAClF,OAAO,KAAK,EAAE,gBAAgB,EAAqB,MAAM,sBAAsB,CAAC;AAEhF,2EAA2E;AAC3E,MAAM,WAAW,eAAe;IAC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AAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAS9D,GAAG,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IAKlF,SAAS,CAAC,EACd,KAAK,EACL,MAAM,EACN,MAAM,EACN,MAAM,GACP,EAAE;QACD,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,qBAAqB,CAAC,QAAQ,CAAC,CAAC;KACzC,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAmC5B,SAAS,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,qBAAqB,GAAG,IAAI,CAAC;IAQtG,WAAW,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAInF,MAAM,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC;CAO7E"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FactoryStorageDomain } from "@mastra/core/storage";
|
|
1
|
+
import { FactoryStorageDomain, UniqueViolationError } from "@mastra/core/storage";
|
|
2
2
|
//#region src/storage/domains/model-packs/base.ts
|
|
3
3
|
const MODEL_PACKS_SCHEMA = {
|
|
4
4
|
name: "model_packs",
|
|
@@ -18,6 +18,23 @@ const MODEL_PACKS_SCHEMA = {
|
|
|
18
18
|
columns: ["org_id", "name"]
|
|
19
19
|
}]
|
|
20
20
|
};
|
|
21
|
+
const ACTIVE_MODEL_PACKS_SCHEMA = {
|
|
22
|
+
name: "active_model_packs",
|
|
23
|
+
columns: {
|
|
24
|
+
id: { type: "uuid-pk" },
|
|
25
|
+
org_id: { type: "text" },
|
|
26
|
+
user_id: { type: "text" },
|
|
27
|
+
pack_id: { type: "text" },
|
|
28
|
+
build_model_id: { type: "text" },
|
|
29
|
+
plan_model_id: { type: "text" },
|
|
30
|
+
fast_model_id: { type: "text" },
|
|
31
|
+
updated_at: { type: "timestamp" }
|
|
32
|
+
},
|
|
33
|
+
uniqueIndexes: [{
|
|
34
|
+
name: "active_model_packs_org_user_key",
|
|
35
|
+
columns: ["org_id", "user_id"]
|
|
36
|
+
}]
|
|
37
|
+
};
|
|
21
38
|
function toModelPack(row) {
|
|
22
39
|
return {
|
|
23
40
|
id: row.id,
|
|
@@ -33,14 +50,28 @@ function toModelPack(row) {
|
|
|
33
50
|
updatedAt: row.updated_at
|
|
34
51
|
};
|
|
35
52
|
}
|
|
53
|
+
function toActiveModelPack(row) {
|
|
54
|
+
return {
|
|
55
|
+
orgId: row.org_id,
|
|
56
|
+
userId: row.user_id,
|
|
57
|
+
packId: row.pack_id,
|
|
58
|
+
models: {
|
|
59
|
+
build: row.build_model_id,
|
|
60
|
+
plan: row.plan_model_id,
|
|
61
|
+
fast: row.fast_model_id
|
|
62
|
+
},
|
|
63
|
+
updatedAt: row.updated_at
|
|
64
|
+
};
|
|
65
|
+
}
|
|
36
66
|
var ModelPacksStorage = class extends FactoryStorageDomain {
|
|
37
67
|
constructor() {
|
|
38
68
|
super("model-packs");
|
|
39
69
|
}
|
|
40
70
|
async init() {
|
|
41
|
-
await this.ensureCollections([MODEL_PACKS_SCHEMA]);
|
|
71
|
+
await this.ensureCollections([MODEL_PACKS_SCHEMA, ACTIVE_MODEL_PACKS_SCHEMA]);
|
|
42
72
|
}
|
|
43
73
|
async dangerouslyClearAll() {
|
|
74
|
+
await this.ops.deleteMany("active_model_packs", {});
|
|
44
75
|
await this.ops.deleteMany("model_packs", {});
|
|
45
76
|
}
|
|
46
77
|
get #db() {
|
|
@@ -53,15 +84,27 @@ var ModelPacksStorage = class extends FactoryStorageDomain {
|
|
|
53
84
|
org_id: orgId,
|
|
54
85
|
name: input.name
|
|
55
86
|
});
|
|
56
|
-
if (existing)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
87
|
+
if (existing) {
|
|
88
|
+
const row = await this.#db.updateAtomic("model_packs", {
|
|
89
|
+
org_id: orgId,
|
|
90
|
+
id: existing.id
|
|
91
|
+
}, () => ({
|
|
92
|
+
build_model_id: input.models.build,
|
|
93
|
+
plan_model_id: input.models.plan,
|
|
94
|
+
fast_model_id: input.models.fast,
|
|
95
|
+
updated_at: now
|
|
96
|
+
}));
|
|
97
|
+
await this.#db.updateMany("active_model_packs", {
|
|
98
|
+
org_id: orgId,
|
|
99
|
+
pack_id: `custom:${existing.id}`
|
|
100
|
+
}, {
|
|
101
|
+
build_model_id: input.models.build,
|
|
102
|
+
plan_model_id: input.models.plan,
|
|
103
|
+
fast_model_id: input.models.fast,
|
|
104
|
+
updated_at: now
|
|
105
|
+
});
|
|
106
|
+
return toModelPack(row ?? existing);
|
|
107
|
+
}
|
|
65
108
|
return toModelPack(await this.#db.insertOne("model_packs", {
|
|
66
109
|
org_id: orgId,
|
|
67
110
|
created_by: userId,
|
|
@@ -83,14 +126,60 @@ var ModelPacksStorage = class extends FactoryStorageDomain {
|
|
|
83
126
|
});
|
|
84
127
|
return row ? toModelPack(row) : null;
|
|
85
128
|
}
|
|
129
|
+
async setActive({ orgId, userId, packId, models }) {
|
|
130
|
+
const now = /* @__PURE__ */ new Date();
|
|
131
|
+
const values = {
|
|
132
|
+
pack_id: packId,
|
|
133
|
+
build_model_id: models.build,
|
|
134
|
+
plan_model_id: models.plan,
|
|
135
|
+
fast_model_id: models.fast,
|
|
136
|
+
updated_at: now
|
|
137
|
+
};
|
|
138
|
+
const updateExisting = () => this.#db.updateAtomic("active_model_packs", {
|
|
139
|
+
org_id: orgId,
|
|
140
|
+
user_id: userId
|
|
141
|
+
}, () => values);
|
|
142
|
+
const updated = await updateExisting();
|
|
143
|
+
if (updated) return toActiveModelPack(updated);
|
|
144
|
+
try {
|
|
145
|
+
return toActiveModelPack(await this.#db.insertOne("active_model_packs", {
|
|
146
|
+
org_id: orgId,
|
|
147
|
+
user_id: userId,
|
|
148
|
+
...values
|
|
149
|
+
}));
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (!(error instanceof UniqueViolationError)) throw error;
|
|
152
|
+
const row = await updateExisting();
|
|
153
|
+
if (!row) throw error;
|
|
154
|
+
return toActiveModelPack(row);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async getActive({ orgId, userId }) {
|
|
158
|
+
const row = await this.#db.findOne("active_model_packs", {
|
|
159
|
+
org_id: orgId,
|
|
160
|
+
user_id: userId
|
|
161
|
+
});
|
|
162
|
+
return row ? toActiveModelPack(row) : null;
|
|
163
|
+
}
|
|
164
|
+
async clearActive({ orgId, userId }) {
|
|
165
|
+
return await this.#db.deleteMany("active_model_packs", {
|
|
166
|
+
org_id: orgId,
|
|
167
|
+
user_id: userId
|
|
168
|
+
}) > 0;
|
|
169
|
+
}
|
|
86
170
|
async delete({ orgId, id }) {
|
|
87
|
-
|
|
171
|
+
const deleted = await this.#db.deleteMany("model_packs", {
|
|
88
172
|
org_id: orgId,
|
|
89
173
|
id
|
|
90
174
|
}) > 0;
|
|
175
|
+
if (deleted) await this.#db.deleteMany("active_model_packs", {
|
|
176
|
+
org_id: orgId,
|
|
177
|
+
pack_id: `custom:${id}`
|
|
178
|
+
});
|
|
179
|
+
return deleted;
|
|
91
180
|
}
|
|
92
181
|
};
|
|
93
182
|
//#endregion
|
|
94
|
-
export { MODEL_PACKS_SCHEMA, ModelPacksStorage };
|
|
183
|
+
export { ACTIVE_MODEL_PACKS_SCHEMA, MODEL_PACKS_SCHEMA, ModelPacksStorage };
|
|
95
184
|
|
|
96
185
|
//# sourceMappingURL=base.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","names":["#db"],"sources":["../../../../src/storage/domains/model-packs/base.ts"],"sourcesContent":["import { FactoryStorageDomain } from '@mastra/core/storage';\nimport type { CollectionSchema, FactoryStorageOps } from '@mastra/core/storage';\n\n/** A saved custom model pack: one model per mode (build / plan / fast). */\nexport interface ModelPackRecord {\n id: string;\n orgId: string;\n createdBy: string;\n name: string;\n models: { build: string; plan: string; fast: string };\n createdAt: Date;\n updatedAt: Date;\n}\n\nexport interface UpsertModelPackInput {\n name: string;\n models: { build: string; plan: string; fast: string };\n}\n\nexport const MODEL_PACKS_SCHEMA: CollectionSchema = {\n name: 'model_packs',\n columns: {\n id: { type: 'uuid-pk' },\n org_id: { type: 'text' },\n created_by: { type: 'text' },\n name: { type: 'text' },\n build_model_id: { type: 'text' },\n plan_model_id: { type: 'text' },\n fast_model_id: { type: 'text' },\n created_at: { type: 'timestamp' },\n updated_at: { type: 'timestamp' },\n },\n indexes: [{ name: 'model_packs_org_name_idx', columns: ['org_id', 'name'] }],\n};\n\ninterface ModelPackDbRow extends Record<string, unknown> {\n id: string;\n org_id: string;\n created_by: string;\n name: string;\n build_model_id: string;\n plan_model_id: string;\n fast_model_id: string;\n created_at: Date;\n updated_at: Date;\n}\n\nfunction toModelPack(row: ModelPackDbRow): ModelPackRecord {\n return {\n id: row.id,\n orgId: row.org_id,\n createdBy: row.created_by,\n name: row.name,\n models: { build: row.build_model_id, plan: row.plan_model_id, fast: row.fast_model_id },\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n}\n\nexport class ModelPacksStorage extends FactoryStorageDomain {\n constructor() {\n super('model-packs');\n }\n\n async init(): Promise<void> {\n await this.ensureCollections([MODEL_PACKS_SCHEMA]);\n }\n\n async dangerouslyClearAll(): Promise<void> {\n await this.ops.deleteMany('model_packs', {});\n }\n\n get #db(): FactoryStorageOps {\n return this.ops;\n }\n\n /** Create or replace a pack by `(orgId, name)` — mirrors the settings.json upsert semantics. */\n async upsert({\n orgId,\n userId,\n input,\n }: {\n orgId: string;\n userId: string;\n input: UpsertModelPackInput;\n }): Promise<ModelPackRecord> {\n const now = new Date();\n const existing = await this.#db.findOne<ModelPackDbRow>('model_packs', { org_id: orgId, name: input.name });\n if (existing) {\n const row = await this.#db.updateAtomic<ModelPackDbRow>(\n 'model_packs',\n { org_id: orgId, id: existing.id },\n () => ({\n build_model_id: input.models.build,\n plan_model_id: input.models.plan,\n fast_model_id: input.models.fast,\n updated_at: now,\n }),\n );\n return toModelPack(row ?? existing);\n }\n const row = await this.#db.insertOne<ModelPackDbRow>('model_packs', {\n org_id: orgId,\n created_by: userId,\n name: input.name,\n build_model_id: input.models.build,\n plan_model_id: input.models.plan,\n fast_model_id: input.models.fast,\n created_at: now,\n updated_at: now,\n });\n return toModelPack(row);\n }\n\n async list({ orgId }: { orgId: string }): Promise<ModelPackRecord[]> {\n const rows = await this.#db.findMany<ModelPackDbRow>(\n 'model_packs',\n { org_id: orgId },\n { orderBy: [['name', 'asc']] },\n );\n return rows.map(toModelPack);\n }\n\n async get({ orgId, id }: { orgId: string; id: string }): Promise<ModelPackRecord | null> {\n const row = await this.#db.findOne<ModelPackDbRow>('model_packs', { org_id: orgId, id });\n return row ? toModelPack(row) : null;\n }\n\n async delete({ orgId, id }: { orgId: string; id: string }): Promise<boolean> {\n return (await this.#db.deleteMany('model_packs', { org_id: orgId, id })) > 0;\n }\n}\n"],"mappings":";;AAmBA,MAAa,qBAAuC;CAClD,MAAM;CACN,SAAS;EACP,IAAI,EAAE,MAAM,UAAU;EACtB,QAAQ,EAAE,MAAM,OAAO;EACvB,YAAY,EAAE,MAAM,OAAO;EAC3B,MAAM,EAAE,MAAM,OAAO;EACrB,gBAAgB,EAAE,MAAM,OAAO;EAC/B,eAAe,EA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M,OAAO;GAC5B,YAAY;GACZ,YAAY;EACd,CAAC,CACqB;CACxB;CAEA,MAAM,KAAK,EAAE,SAAwD;EAMnE,QAAO,MALY,KAAKA,IAAI,SAC1B,eACA,EAAE,QAAQ,MAAM,GAChB,EAAE,SAAS,CAAC,CAAC,QAAQ,KAAK,CAAC,EAAE,CAC/B,EAAA,CACY,IAAI,WAAW;CAC7B;CAEA,MAAM,IAAI,EAAE,OAAO,MAAsE;EACvF,MAAM,MAAM,MAAM,KAAKA,IAAI,QAAwB,eAAe;GAAE,QAAQ;GAAO;EAAG,CAAC;EACvF,OAAO,MAAM,YAAY,GAAG,IAAI;CAClC;CAEA,MAAM,OAAO,EAAE,OAAO,MAAuD;EAC3E,OAAQ,MAAM,KAAKA,IAAI,WAAW,eAAe;GAAE,QAAQ;GAAO;EAAG,CAAC,IAAK;CAC7E;AACF"}
|
|
1
|
+
{"version":3,"file":"base.js","names":["#db"],"sources":["../../../../src/storage/domains/model-packs/base.ts"],"sourcesContent":["import { FactoryStorageDomain, UniqueViolationError } from '@mastra/core/storage';\nimport type { CollectionSchema, FactoryStorageOps } from '@mastra/core/storage';\n\n/** A saved custom model pack: one model per mode (build / plan / fast). */\nexport interface ModelPackRecord {\n id: string;\n orgId: string;\n createdBy: string;\n name: string;\n models: { build: string; plan: string; fast: string };\n createdAt: Date;\n updatedAt: Date;\n}\n\nexport interface UpsertModelPackInput {\n name: string;\n models: { build: string; plan: string; fast: string };\n}\n\nexport interface ActiveModelPackRecord {\n orgId: string;\n userId: string;\n packId: string;\n models: { build: string; plan: string; fast: string };\n updatedAt: Date;\n}\n\nexport const MODEL_PACKS_SCHEMA: CollectionSchema = {\n name: 'model_packs',\n columns: {\n id: { type: 'uuid-pk' },\n org_id: { type: 'text' },\n created_by: { type: 'text' },\n name: { type: 'text' },\n build_model_id: { type: 'text' },\n plan_model_id: { type: 'text' },\n fast_model_id: { type: 'text' },\n created_at: { type: 'timestamp' },\n updated_at: { type: 'timestamp' },\n },\n indexes: [{ name: 'model_packs_org_name_idx', columns: ['org_id', 'name'] }],\n};\n\nexport const ACTIVE_MODEL_PACKS_SCHEMA: CollectionSchema = {\n name: 'active_model_packs',\n columns: {\n id: { type: 'uuid-pk' },\n org_id: { type: 'text' },\n user_id: { type: 'text' },\n pack_id: { type: 'text' },\n build_model_id: { type: 'text' },\n plan_model_id: { type: 'text' },\n fast_model_id: { type: 'text' },\n updated_at: { type: 'timestamp' },\n },\n uniqueIndexes: [{ name: 'active_model_packs_org_user_key', columns: ['org_id', 'user_id'] }],\n};\n\ninterface ModelPackDbRow extends Record<string, unknown> {\n id: string;\n org_id: string;\n created_by: string;\n name: string;\n build_model_id: string;\n plan_model_id: string;\n fast_model_id: string;\n created_at: Date;\n updated_at: Date;\n}\n\ninterface ActiveModelPackDbRow extends Record<string, unknown> {\n id: string;\n org_id: string;\n user_id: string;\n pack_id: string;\n build_model_id: string;\n plan_model_id: string;\n fast_model_id: string;\n updated_at: Date;\n}\n\nfunction toModelPack(row: ModelPackDbRow): ModelPackRecord {\n return {\n id: row.id,\n orgId: row.org_id,\n createdBy: row.created_by,\n name: row.name,\n models: { build: row.build_model_id, plan: row.plan_model_id, fast: row.fast_model_id },\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n}\n\nfunction toActiveModelPack(row: ActiveModelPackDbRow): ActiveModelPackRecord {\n return {\n orgId: row.org_id,\n userId: row.user_id,\n packId: row.pack_id,\n models: { build: row.build_model_id, plan: row.plan_model_id, fast: row.fast_model_id },\n updatedAt: row.updated_at,\n };\n}\n\nexport class ModelPacksStorage extends FactoryStorageDomain {\n constructor() {\n super('model-packs');\n }\n\n async init(): Promise<void> {\n await this.ensureCollections([MODEL_PACKS_SCHEMA, ACTIVE_MODEL_PACKS_SCHEMA]);\n }\n\n async dangerouslyClearAll(): Promise<void> {\n await this.ops.deleteMany('active_model_packs', {});\n await this.ops.deleteMany('model_packs', {});\n }\n\n get #db(): FactoryStorageOps {\n return this.ops;\n }\n\n /** Create or replace a pack by `(orgId, name)` — mirrors the settings.json upsert semantics. */\n async upsert({\n orgId,\n userId,\n input,\n }: {\n orgId: string;\n userId: string;\n input: UpsertModelPackInput;\n }): Promise<ModelPackRecord> {\n const now = new Date();\n const existing = await this.#db.findOne<ModelPackDbRow>('model_packs', { org_id: orgId, name: input.name });\n if (existing) {\n const row = await this.#db.updateAtomic<ModelPackDbRow>(\n 'model_packs',\n { org_id: orgId, id: existing.id },\n () => ({\n build_model_id: input.models.build,\n plan_model_id: input.models.plan,\n fast_model_id: input.models.fast,\n updated_at: now,\n }),\n );\n await this.#db.updateMany(\n 'active_model_packs',\n { org_id: orgId, pack_id: `custom:${existing.id}` },\n {\n build_model_id: input.models.build,\n plan_model_id: input.models.plan,\n fast_model_id: input.models.fast,\n updated_at: now,\n },\n );\n return toModelPack(row ?? existing);\n }\n const row = await this.#db.insertOne<ModelPackDbRow>('model_packs', {\n org_id: orgId,\n created_by: userId,\n name: input.name,\n build_model_id: input.models.build,\n plan_model_id: input.models.plan,\n fast_model_id: input.models.fast,\n created_at: now,\n updated_at: now,\n });\n return toModelPack(row);\n }\n\n async list({ orgId }: { orgId: string }): Promise<ModelPackRecord[]> {\n const rows = await this.#db.findMany<ModelPackDbRow>(\n 'model_packs',\n { org_id: orgId },\n { orderBy: [['name', 'asc']] },\n );\n return rows.map(toModelPack);\n }\n\n async get({ orgId, id }: { orgId: string; id: string }): Promise<ModelPackRecord | null> {\n const row = await this.#db.findOne<ModelPackDbRow>('model_packs', { org_id: orgId, id });\n return row ? toModelPack(row) : null;\n }\n\n async setActive({\n orgId,\n userId,\n packId,\n models,\n }: {\n orgId: string;\n userId: string;\n packId: string;\n models: ActiveModelPackRecord['models'];\n }): Promise<ActiveModelPackRecord> {\n const now = new Date();\n const values = {\n pack_id: packId,\n build_model_id: models.build,\n plan_model_id: models.plan,\n fast_model_id: models.fast,\n updated_at: now,\n };\n const updateExisting = () =>\n this.#db.updateAtomic<ActiveModelPackDbRow>(\n 'active_model_packs',\n { org_id: orgId, user_id: userId },\n () => values,\n );\n\n const updated = await updateExisting();\n if (updated) return toActiveModelPack(updated);\n\n try {\n return toActiveModelPack(\n await this.#db.insertOne<ActiveModelPackDbRow>('active_model_packs', {\n org_id: orgId,\n user_id: userId,\n ...values,\n }),\n );\n } catch (error) {\n if (!(error instanceof UniqueViolationError)) throw error;\n const row = await updateExisting();\n if (!row) throw error;\n return toActiveModelPack(row);\n }\n }\n\n async getActive({ orgId, userId }: { orgId: string; userId: string }): Promise<ActiveModelPackRecord | null> {\n const row = await this.#db.findOne<ActiveModelPackDbRow>('active_model_packs', {\n org_id: orgId,\n user_id: userId,\n });\n return row ? toActiveModelPack(row) : null;\n }\n\n async clearActive({ orgId, userId }: { orgId: string; userId: string }): Promise<boolean> {\n return (await this.#db.deleteMany('active_model_packs', { org_id: orgId, user_id: userId })) > 0;\n }\n\n async delete({ orgId, id }: { orgId: string; id: string }): Promise<boolean> {\n const deleted = (await this.#db.deleteMany('model_packs', { org_id: orgId, id })) > 0;\n if (deleted) {\n await this.#db.deleteMany('active_model_packs', { org_id: orgId, pack_id: `custom:${id}` });\n }\n return deleted;\n }\n}\n"],"mappings":";;AA2BA,MAAa,qBAAuC;CAClD,MAAM;CACN,SAAS;EACP,IAAI,EAAE,MAAM,UAAU;EACtB,QAAQ,EAAE,MAAM,OAAO;EACvB,YAAY,EAAE,MAAM,OAAO;EAC3B,MAAM,EAAE,MAAM,OAAO;EACrB,gBAAgB,EAAE,MAAM,OAAO;EAC/B,eAAe,EAAE,MAAM,OAAO;EAC9B,eAAe,EAAE,MAAM,OAAO;EAC9B,YAAY,EAAE,MAAM,YAAY;EAChC,YAAY,EAAE,MAAM,YAAY;CAClC;CACA,SAAS,CAAC;EAAE,MAAM;EAA4B,SAAS,CAAC,UAAU,MAAM;CAAE,CAAC;AAC7E;AAEA,MAAa,4BAA8C;CACzD,MAAM;CACN,SAAS;EACP,IAAI,EAAE,MAAM,UAAU;EACtB,QAAQ,EAAE,MAAM,OAAO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,MAAM,eAAe;EACrC,IAAI,SAAS,OAAO,kBAAkB,OAAO;EAE7C,IAAI;GACF,OAAO,kBACL,MAAM,KAAKA,IAAI,UAAgC,sBAAsB;IACnE,QAAQ;IACR,SAAS;IACT,GAAG;GACL,CAAC,CACH;EACF,SAAS,OAAO;GACd,IAAI,EAAE,iBAAiB,uBAAuB,MAAM;GACpD,MAAM,MAAM,MAAM,eAAe;GACjC,IAAI,CAAC,KAAK,MAAM;GAChB,OAAO,kBAAkB,GAAG;EAC9B;CACF;CAEA,MAAM,UAAU,EAAE,OAAO,UAAoF;EAC3G,MAAM,MAAM,MAAM,KAAKA,IAAI,QAA8B,sBAAsB;GAC7E,QAAQ;GACR,SAAS;EACX,CAAC;EACD,OAAO,MAAM,kBAAkB,GAAG,IAAI;CACxC;CAEA,MAAM,YAAY,EAAE,OAAO,UAA+D;EACxF,OAAQ,MAAM,KAAKA,IAAI,WAAW,sBAAsB;GAAE,QAAQ;GAAO,SAAS;EAAO,CAAC,IAAK;CACjG;CAEA,MAAM,OAAO,EAAE,OAAO,MAAuD;EAC3E,MAAM,UAAW,MAAM,KAAKA,IAAI,WAAW,eAAe;GAAE,QAAQ;GAAO;EAAG,CAAC,IAAK;EACpF,IAAI,SACF,MAAM,KAAKA,IAAI,WAAW,sBAAsB;GAAE,QAAQ;GAAO,SAAS,UAAU;EAAK,CAAC;EAE5F,OAAO;CACT;AACF"}
|
|
@@ -61,10 +61,25 @@ export interface ProjectRepository {
|
|
|
61
61
|
sandboxProvider: string;
|
|
62
62
|
sandboxWorkdir: string;
|
|
63
63
|
setupCommand: string | null;
|
|
64
|
+
/** Base checkpoint metadata — set by the base-checkpoint build job. */
|
|
65
|
+
baseCheckpoint: ProjectRepositoryBaseCheckpoint | null;
|
|
64
66
|
teardownCommand: string | null;
|
|
65
67
|
createdAt: Date;
|
|
66
68
|
updatedAt: Date;
|
|
67
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Metadata for a repo's warm base checkpoint (cloned default branch + setup
|
|
72
|
+
* command already run). New sessions boot from it instead of a cold clone.
|
|
73
|
+
*/
|
|
74
|
+
export interface ProjectRepositoryBaseCheckpoint {
|
|
75
|
+
/** Provider checkpoint name, e.g. `repo-<projectRepositoryId>`. */
|
|
76
|
+
name: string;
|
|
77
|
+
/** Default-branch HEAD sha the checkpoint was built at. */
|
|
78
|
+
sha: string;
|
|
79
|
+
builtAt: Date;
|
|
80
|
+
/** Hash of the setup command at build time (null when no setup command) — mismatch invalidates the checkpoint. */
|
|
81
|
+
setupCommandHash: string | null;
|
|
82
|
+
}
|
|
68
83
|
export interface ExternalRepositoryProjectTarget {
|
|
69
84
|
orgId: string;
|
|
70
85
|
factoryProjectId: string;
|
|
@@ -143,6 +158,12 @@ export interface UpsertSourceControlWorktreeInput {
|
|
|
143
158
|
baseBranch: string;
|
|
144
159
|
worktreePath: string;
|
|
145
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Who can open a session: 'org' sessions are visible to every member of the
|
|
163
|
+
* owning organization, 'private' sessions only to their owner. Stored at
|
|
164
|
+
* creation; rows created before the column existed read as 'org'.
|
|
165
|
+
*/
|
|
166
|
+
export type SourceControlSessionVisibility = 'org' | 'private';
|
|
146
167
|
export interface SourceControlSession {
|
|
147
168
|
id: string;
|
|
148
169
|
sessionId: string;
|
|
@@ -151,6 +172,7 @@ export interface SourceControlSession {
|
|
|
151
172
|
userId: string;
|
|
152
173
|
branch: string;
|
|
153
174
|
title: string | null;
|
|
175
|
+
visibility: SourceControlSessionVisibility;
|
|
154
176
|
baseBranch: string;
|
|
155
177
|
sandboxId: string | null;
|
|
156
178
|
sandboxWorkdir: string | null;
|
|
@@ -169,6 +191,8 @@ export interface CreateSourceControlSessionInput {
|
|
|
169
191
|
userId: string;
|
|
170
192
|
branch: string;
|
|
171
193
|
title?: string | null;
|
|
194
|
+
/** Defaults to 'org' when omitted, matching how NULL rows are read. */
|
|
195
|
+
visibility?: SourceControlSessionVisibility;
|
|
172
196
|
baseBranch: string;
|
|
173
197
|
}
|
|
174
198
|
export interface SourceControlStorageHandle {
|
|
@@ -264,6 +288,19 @@ export interface SourceControlStorageHandle {
|
|
|
264
288
|
id: string;
|
|
265
289
|
input: UpdateProjectRepositoryInput;
|
|
266
290
|
}): Promise<ProjectRepository | null>;
|
|
291
|
+
/**
|
|
292
|
+
* Record (or clear, with `checkpoint: null`) the repo's base-checkpoint
|
|
293
|
+
* metadata after a build job snapshots the prepared workdir. Writes from a
|
|
294
|
+
* build are ignored when the setup command changed while it was running.
|
|
295
|
+
*/
|
|
296
|
+
setBaseCheckpoint(args: {
|
|
297
|
+
id: string;
|
|
298
|
+
checkpoint: null;
|
|
299
|
+
} | {
|
|
300
|
+
id: string;
|
|
301
|
+
checkpoint: ProjectRepositoryBaseCheckpoint;
|
|
302
|
+
expectedSetupCommand: string | null;
|
|
303
|
+
}): Promise<void>;
|
|
267
304
|
unlink(args: {
|
|
268
305
|
orgId: string;
|
|
269
306
|
id: string;
|
|
@@ -336,10 +373,19 @@ export interface SourceControlStorageHandle {
|
|
|
336
373
|
}): Promise<void>;
|
|
337
374
|
};
|
|
338
375
|
readonly sessions: {
|
|
376
|
+
/**
|
|
377
|
+
* Viewer-aware listing: every org-visible session for the repository
|
|
378
|
+
* (regardless of owner) plus the viewer's own private sessions.
|
|
379
|
+
*/
|
|
339
380
|
list(args: {
|
|
340
381
|
projectRepositoryId: string;
|
|
341
|
-
|
|
382
|
+
viewerUserId: string;
|
|
342
383
|
}): Promise<SourceControlSession[]>;
|
|
384
|
+
/**
|
|
385
|
+
* System-level listing of every session for the repository regardless of
|
|
386
|
+
* visibility. For internal flows only (session retirement, repository
|
|
387
|
+
* teardown); never expose directly to a viewer.
|
|
388
|
+
*/
|
|
343
389
|
listByProjectRepository(args: {
|
|
344
390
|
projectRepositoryId: string;
|
|
345
391
|
}): Promise<SourceControlSession[]>;
|