diff --git a/apps/server/src/git/GitManager.ts b/apps/server/src/git/GitManager.ts index 53d0b4f5853ff..41427acb01762 100644 --- a/apps/server/src/git/GitManager.ts +++ b/apps/server/src/git/GitManager.ts @@ -113,6 +113,7 @@ export type GitBranchPullRequest = NonNullable & { interface SourceControlTextGenerationSettings { readonly modelSelection: ModelSelection; + readonly projectId: ProjectId | null; readonly style: SourceControlWritingStyleSettings; } @@ -2092,6 +2093,7 @@ export const make = Effect.gen(function* () { ...(input.includeBranch ? { includeBranch: true } : {}), ...(policy ? { policy } : {}), modelSelection: input.settings.modelSelection, + projectId: input.settings.projectId, }) .pipe(Effect.map((result) => sanitizeCommitMessage(result))); @@ -2294,6 +2296,7 @@ export const make = Effect.gen(function* () { ...(threadContext ? { threadContext } : {}), ...(policy ? { policy } : {}), modelSelection: settings.modelSelection, + projectId: settings.projectId, }); const bodyFile = path.join( @@ -3077,6 +3080,7 @@ export const make = Effect.gen(function* () { settings.sourceControlWriterModelSelection === null ? Effect.succeed({ modelSelection: settings.textGenerationModelSelection, + projectId, style: settings.sourceControlWritingStyle, }) : providerRegistry.getProviders.pipe( @@ -3086,6 +3090,7 @@ export const make = Effect.gen(function* () { providers, projectId, ), + projectId, style: settings.sourceControlWritingStyle, })), ), diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.ts index 7a45a54a312f4..56abd5161bfe2 100644 --- a/apps/server/src/orchestration-v2/ThreadLaunchService.ts +++ b/apps/server/src/orchestration-v2/ThreadLaunchService.ts @@ -360,6 +360,7 @@ const make = Effect.gen(function* () { attachments: message.attachments, ...(message.context ? { context: message.context } : {}), modelSelection, + projectId: input.projectId, }) .pipe( Effect.map((result) => ({ diff --git a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts index b73f6a2ab061a..e329edf7e2731 100644 --- a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts +++ b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts @@ -164,6 +164,7 @@ const make = Effect.gen(function* () { ...(inherited === undefined ? {} : { forkSource: inherited }), instructions: settings.threadTitleInstructions, modelSelection: settings.textGenerationModelSelection, + projectId: projection.thread.projectId, }); const generatedTitle = result.title.trim(); return generatedTitle === "New thread" || diff --git a/apps/server/src/pullRequest/PullRequestReviewGuideService.ts b/apps/server/src/pullRequest/PullRequestReviewGuideService.ts index e0dc7309289b6..8ae61b11ccb06 100644 --- a/apps/server/src/pullRequest/PullRequestReviewGuideService.ts +++ b/apps/server/src/pullRequest/PullRequestReviewGuideService.ts @@ -390,6 +390,7 @@ const make = Effect.gen(function* () { ...reviewGuideInputFromPullRequest(detail, activity, checkout.baseCommit), cwd: checkout.path, modelSelection, + projectId: input.projectId, instructions: projectSettings.reviewGuideInstructions, stack: checkout.stack, }), diff --git a/apps/server/src/textGeneration/CodexTextGeneration.test.ts b/apps/server/src/textGeneration/CodexTextGeneration.test.ts index 04ae83952b3c4..a840a8659cea8 100644 --- a/apps/server/src/textGeneration/CodexTextGeneration.test.ts +++ b/apps/server/src/textGeneration/CodexTextGeneration.test.ts @@ -1,5 +1,6 @@ import { DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER, + ProjectId, ProviderDriverKind, type ModelSelection, type ServerProvider, @@ -444,10 +445,20 @@ it.layer(layerCodexTextGenerationTest)("CodexTextGeneration", (it) => { ); }); -it.effect.each([false, true])("text generation preflight: exhausted=%s", (exhausted) => +const PREFLIGHT_PRIMARY_STATES = [ + "ready", + "pending", + "exhausted", + "not installed", + "not installed, fallback disabled for project", +] as const; + +it.effect.each(PREFLIGHT_PRIMARY_STATES)("text generation preflight: %s", (primaryState) => Effect.gen(function* () { const primary = ProviderInstanceId.make("codex"); const fallback = ProviderInstanceId.make("claude_work"); + const projectId = ProjectId.make("project-a"); + const primaryMissing = primaryState.startsWith("not installed"); const selection = { instanceId: primary, model: "chat-model", @@ -468,7 +479,13 @@ it.effect.each([false, true])("text generation preflight: exhausted=%s", (exhaus models: [], slashCommands: [], skills: [], - ...(exhausted && instanceId === primary + ...(primaryState === "pending" && instanceId === primary + ? { installed: false, status: "warning", auth: { status: "unknown" } } + : {}), + ...(primaryMissing && instanceId === primary + ? { installed: false, status: "error", auth: { status: "unknown" } } + : {}), + ...(primaryState === "exhausted" && instanceId === primary ? { usageLimits: { checkedAt: "1970-01-01T00:00:00Z", @@ -510,6 +527,13 @@ it.effect.each([false, true])("text generation preflight: exhausted=%s", (exhaus providerInstances: { [fallback]: { driver: ProviderDriverKind.make("claudeAgent"), config: {} }, }, + ...(primaryState === "not installed, fallback disabled for project" + ? { + projectSettingsOverrides: { + [projectId]: { providerInstances: { [fallback]: { enabled: false } } }, + }, + } + : {}), }), Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({ resolveLink: () => Effect.die("unused"), @@ -523,10 +547,11 @@ it.effect.each([false, true])("text generation preflight: exhausted=%s", (exhaus message: "hello", linkedContext: "", modelSelection: selection, + projectId, }), ).toEqual({ title: "Generated title" }); expect(calls).toEqual([ - exhausted + primaryState === "exhausted" || primaryState === "not installed" ? { instanceId: fallback, model: diff --git a/apps/server/src/textGeneration/TextGeneration.ts b/apps/server/src/textGeneration/TextGeneration.ts index fc8d0fb68da31..84fe9938e359e 100644 --- a/apps/server/src/textGeneration/TextGeneration.ts +++ b/apps/server/src/textGeneration/TextGeneration.ts @@ -1,7 +1,7 @@ import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; -import type { ModelSelection, ProviderInstanceId } from "@t3tools/contracts"; +import type { ModelSelection, ProjectId, ProviderInstanceId } from "@t3tools/contracts"; import { isProviderAvailable, isProviderTextGenerationCapable, @@ -74,7 +74,10 @@ export const make = Effect.gen(function* () { const serverSettings = yield* ServerSettings.ServerSettingsService; const generate = Effect.fn("TextGeneration.generate")(function* ( operation: TextGenerationOp, - selection: ModelSelection, + input: { + readonly modelSelection: ModelSelection; + readonly projectId?: ProjectId | null | undefined; + }, invoke: ( service: ProviderTextGeneration, modelSelection: ModelSelection, @@ -86,12 +89,12 @@ export const make = Effect.gen(function* () { ), ); const now = DateTime.toEpochMillis(yield* DateTime.now); - const limited = exhaustedUsageWindow( - snapshots.find((provider) => provider.instanceId === selection.instanceId), - now, - selection.model, - ); - if (limited) { + let selection = input.modelSelection; + const selected = snapshots.find((provider) => provider.instanceId === selection.instanceId); + // A pending snapshot is also not installed, but only a failed probe reports an error. + const missing = + selected?.enabled === true && !selected.installed && selected.status === "error"; + if (missing || exhaustedUsageWindow(selected, now, selection.model)) { const settings = yield* serverSettings.getSettings.pipe( Effect.mapError( (cause) => @@ -110,7 +113,11 @@ export const make = Effect.gen(function* () { return ( instanceId !== selection.instanceId && provider?.enabled === true && - resolveProjectProviderInstanceEnabled(settings, null, instanceId) && + resolveProjectProviderInstanceEnabled( + settings, + input.projectId ?? null, + instanceId, + ) && provider.installed && provider.auth.status !== "unauthenticated" && isProviderAvailable(provider) && @@ -128,45 +135,42 @@ export const make = Effect.gen(function* () { }); return TextGeneration.of({ generateCommitMessage: Effect.fn("TextGeneration.generateCommitMessage")(function (input) { - return generate("generateCommitMessage", input.modelSelection, (service, modelSelection) => + return generate("generateCommitMessage", input, (service, modelSelection) => service.generateCommitMessage({ ...input, modelSelection }), ); }), generatePrContent: Effect.fn("TextGeneration.generatePrContent")(function (input) { - return generate("generatePrContent", input.modelSelection, (service, modelSelection) => + return generate("generatePrContent", input, (service, modelSelection) => service.generatePrContent({ ...input, modelSelection }), ); }), generateBranchName: Effect.fn("TextGeneration.generateBranchName")(function (input) { - return generate("generateBranchName", input.modelSelection, (service, modelSelection) => + return generate("generateBranchName", input, (service, modelSelection) => service.generateBranchName({ ...input, modelSelection }), ); }), generateReviewGuide: Effect.fn("TextGeneration.generateReviewGuide")(function (input) { - return generate("generateReviewGuide", input.modelSelection, (service, modelSelection) => + return generate("generateReviewGuide", input, (service, modelSelection) => service.generateReviewGuide({ ...input, modelSelection }), ); }), generateThreadTitle: Effect.fn("TextGeneration.generateThreadTitle")(function (input) { - return generate( - "generateThreadTitle", - input.modelSelection, - (textGeneration, modelSelection) => - Effect.gen(function* () { - const linkedContext = - input.linkedContext ?? - (yield* ThreadTitleLinks.resolveThreadTitleLinks(input).pipe( - Effect.provideService( - SourceControlProviderRegistry.SourceControlProviderRegistry, - sourceControl, - ), - )); - return yield* textGeneration.generateThreadTitle({ - ...input, - linkedContext, - modelSelection, - }); - }), + return generate("generateThreadTitle", input, (textGeneration, modelSelection) => + Effect.gen(function* () { + const linkedContext = + input.linkedContext ?? + (yield* ThreadTitleLinks.resolveThreadTitleLinks(input).pipe( + Effect.provideService( + SourceControlProviderRegistry.SourceControlProviderRegistry, + sourceControl, + ), + )); + return yield* textGeneration.generateThreadTitle({ + ...input, + linkedContext, + modelSelection, + }); + }), ); }), }); diff --git a/packages/provider-core/src/server/textGeneration.ts b/packages/provider-core/src/server/textGeneration.ts index 3a80457cad0bf..ff4feaa2d64e8 100644 --- a/packages/provider-core/src/server/textGeneration.ts +++ b/packages/provider-core/src/server/textGeneration.ts @@ -2,6 +2,7 @@ import type { BranchNamingOptions, ChatAttachment, ModelSelection, + ProjectId, PullRequestReviewGuide, TextGenerationError, } from "@t3tools/contracts"; @@ -18,6 +19,7 @@ export interface CommitMessageGenerationInput { includeBranch?: boolean; policy?: TextGenerationPolicy | undefined; modelSelection: ModelSelection; + projectId?: ProjectId | null | undefined; } export interface CommitMessageGenerationResult { @@ -37,6 +39,7 @@ export interface PrContentGenerationInput { threadContext?: string | undefined; policy?: TextGenerationPolicy | undefined; modelSelection: ModelSelection; + projectId?: ProjectId | null | undefined; } export interface PrContentGenerationResult { @@ -50,6 +53,7 @@ export interface BranchNameGenerationInput { message: string; attachments?: ReadonlyArray | undefined; modelSelection: ModelSelection; + projectId?: ProjectId | null | undefined; } export interface BranchNameGenerationResult { @@ -65,6 +69,7 @@ export interface ThreadTitleGenerationInput { attachments?: ReadonlyArray | undefined; instructions?: string | undefined; modelSelection: ModelSelection; + projectId?: ProjectId | null | undefined; } export interface ThreadTitleGenerationResult { @@ -90,6 +95,7 @@ export interface ReviewGuideGenerationInput { instructions?: string | undefined; stack?: ReadonlyArray | undefined; modelSelection: ModelSelection; + projectId?: ProjectId | null | undefined; } export type ReviewGuideGenerationResult = Omit<