feat(core): refactor project copies for v2 (#31943)
This commit is contained in:
@@ -1,87 +1,31 @@
|
||||
import { ProjectCopy } from "@opencode-ai/core/project/copy"
|
||||
import { ProjectV2 } from "@opencode-ai/core/project"
|
||||
import { Schema } from "effect"
|
||||
import { HttpApi, HttpApiEndpoint, HttpApiGroup, HttpApiSchema, OpenApi } from "effect/unstable/httpapi"
|
||||
import { HttpApi, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
|
||||
import { Authorization } from "../middleware/authorization"
|
||||
import { InstanceContextMiddleware } from "../middleware/instance-context"
|
||||
import {
|
||||
WorkspaceRoutingMiddleware,
|
||||
WorkspaceRoutingQuery,
|
||||
WorkspaceRoutingQueryFields,
|
||||
} from "../middleware/workspace-routing"
|
||||
import { described } from "./metadata"
|
||||
import { WorkspaceRoutingMiddleware, WorkspaceRoutingQuery } from "../middleware/workspace-routing"
|
||||
|
||||
const root = "/experimental/project/:projectID/copy"
|
||||
const CopyQuery = Schema.Struct({
|
||||
workspace: WorkspaceRoutingQueryFields.workspace,
|
||||
export const GenerateNamePayload = Schema.Struct({
|
||||
context: Schema.optional(Schema.String),
|
||||
})
|
||||
|
||||
export const CreatePayload = Schema.Struct({
|
||||
strategy: ProjectCopy.StrategyID,
|
||||
directory: ProjectCopy.CreateInput.fields.directory,
|
||||
name: ProjectCopy.CreateInput.fields.name,
|
||||
context: ProjectCopy.CreateInput.fields.context,
|
||||
})
|
||||
export const RemovePayload = Schema.Struct({
|
||||
directory: ProjectCopy.RemoveInput.fields.directory,
|
||||
force: ProjectCopy.RemoveInput.fields.force,
|
||||
})
|
||||
|
||||
export class ApiProjectCopyError extends Schema.ErrorClass<ApiProjectCopyError>("ProjectCopyError")(
|
||||
{
|
||||
name: Schema.Literal("ProjectCopyError"),
|
||||
data: Schema.Struct({
|
||||
message: Schema.String,
|
||||
forceRequired: Schema.optional(Schema.Boolean),
|
||||
}),
|
||||
},
|
||||
{ httpApiStatus: 400 },
|
||||
) {}
|
||||
|
||||
export const ProjectCopyApi = HttpApi.make("projectCopy").add(
|
||||
HttpApiGroup.make("projectCopy")
|
||||
export const ProjectCopyApi = HttpApi.make("projectCopyName").add(
|
||||
HttpApiGroup.make("projectCopyName")
|
||||
.add(
|
||||
HttpApiEndpoint.post("create", root, {
|
||||
params: { projectID: ProjectV2.ID },
|
||||
query: CopyQuery,
|
||||
payload: CreatePayload,
|
||||
success: described(ProjectCopy.Copy, "Project copy created"),
|
||||
error: ApiProjectCopyError,
|
||||
}).annotateMerge(
|
||||
OpenApi.annotations({
|
||||
identifier: "experimental.projectCopy.create",
|
||||
summary: "Create project copy",
|
||||
description: "Create a local physical copy of a project using the selected strategy.",
|
||||
}),
|
||||
),
|
||||
HttpApiEndpoint.delete("remove", root, {
|
||||
params: { projectID: ProjectV2.ID },
|
||||
query: CopyQuery,
|
||||
payload: RemovePayload,
|
||||
success: described(HttpApiSchema.NoContent, "Project copy removed"),
|
||||
error: ApiProjectCopyError,
|
||||
}).annotateMerge(
|
||||
OpenApi.annotations({
|
||||
identifier: "experimental.projectCopy.remove",
|
||||
summary: "Remove project copy",
|
||||
description: "Remove a local physical copy of a project using the selected strategy.",
|
||||
}),
|
||||
),
|
||||
HttpApiEndpoint.post("refresh", `${root}/refresh`, {
|
||||
HttpApiEndpoint.post("generateName", "/experimental/project/:projectID/copy/generate-name", {
|
||||
params: { projectID: ProjectV2.ID },
|
||||
query: WorkspaceRoutingQuery,
|
||||
payload: HttpApiSchema.NoContent,
|
||||
success: described(HttpApiSchema.NoContent, "Project copies refreshed"),
|
||||
error: ApiProjectCopyError,
|
||||
payload: GenerateNamePayload,
|
||||
success: Schema.Struct({ name: Schema.String }),
|
||||
}).annotateMerge(
|
||||
OpenApi.annotations({
|
||||
identifier: "experimental.projectCopy.refresh",
|
||||
summary: "Refresh project copies",
|
||||
description: "Discover local project copies using one or all configured strategies.",
|
||||
identifier: "experimental.projectCopy.generateName",
|
||||
summary: "Generate project copy name",
|
||||
description: "Generate a short name for a project copy from task context.",
|
||||
}),
|
||||
),
|
||||
)
|
||||
.annotateMerge(OpenApi.annotations({ title: "projectCopy", description: "Project copy management routes." }))
|
||||
.annotateMerge(OpenApi.annotations({ title: "projectCopy", description: "Project copy naming routes." }))
|
||||
.middleware(InstanceContextMiddleware)
|
||||
.middleware(WorkspaceRoutingMiddleware)
|
||||
.middleware(Authorization),
|
||||
|
||||
@@ -1,75 +1,45 @@
|
||||
import { ProjectCopy } from "@opencode-ai/core/project/copy"
|
||||
import { Git } from "@opencode-ai/core/git"
|
||||
import { ProjectV2 } from "@opencode-ai/core/project"
|
||||
import { AbsolutePath } from "@opencode-ai/core/schema"
|
||||
import { InstanceState } from "@/effect/instance-state"
|
||||
import { Agent } from "@/agent/agent"
|
||||
import { Provider } from "@/provider/provider"
|
||||
import { LLM } from "@/session/llm"
|
||||
import { MessageID, SessionID } from "@/session/schema"
|
||||
import { Slug } from "@opencode-ai/core/util/slug"
|
||||
import { LLMEvent } from "@opencode-ai/llm"
|
||||
import { Effect, Stream } from "effect"
|
||||
import { HttpApiBuilder } from "effect/unstable/httpapi"
|
||||
import { InstanceHttpApi } from "../api"
|
||||
import { ApiProjectCopyError, CreatePayload, RemovePayload } from "../groups/project-copy"
|
||||
import { Agent } from "@/agent/agent"
|
||||
import { LLM } from "@/session/llm"
|
||||
import { LLMEvent } from "@opencode-ai/llm"
|
||||
import { MessageID, SessionID } from "@/session/schema"
|
||||
import { Provider } from "@/provider/provider"
|
||||
import { Slug } from "@opencode-ai/core/util/slug"
|
||||
|
||||
const FALLBACK_AGENT: Agent.Info = {
|
||||
name: "title",
|
||||
mode: "primary" as const,
|
||||
const COPY_NAME_AGENT: Agent.Info = {
|
||||
name: "project-copy-name",
|
||||
mode: "primary",
|
||||
permission: [],
|
||||
options: {},
|
||||
native: true,
|
||||
prompt: "",
|
||||
}
|
||||
|
||||
function badRequest<A, R>(effect: Effect.Effect<A, ProjectCopy.Error, R>) {
|
||||
return effect.pipe(
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new ApiProjectCopyError({
|
||||
name: "ProjectCopyError",
|
||||
data: {
|
||||
message: message(error),
|
||||
forceRequired: error instanceof Git.WorktreeError ? error.forceRequired : undefined,
|
||||
},
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
export const projectCopyHandlers = HttpApiBuilder.group(InstanceHttpApi, "projectCopy", (handlers) =>
|
||||
export const projectCopyHandlers = HttpApiBuilder.group(InstanceHttpApi, "projectCopyName", (handlers) =>
|
||||
Effect.gen(function* () {
|
||||
const llm = yield* LLM.Service
|
||||
const agent = yield* Agent.Service
|
||||
const provider = yield* Provider.Service
|
||||
const service = yield* ProjectCopy.Service
|
||||
|
||||
const generateName = Effect.fn("ProjectCopyHttpApi.generateName")(function* (context: string | undefined) {
|
||||
const text = context?.trim()
|
||||
if (!text) return Slug.create()
|
||||
const [titleAgent, fallback] = yield* Effect.all(
|
||||
[
|
||||
agent.get("title").pipe(Effect.catch(() => Effect.succeed(FALLBACK_AGENT))),
|
||||
provider.defaultModel().pipe(Effect.catch(() => Effect.succeed(undefined))),
|
||||
],
|
||||
{ concurrency: 2 },
|
||||
)
|
||||
const fallback = yield* provider.defaultModel().pipe(Effect.catch(() => Effect.succeed(undefined)))
|
||||
if (!fallback) return Slug.create()
|
||||
const model = titleAgent.model
|
||||
? yield* provider.getModel(titleAgent.model.providerID, titleAgent.model.modelID)
|
||||
: ((yield* provider.getSmallModel(fallback.providerID)) ??
|
||||
(yield* provider.getModel(fallback.providerID, fallback.modelID)))
|
||||
const model =
|
||||
(yield* provider.getSmallModel(fallback.providerID)) ??
|
||||
(yield* provider.getModel(fallback.providerID, fallback.modelID))
|
||||
const sessionID = SessionID.descending()
|
||||
const result = yield* llm
|
||||
.stream({
|
||||
agent: titleAgent,
|
||||
agent: COPY_NAME_AGENT,
|
||||
user: {
|
||||
id: MessageID.ascending(),
|
||||
sessionID,
|
||||
role: "user",
|
||||
time: { created: Date.now() },
|
||||
agent: titleAgent.name,
|
||||
agent: COPY_NAME_AGENT.name,
|
||||
model: { providerID: model.providerID, modelID: model.id },
|
||||
},
|
||||
system: [],
|
||||
@@ -78,12 +48,7 @@ export const projectCopyHandlers = HttpApiBuilder.group(InstanceHttpApi, "projec
|
||||
model,
|
||||
sessionID,
|
||||
retries: 2,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: `Generate a short 3-4 word name that describes this task:\n${text}`,
|
||||
},
|
||||
],
|
||||
messages: [{ role: "user", content: `Generate a short 2-3 word name that describes this task:\n${text}` }],
|
||||
})
|
||||
.pipe(
|
||||
Stream.filter(LLMEvent.is.textDelta),
|
||||
@@ -91,47 +56,20 @@ export const projectCopyHandlers = HttpApiBuilder.group(InstanceHttpApi, "projec
|
||||
Stream.mkString,
|
||||
)
|
||||
const output = result.trim()
|
||||
return output ? slugify(output.split(/\s+/).slice(0, 4).join(" ")) : Slug.create()
|
||||
return output ? slugify(output.split(/\s+/).slice(0, 3).join(" ")) : Slug.create()
|
||||
})
|
||||
|
||||
const create = Effect.fn("ProjectCopyHttpApi.create")(function* (ctx: {
|
||||
params: { projectID: ProjectV2.ID }
|
||||
payload: typeof CreatePayload.Type
|
||||
}) {
|
||||
const name =
|
||||
ctx.payload.name ??
|
||||
(yield* generateName(ctx.payload.context).pipe(Effect.catch(() => Effect.succeed(Slug.create()))))
|
||||
return yield* badRequest(
|
||||
service.create({
|
||||
...ctx.payload,
|
||||
name,
|
||||
projectID: ctx.params.projectID,
|
||||
sourceDirectory: AbsolutePath.make((yield* InstanceState.context).worktree),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
const remove = Effect.fn("ProjectCopyHttpApi.remove")(function* (ctx: {
|
||||
params: { projectID: ProjectV2.ID }
|
||||
payload: typeof RemovePayload.Type
|
||||
}) {
|
||||
yield* badRequest(
|
||||
service.remove({
|
||||
...ctx.payload,
|
||||
projectID: ctx.params.projectID,
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
const refresh = Effect.fn("ProjectCopyHttpApi.refresh")(function* (ctx: { params: { projectID: ProjectV2.ID } }) {
|
||||
yield* badRequest(
|
||||
service.refresh({
|
||||
projectID: ctx.params.projectID,
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
return handlers.handle("create", create).handle("remove", remove).handle("refresh", refresh)
|
||||
return handlers.handle("generateName", (ctx) =>
|
||||
generateName(ctx.payload.context).pipe(
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.logWarning("project copy name generation failed", {
|
||||
projectID: ctx.params.projectID,
|
||||
cause,
|
||||
}).pipe(Effect.as(Slug.create())),
|
||||
),
|
||||
Effect.map((name) => ({ name })),
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -143,15 +81,3 @@ function slugify(input: string) {
|
||||
.replace(/^-+/, "")
|
||||
.replace(/-+$/, "")
|
||||
}
|
||||
|
||||
function message(error: ProjectCopy.Error) {
|
||||
if (error instanceof ProjectCopy.SourceDirectoryNotFoundError)
|
||||
return `Project copy source not found: ${error.directory}`
|
||||
if (error instanceof ProjectCopy.DestinationExistsError)
|
||||
return `Project copy destination already exists: ${error.directory}`
|
||||
if (error instanceof ProjectCopy.DirectoryUnavailableError)
|
||||
return `Project copy directory unavailable: ${error.directory}`
|
||||
if (error instanceof ProjectCopy.StrategyNotFoundError)
|
||||
return `Project copy strategy not found for: ${error.directory}`
|
||||
return error.message
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user