mirror of
https://github.com/anomalyco/opencode.git
synced 2026-08-02 16:26:14 -04:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bd19dc57e2 |
@@ -1,4 +1,5 @@
|
|||||||
import z from "zod"
|
import z from "zod"
|
||||||
|
import { zod } from "@/util/effect-zod"
|
||||||
import { and, Database, eq } from "../storage/db"
|
import { and, Database, eq } from "../storage/db"
|
||||||
import { ProjectTable } from "./project.sql"
|
import { ProjectTable } from "./project.sql"
|
||||||
import { SessionTable } from "../session/session.sql"
|
import { SessionTable } from "../session/session.sql"
|
||||||
@@ -8,7 +9,7 @@ import { BusEvent } from "@/bus/bus-event"
|
|||||||
import { GlobalBus } from "@/bus/global"
|
import { GlobalBus } from "@/bus/global"
|
||||||
import { which } from "../util/which"
|
import { which } from "../util/which"
|
||||||
import { ProjectID } from "./schema"
|
import { ProjectID } from "./schema"
|
||||||
import { Effect, Layer, Path, Scope, Context, Stream } from "effect"
|
import { Effect, Layer, Path, Scope, Context, Stream, Schema } from "effect"
|
||||||
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
|
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
|
||||||
import { NodePath } from "@effect/platform-node"
|
import { NodePath } from "@effect/platform-node"
|
||||||
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
|
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
|
||||||
@@ -17,38 +18,39 @@ import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
|
|||||||
export namespace Project {
|
export namespace Project {
|
||||||
const log = Log.create({ service: "project" })
|
const log = Log.create({ service: "project" })
|
||||||
|
|
||||||
export const Info = z
|
const IconSchema = Schema.Struct({
|
||||||
.object({
|
url: Schema.optional(Schema.String),
|
||||||
id: ProjectID.zod,
|
override: Schema.optional(Schema.String),
|
||||||
worktree: z.string(),
|
color: Schema.optional(Schema.String),
|
||||||
vcs: z.literal("git").optional(),
|
})
|
||||||
name: z.string().optional(),
|
|
||||||
icon: z
|
const CommandsSchema = Schema.Struct({
|
||||||
.object({
|
start: Schema.optional(
|
||||||
url: z.string().optional(),
|
Schema.String.annotate({ description: "Startup script to run when creating a new workspace (worktree)" }),
|
||||||
override: z.string().optional(),
|
),
|
||||||
color: z.string().optional(),
|
})
|
||||||
})
|
|
||||||
.optional(),
|
const TimeSchema = Schema.Struct({
|
||||||
commands: z
|
created: Schema.Number,
|
||||||
.object({
|
updated: Schema.Number,
|
||||||
start: z.string().optional().describe("Startup script to run when creating a new workspace (worktree)"),
|
initialized: Schema.optional(Schema.Number),
|
||||||
})
|
})
|
||||||
.optional(),
|
|
||||||
time: z.object({
|
export class Info extends Schema.Class<Info>("Project")({
|
||||||
created: z.number(),
|
id: ProjectID,
|
||||||
updated: z.number(),
|
worktree: Schema.String,
|
||||||
initialized: z.number().optional(),
|
vcs: Schema.optional(Schema.Literal("git")),
|
||||||
}),
|
name: Schema.optional(Schema.String),
|
||||||
sandboxes: z.array(z.string()),
|
icon: Schema.optional(IconSchema),
|
||||||
})
|
commands: Schema.optional(CommandsSchema),
|
||||||
.meta({
|
time: TimeSchema,
|
||||||
ref: "Project",
|
sandboxes: Schema.mutable(Schema.Array(Schema.String)),
|
||||||
})
|
}) {
|
||||||
export type Info = z.infer<typeof Info>
|
static readonly zod = zod(this)
|
||||||
|
}
|
||||||
|
|
||||||
export const Event = {
|
export const Event = {
|
||||||
Updated: BusEvent.define("project.updated", Info),
|
Updated: BusEvent.define("project.updated", Info.zod),
|
||||||
}
|
}
|
||||||
|
|
||||||
type Row = typeof ProjectTable.$inferSelect
|
type Row = typeof ProjectTable.$inferSelect
|
||||||
@@ -58,10 +60,10 @@ export namespace Project {
|
|||||||
row.icon_url || row.icon_color
|
row.icon_url || row.icon_color
|
||||||
? { url: row.icon_url ?? undefined, color: row.icon_color ?? undefined }
|
? { url: row.icon_url ?? undefined, color: row.icon_color ?? undefined }
|
||||||
: undefined
|
: undefined
|
||||||
return {
|
return new Info({
|
||||||
id: row.id,
|
id: row.id as ProjectID,
|
||||||
worktree: row.worktree,
|
worktree: row.worktree,
|
||||||
vcs: row.vcs ? Info.shape.vcs.parse(row.vcs) : undefined,
|
vcs: row.vcs === "git" ? "git" : undefined,
|
||||||
name: row.name ?? undefined,
|
name: row.name ?? undefined,
|
||||||
icon,
|
icon,
|
||||||
time: {
|
time: {
|
||||||
@@ -71,14 +73,14 @@ export namespace Project {
|
|||||||
},
|
},
|
||||||
sandboxes: row.sandboxes,
|
sandboxes: row.sandboxes,
|
||||||
commands: row.commands ?? undefined,
|
commands: row.commands ?? undefined,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UpdateInput = z.object({
|
export const UpdateInput = z.object({
|
||||||
projectID: ProjectID.zod,
|
projectID: ProjectID.zod,
|
||||||
name: z.string().optional(),
|
name: z.string().optional(),
|
||||||
icon: Info.shape.icon.optional(),
|
icon: zod(IconSchema).optional(),
|
||||||
commands: Info.shape.commands.optional(),
|
commands: zod(CommandsSchema).optional(),
|
||||||
})
|
})
|
||||||
export type UpdateInput = z.infer<typeof UpdateInput>
|
export type UpdateInput = z.infer<typeof UpdateInput>
|
||||||
|
|
||||||
@@ -142,7 +144,7 @@ export namespace Project {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
const fakeVcs = Info.shape.vcs.parse(Flag.OPENCODE_FAKE_VCS)
|
const fakeVcs: Info["vcs"] = Flag.OPENCODE_FAKE_VCS === "git" ? "git" : undefined
|
||||||
|
|
||||||
const resolveGitPath = (cwd: string, name: string) => {
|
const resolveGitPath = (cwd: string, name: string) => {
|
||||||
if (!name) return cwd
|
if (!name) return cwd
|
||||||
@@ -249,27 +251,23 @@ export namespace Project {
|
|||||||
const row = yield* db((d) => d.select().from(ProjectTable).where(eq(ProjectTable.id, data.id)).get())
|
const row = yield* db((d) => d.select().from(ProjectTable).where(eq(ProjectTable.id, data.id)).get())
|
||||||
const existing = row
|
const existing = row
|
||||||
? fromRow(row)
|
? fromRow(row)
|
||||||
: {
|
: new Info({
|
||||||
id: data.id,
|
id: data.id as ProjectID,
|
||||||
worktree: data.worktree,
|
worktree: data.worktree,
|
||||||
vcs: data.vcs,
|
vcs: data.vcs,
|
||||||
sandboxes: [] as string[],
|
sandboxes: [],
|
||||||
time: { created: Date.now(), updated: Date.now() },
|
time: { created: Date.now(), updated: Date.now() },
|
||||||
}
|
})
|
||||||
|
|
||||||
if (Flag.OPENCODE_EXPERIMENTAL_ICON_DISCOVERY)
|
if (Flag.OPENCODE_EXPERIMENTAL_ICON_DISCOVERY)
|
||||||
yield* discover(existing).pipe(Effect.ignore, Effect.forkIn(scope))
|
yield* discover(existing).pipe(Effect.ignore, Effect.forkIn(scope))
|
||||||
|
|
||||||
const result: Info = {
|
const sandboxes =
|
||||||
...existing,
|
data.sandbox !== existing.worktree && !existing.sandboxes.includes(data.sandbox)
|
||||||
worktree: data.worktree,
|
? [...existing.sandboxes, data.sandbox]
|
||||||
vcs: data.vcs,
|
: existing.sandboxes
|
||||||
time: { ...existing.time, updated: Date.now() },
|
const filteredSandboxes = yield* Effect.forEach(
|
||||||
}
|
sandboxes,
|
||||||
if (data.sandbox !== result.worktree && !result.sandboxes.includes(data.sandbox))
|
|
||||||
result.sandboxes.push(data.sandbox)
|
|
||||||
result.sandboxes = yield* Effect.forEach(
|
|
||||||
result.sandboxes,
|
|
||||||
(s) =>
|
(s) =>
|
||||||
fs.exists(s).pipe(
|
fs.exists(s).pipe(
|
||||||
Effect.orDie,
|
Effect.orDie,
|
||||||
@@ -278,6 +276,14 @@ export namespace Project {
|
|||||||
{ concurrency: "unbounded" },
|
{ concurrency: "unbounded" },
|
||||||
).pipe(Effect.map((arr) => arr.filter((x): x is string => x !== undefined)))
|
).pipe(Effect.map((arr) => arr.filter((x): x is string => x !== undefined)))
|
||||||
|
|
||||||
|
const result = new Info({
|
||||||
|
...existing,
|
||||||
|
worktree: data.worktree,
|
||||||
|
vcs: data.vcs,
|
||||||
|
time: { ...existing.time, updated: Date.now() },
|
||||||
|
sandboxes: filteredSandboxes,
|
||||||
|
})
|
||||||
|
|
||||||
yield* db((d) =>
|
yield* db((d) =>
|
||||||
d
|
d
|
||||||
.insert(ProjectTable)
|
.insert(ProjectTable)
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import { Instance } from "@/project/instance"
|
||||||
|
import { Project } from "@/project/project"
|
||||||
|
import { Effect, Layer, Schema } from "effect"
|
||||||
|
import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
|
||||||
|
|
||||||
|
const root = "/project"
|
||||||
|
|
||||||
|
export const ProjectApi = HttpApi.make("project")
|
||||||
|
.add(
|
||||||
|
HttpApiGroup.make("project")
|
||||||
|
.add(
|
||||||
|
HttpApiEndpoint.get("list", root, {
|
||||||
|
success: Schema.Array(Project.Info),
|
||||||
|
}).annotateMerge(
|
||||||
|
OpenApi.annotations({
|
||||||
|
identifier: "project.list",
|
||||||
|
summary: "List all projects",
|
||||||
|
description: "Get a list of projects that have been opened with OpenCode.",
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
HttpApiEndpoint.get("current", `${root}/current`, {
|
||||||
|
success: Project.Info,
|
||||||
|
}).annotateMerge(
|
||||||
|
OpenApi.annotations({
|
||||||
|
identifier: "project.current",
|
||||||
|
summary: "Get current project",
|
||||||
|
description: "Retrieve the currently active project that OpenCode is working with.",
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.annotateMerge(
|
||||||
|
OpenApi.annotations({
|
||||||
|
title: "project",
|
||||||
|
description: "Project routes.",
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.annotateMerge(
|
||||||
|
OpenApi.annotations({
|
||||||
|
title: "opencode HttpApi",
|
||||||
|
version: "0.0.1",
|
||||||
|
description: "Effect HttpApi surface for instance routes.",
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
export const projectHandlers = Layer.unwrap(
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const svc = yield* Project.Service
|
||||||
|
|
||||||
|
const list = Effect.fn("ProjectHttpApi.list")(function* () {
|
||||||
|
return yield* svc.list()
|
||||||
|
})
|
||||||
|
|
||||||
|
const current = Effect.fn("ProjectHttpApi.current")(function* () {
|
||||||
|
return Instance.project
|
||||||
|
})
|
||||||
|
|
||||||
|
return HttpApiBuilder.group(ProjectApi, "project", (handlers) =>
|
||||||
|
handlers.handle("list", list).handle("current", current),
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
).pipe(Layer.provide(Project.defaultLayer))
|
||||||
@@ -11,6 +11,7 @@ import { Instance } from "@/project/instance"
|
|||||||
import { lazy } from "@/util/lazy"
|
import { lazy } from "@/util/lazy"
|
||||||
import { Filesystem } from "@/util"
|
import { Filesystem } from "@/util"
|
||||||
import { PermissionApi, permissionHandlers } from "./permission"
|
import { PermissionApi, permissionHandlers } from "./permission"
|
||||||
|
import { ProjectApi, projectHandlers } from "./project"
|
||||||
import { ProviderApi, providerHandlers } from "./provider"
|
import { ProviderApi, providerHandlers } from "./provider"
|
||||||
import { QuestionApi, questionHandlers } from "./question"
|
import { QuestionApi, questionHandlers } from "./question"
|
||||||
|
|
||||||
@@ -109,11 +110,13 @@ export namespace ExperimentalHttpApiServer {
|
|||||||
const QuestionSecured = QuestionApi.middleware(Authorization)
|
const QuestionSecured = QuestionApi.middleware(Authorization)
|
||||||
const PermissionSecured = PermissionApi.middleware(Authorization)
|
const PermissionSecured = PermissionApi.middleware(Authorization)
|
||||||
const ProviderSecured = ProviderApi.middleware(Authorization)
|
const ProviderSecured = ProviderApi.middleware(Authorization)
|
||||||
|
const ProjectSecured = ProjectApi.middleware(Authorization)
|
||||||
|
|
||||||
export const routes = Layer.mergeAll(
|
export const routes = Layer.mergeAll(
|
||||||
HttpApiBuilder.layer(QuestionSecured).pipe(Layer.provide(questionHandlers)),
|
HttpApiBuilder.layer(QuestionSecured).pipe(Layer.provide(questionHandlers)),
|
||||||
HttpApiBuilder.layer(PermissionSecured).pipe(Layer.provide(permissionHandlers)),
|
HttpApiBuilder.layer(PermissionSecured).pipe(Layer.provide(permissionHandlers)),
|
||||||
HttpApiBuilder.layer(ProviderSecured).pipe(Layer.provide(providerHandlers)),
|
HttpApiBuilder.layer(ProviderSecured).pipe(Layer.provide(providerHandlers)),
|
||||||
|
HttpApiBuilder.layer(ProjectSecured).pipe(Layer.provide(projectHandlers)),
|
||||||
).pipe(
|
).pipe(
|
||||||
Layer.provide(auth),
|
Layer.provide(auth),
|
||||||
Layer.provide(normalize),
|
Layer.provide(normalize),
|
||||||
|
|||||||
@@ -30,14 +30,7 @@ import { WorkspaceRouterMiddleware } from "./middleware"
|
|||||||
import { AppRuntime } from "@/effect/app-runtime"
|
import { AppRuntime } from "@/effect/app-runtime"
|
||||||
|
|
||||||
export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
|
export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
|
||||||
const app = new Hono()
|
const app = new Hono().use(WorkspaceRouterMiddleware(upgrade))
|
||||||
.use(WorkspaceRouterMiddleware(upgrade))
|
|
||||||
.route("/project", ProjectRoutes())
|
|
||||||
.route("/pty", PtyRoutes(upgrade))
|
|
||||||
.route("/config", ConfigRoutes())
|
|
||||||
.route("/experimental", ExperimentalRoutes())
|
|
||||||
.route("/session", SessionRoutes())
|
|
||||||
.route("/permission", PermissionRoutes())
|
|
||||||
|
|
||||||
if (Flag.OPENCODE_EXPERIMENTAL_HTTPAPI) {
|
if (Flag.OPENCODE_EXPERIMENTAL_HTTPAPI) {
|
||||||
const handler = ExperimentalHttpApiServer.webHandler().handler
|
const handler = ExperimentalHttpApiServer.webHandler().handler
|
||||||
@@ -47,9 +40,17 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
|
|||||||
.all("/permission", (c) => handler(c.req.raw))
|
.all("/permission", (c) => handler(c.req.raw))
|
||||||
.all("/permission/*", (c) => handler(c.req.raw))
|
.all("/permission/*", (c) => handler(c.req.raw))
|
||||||
.all("/provider/auth", (c) => handler(c.req.raw))
|
.all("/provider/auth", (c) => handler(c.req.raw))
|
||||||
|
.all("/project", (c) => handler(c.req.raw))
|
||||||
|
.all("/project/*", (c) => handler(c.req.raw))
|
||||||
}
|
}
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
.route("/project", ProjectRoutes())
|
||||||
|
.route("/pty", PtyRoutes(upgrade))
|
||||||
|
.route("/config", ConfigRoutes())
|
||||||
|
.route("/experimental", ExperimentalRoutes())
|
||||||
|
.route("/session", SessionRoutes())
|
||||||
|
.route("/permission", PermissionRoutes())
|
||||||
.route("/question", QuestionRoutes())
|
.route("/question", QuestionRoutes())
|
||||||
.route("/provider", ProviderRoutes())
|
.route("/provider", ProviderRoutes())
|
||||||
.route("/sync", SyncRoutes())
|
.route("/sync", SyncRoutes())
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export const ProjectRoutes = lazy(() =>
|
|||||||
description: "List of projects",
|
description: "List of projects",
|
||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: resolver(Project.Info.array()),
|
schema: resolver(z.array(Project.Info.zod)),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -45,7 +45,7 @@ export const ProjectRoutes = lazy(() =>
|
|||||||
description: "Current project information",
|
description: "Current project information",
|
||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: resolver(Project.Info),
|
schema: resolver(Project.Info.zod),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -66,7 +66,7 @@ export const ProjectRoutes = lazy(() =>
|
|||||||
description: "Project information after git initialization",
|
description: "Project information after git initialization",
|
||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: resolver(Project.Info),
|
schema: resolver(Project.Info.zod),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -99,7 +99,7 @@ export const ProjectRoutes = lazy(() =>
|
|||||||
description: "Updated project information",
|
description: "Updated project information",
|
||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: resolver(Project.Info),
|
schema: resolver(Project.Info.zod),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user