mirror of
https://github.com/anomalyco/opencode.git
synced 2026-04-15 10:24:53 +00:00
Compare commits
3 Commits
dev
...
kit/projec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b92ca9caf | ||
|
|
847e5f3a70 | ||
|
|
a1f7efd156 |
@@ -1,4 +1,5 @@
|
||||
import z from "zod"
|
||||
import { zod } from "@/util/effect-zod"
|
||||
import { and, Database, eq } from "../storage/db"
|
||||
import { ProjectTable } from "./project.sql"
|
||||
import { SessionTable } from "../session/session.sql"
|
||||
@@ -8,7 +9,7 @@ import { BusEvent } from "@/bus/bus-event"
|
||||
import { GlobalBus } from "@/bus/global"
|
||||
import { which } from "../util/which"
|
||||
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 { NodePath } from "@effect/platform-node"
|
||||
import { AppFileSystem } from "@/filesystem"
|
||||
@@ -17,38 +18,45 @@ import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
|
||||
export namespace Project {
|
||||
const log = Log.create({ service: "project" })
|
||||
|
||||
export const Info = z
|
||||
.object({
|
||||
id: ProjectID.zod,
|
||||
worktree: z.string(),
|
||||
vcs: z.literal("git").optional(),
|
||||
name: z.string().optional(),
|
||||
icon: z
|
||||
.object({
|
||||
url: z.string().optional(),
|
||||
override: z.string().optional(),
|
||||
color: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
commands: z
|
||||
.object({
|
||||
start: z.string().optional().describe("Startup script to run when creating a new workspace (worktree)"),
|
||||
})
|
||||
.optional(),
|
||||
time: z.object({
|
||||
created: z.number(),
|
||||
updated: z.number(),
|
||||
initialized: z.number().optional(),
|
||||
}),
|
||||
sandboxes: z.array(z.string()),
|
||||
})
|
||||
.meta({
|
||||
ref: "Project",
|
||||
})
|
||||
export type Info = z.infer<typeof Info>
|
||||
export class Icon extends Schema.Class<Icon>("ProjectIcon")({
|
||||
url: Schema.optional(Schema.String),
|
||||
override: Schema.optional(Schema.String),
|
||||
color: Schema.optional(Schema.String),
|
||||
}) {
|
||||
static readonly zod = zod(this)
|
||||
}
|
||||
|
||||
export class Commands extends Schema.Class<Commands>("ProjectCommands")({
|
||||
start: Schema.optional(
|
||||
Schema.String.annotate({ description: "Startup script to run when creating a new workspace (worktree)" }),
|
||||
),
|
||||
}) {
|
||||
static readonly zod = zod(this)
|
||||
}
|
||||
|
||||
export class Time extends Schema.Class<Time>("ProjectTime")({
|
||||
created: Schema.Number,
|
||||
updated: Schema.Number,
|
||||
initialized: Schema.optional(Schema.Number),
|
||||
}) {
|
||||
static readonly zod = zod(this)
|
||||
}
|
||||
|
||||
export class Info extends Schema.Class<Info>("Project")({
|
||||
id: ProjectID,
|
||||
worktree: Schema.String,
|
||||
vcs: Schema.optional(Schema.Literal("git")),
|
||||
name: Schema.optional(Schema.String),
|
||||
icon: Schema.optional(Icon),
|
||||
commands: Schema.optional(Commands),
|
||||
time: Time,
|
||||
sandboxes: Schema.mutable(Schema.Array(Schema.String)),
|
||||
}) {
|
||||
static readonly zod = zod(this)
|
||||
}
|
||||
|
||||
export const Event = {
|
||||
Updated: BusEvent.define("project.updated", Info),
|
||||
Updated: BusEvent.define("project.updated", Info.zod),
|
||||
}
|
||||
|
||||
type Row = typeof ProjectTable.$inferSelect
|
||||
@@ -58,10 +66,10 @@ export namespace Project {
|
||||
row.icon_url || row.icon_color
|
||||
? { url: row.icon_url ?? undefined, color: row.icon_color ?? undefined }
|
||||
: undefined
|
||||
return {
|
||||
return new Info({
|
||||
id: row.id,
|
||||
worktree: row.worktree,
|
||||
vcs: row.vcs ? Info.shape.vcs.parse(row.vcs) : undefined,
|
||||
vcs: row.vcs === "git" ? "git" : undefined,
|
||||
name: row.name ?? undefined,
|
||||
icon,
|
||||
time: {
|
||||
@@ -71,14 +79,14 @@ export namespace Project {
|
||||
},
|
||||
sandboxes: row.sandboxes,
|
||||
commands: row.commands ?? undefined,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const UpdateInput = z.object({
|
||||
projectID: ProjectID.zod,
|
||||
name: z.string().optional(),
|
||||
icon: Info.shape.icon.optional(),
|
||||
commands: Info.shape.commands.optional(),
|
||||
icon: Icon.zod.optional(),
|
||||
commands: Commands.zod.optional(),
|
||||
})
|
||||
export type UpdateInput = z.infer<typeof UpdateInput>
|
||||
|
||||
@@ -142,7 +150,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) => {
|
||||
if (!name) return cwd
|
||||
@@ -249,27 +257,21 @@ export namespace Project {
|
||||
const row = yield* db((d) => d.select().from(ProjectTable).where(eq(ProjectTable.id, data.id)).get())
|
||||
const existing = row
|
||||
? fromRow(row)
|
||||
: {
|
||||
: new Info({
|
||||
id: data.id,
|
||||
worktree: data.worktree,
|
||||
vcs: data.vcs,
|
||||
sandboxes: [] as string[],
|
||||
sandboxes: [],
|
||||
time: { created: Date.now(), updated: Date.now() },
|
||||
}
|
||||
})
|
||||
|
||||
if (Flag.OPENCODE_EXPERIMENTAL_ICON_DISCOVERY)
|
||||
yield* discover(existing).pipe(Effect.ignore, Effect.forkIn(scope))
|
||||
|
||||
const result: Info = {
|
||||
...existing,
|
||||
worktree: data.worktree,
|
||||
vcs: data.vcs,
|
||||
time: { ...existing.time, updated: Date.now() },
|
||||
}
|
||||
if (data.sandbox !== result.worktree && !result.sandboxes.includes(data.sandbox))
|
||||
result.sandboxes.push(data.sandbox)
|
||||
result.sandboxes = yield* Effect.forEach(
|
||||
result.sandboxes,
|
||||
const sandboxes = yield* Effect.forEach(
|
||||
data.sandbox !== existing.worktree && !existing.sandboxes.includes(data.sandbox)
|
||||
? [...existing.sandboxes, data.sandbox]
|
||||
: existing.sandboxes,
|
||||
(s) =>
|
||||
fs.exists(s).pipe(
|
||||
Effect.orDie,
|
||||
@@ -278,6 +280,14 @@ export namespace Project {
|
||||
{ concurrency: "unbounded" },
|
||||
).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,
|
||||
})
|
||||
|
||||
yield* db((d) =>
|
||||
d
|
||||
.insert(ProjectTable)
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { lazy } from "@/util/lazy"
|
||||
import { Hono } from "hono"
|
||||
import { ProjectHttpApiHandler } from "./project"
|
||||
import { QuestionHttpApiHandler } from "./question"
|
||||
|
||||
export const HttpApiRoutes = lazy(() =>
|
||||
new Hono().all("/question", QuestionHttpApiHandler).all("/question/*", QuestionHttpApiHandler),
|
||||
new Hono()
|
||||
.all("/question", QuestionHttpApiHandler)
|
||||
.all("/question/*", QuestionHttpApiHandler)
|
||||
.all("/project", ProjectHttpApiHandler)
|
||||
.all("/project/*", ProjectHttpApiHandler),
|
||||
)
|
||||
|
||||
83
packages/opencode/src/server/instance/httpapi/project.ts
Normal file
83
packages/opencode/src/server/instance/httpapi/project.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { AppLayer } from "@/effect/app-runtime"
|
||||
import { memoMap } from "@/effect/run-service"
|
||||
import { Instance } from "@/project/instance"
|
||||
import { Project } from "@/project/project"
|
||||
import { lazy } from "@/util/lazy"
|
||||
import { Effect, Layer, Schema } from "effect"
|
||||
import { HttpRouter, HttpServer } from "effect/unstable/http"
|
||||
import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
|
||||
import type { Handler } from "hono"
|
||||
|
||||
const root = "/experimental/httpapi/project"
|
||||
|
||||
const Api = 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: "Experimental HttpApi project routes.",
|
||||
}),
|
||||
),
|
||||
)
|
||||
.annotateMerge(
|
||||
OpenApi.annotations({
|
||||
title: "opencode experimental HttpApi",
|
||||
version: "0.0.1",
|
||||
description: "Experimental HttpApi surface for selected instance routes.",
|
||||
}),
|
||||
)
|
||||
|
||||
const list = Effect.fn("ProjectHttpApi.list")(function* () {
|
||||
return Project.list()
|
||||
})
|
||||
|
||||
const current = Effect.fn("ProjectHttpApi.current")(function* () {
|
||||
return Instance.project
|
||||
})
|
||||
|
||||
const ProjectLive = HttpApiBuilder.group(
|
||||
Api,
|
||||
"project",
|
||||
Effect.fn("ProjectHttpApi.handlers")(function* (handlers) {
|
||||
return handlers.handle("list", list).handle("current", current)
|
||||
}),
|
||||
)
|
||||
|
||||
const web = lazy(() =>
|
||||
HttpRouter.toWebHandler(
|
||||
Layer.mergeAll(
|
||||
AppLayer,
|
||||
HttpApiBuilder.layer(Api, { openapiPath: `${root}/doc` }).pipe(
|
||||
Layer.provide(ProjectLive),
|
||||
Layer.provide(HttpServer.layerServices),
|
||||
),
|
||||
),
|
||||
{
|
||||
disableLogger: true,
|
||||
memoMap,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
export const ProjectHttpApiHandler: Handler = (c, _next) => web().handler(c.req.raw)
|
||||
@@ -23,7 +23,7 @@ export const ProjectRoutes = lazy(() =>
|
||||
description: "List of projects",
|
||||
content: {
|
||||
"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",
|
||||
content: {
|
||||
"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",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(Project.Info),
|
||||
schema: resolver(Project.Info.zod),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -99,7 +99,7 @@ export const ProjectRoutes = lazy(() =>
|
||||
description: "Updated project information",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(Project.Info),
|
||||
schema: resolver(Project.Info.zod),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
39
packages/opencode/test/server/project-httpapi.test.ts
Normal file
39
packages/opencode/test/server/project-httpapi.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { Server } from "../../src/server/server"
|
||||
import { tmpdir } from "../fixture/fixture"
|
||||
import { Log } from "../../src/util/log"
|
||||
|
||||
Log.init({ print: false })
|
||||
|
||||
afterEach(async () => {
|
||||
await Instance.disposeAll()
|
||||
})
|
||||
|
||||
describe("experimental project httpapi", () => {
|
||||
test("lists projects, returns current project, and serves docs", async () => {
|
||||
await using tmp = await tmpdir({ git: true })
|
||||
const app = Server.Default().app
|
||||
const headers = {
|
||||
"content-type": "application/json",
|
||||
"x-opencode-directory": tmp.path,
|
||||
}
|
||||
|
||||
const list = await app.request("/experimental/httpapi/project", { headers })
|
||||
expect(list.status).toBe(200)
|
||||
const items = await list.json()
|
||||
expect(items.length).toBeGreaterThan(0)
|
||||
expect(items[0].worktree).toBeDefined()
|
||||
|
||||
const current = await app.request("/experimental/httpapi/project/current", { headers })
|
||||
expect(current.status).toBe(200)
|
||||
const project = await current.json()
|
||||
expect(project.worktree).toBe(tmp.path)
|
||||
|
||||
const doc = await app.request("/experimental/httpapi/project/doc", { headers })
|
||||
expect(doc.status).toBe(200)
|
||||
const spec = await doc.json()
|
||||
expect(spec.paths["/experimental/httpapi/project"]?.get?.operationId).toBe("project.list")
|
||||
expect(spec.paths["/experimental/httpapi/project/current"]?.get?.operationId).toBe("project.current")
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user