Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/server/src/git/GitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export type GitBranchPullRequest = NonNullable<VcsStatusResult["pr"]> & {

interface SourceControlTextGenerationSettings {
readonly modelSelection: ModelSelection;
readonly projectId: ProjectId | null;
readonly style: SourceControlWritingStyleSettings;
}

Expand Down Expand Up @@ -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)));

Expand Down Expand Up @@ -2294,6 +2296,7 @@ export const make = Effect.gen(function* () {
...(threadContext ? { threadContext } : {}),
...(policy ? { policy } : {}),
modelSelection: settings.modelSelection,
projectId: settings.projectId,
});

const bodyFile = path.join(
Expand Down Expand Up @@ -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(
Expand All @@ -3086,6 +3090,7 @@ export const make = Effect.gen(function* () {
providers,
projectId,
),
projectId,
style: settings.sourceControlWritingStyle,
})),
),
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/orchestration-v2/ThreadLaunchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
Expand Down
31 changes: 28 additions & 3 deletions apps/server/src/textGeneration/CodexTextGeneration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER,
ProjectId,
ProviderDriverKind,
type ModelSelection,
type ServerProvider,
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"),
Expand All @@ -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:
Expand Down
68 changes: 36 additions & 32 deletions apps/server/src/textGeneration/TextGeneration.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -74,7 +74,10 @@ export const make = Effect.gen(function* () {
const serverSettings = yield* ServerSettings.ServerSettingsService;
const generate = Effect.fn("TextGeneration.generate")(function* <A>(
operation: TextGenerationOp,
selection: ModelSelection,
input: {
readonly modelSelection: ModelSelection;
readonly projectId?: ProjectId | null | undefined;
},
invoke: (
service: ProviderTextGeneration,
modelSelection: ModelSelection,
Expand All @@ -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) =>
Expand All @@ -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) &&
Expand All @@ -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,
});
}),
);
}),
});
Expand Down
6 changes: 6 additions & 0 deletions packages/provider-core/src/server/textGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
BranchNamingOptions,
ChatAttachment,
ModelSelection,
ProjectId,
PullRequestReviewGuide,
TextGenerationError,
} from "@t3tools/contracts";
Expand All @@ -18,6 +19,7 @@ export interface CommitMessageGenerationInput {
includeBranch?: boolean;
policy?: TextGenerationPolicy | undefined;
modelSelection: ModelSelection;
projectId?: ProjectId | null | undefined;
}

export interface CommitMessageGenerationResult {
Expand All @@ -37,6 +39,7 @@ export interface PrContentGenerationInput {
threadContext?: string | undefined;
policy?: TextGenerationPolicy | undefined;
modelSelection: ModelSelection;
projectId?: ProjectId | null | undefined;
}

export interface PrContentGenerationResult {
Expand All @@ -50,6 +53,7 @@ export interface BranchNameGenerationInput {
message: string;
attachments?: ReadonlyArray<ChatAttachment> | undefined;
modelSelection: ModelSelection;
projectId?: ProjectId | null | undefined;
}

export interface BranchNameGenerationResult {
Expand All @@ -65,6 +69,7 @@ export interface ThreadTitleGenerationInput {
attachments?: ReadonlyArray<ChatAttachment> | undefined;
instructions?: string | undefined;
modelSelection: ModelSelection;
projectId?: ProjectId | null | undefined;
}

export interface ThreadTitleGenerationResult {
Expand All @@ -90,6 +95,7 @@ export interface ReviewGuideGenerationInput {
instructions?: string | undefined;
stack?: ReadonlyArray<ReviewGuideStackLayer> | undefined;
modelSelection: ModelSelection;
projectId?: ProjectId | null | undefined;
}

export type ReviewGuideGenerationResult = Omit<
Expand Down
Loading