Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/opencode/src/account/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Schema } from "effect"
import { withStatics } from "@/util/schema"

export const AccountID = Schema.String.pipe(
Schema.brand("AccountId"),
Schema.brand("AccountID"),
withStatics((s) => ({ make: (id: string) => s.makeUnsafe(id) })),
)
export type AccountID = Schema.Schema.Type<typeof AccountID>

export const OrgID = Schema.String.pipe(
Schema.brand("OrgId"),
Schema.brand("OrgID"),
withStatics((s) => ({ make: (id: string) => s.makeUnsafe(id) })),
)
export type OrgID = Schema.Schema.Type<typeof OrgID>
Expand Down
4 changes: 2 additions & 2 deletions packages/opencode/src/control-plane/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import z from "zod"
import { withStatics } from "@/util/schema"
import { Identifier } from "@/id/id"

const workspaceIdSchema = Schema.String.pipe(Schema.brand("WorkspaceId"))
const workspaceIdSchema = Schema.String.pipe(Schema.brand("WorkspaceID"))

export type WorkspaceID = typeof workspaceIdSchema.Type

export const WorkspaceID = workspaceIdSchema.pipe(
withStatics((schema: typeof workspaceIdSchema) => ({
make: (id: string) => schema.makeUnsafe(id),
ascending: (id?: string) => schema.makeUnsafe(Identifier.ascending("workspace", id)),
zod: z.string().startsWith("wrk").pipe(z.custom<WorkspaceID>()),
zod: Identifier.schema("workspace").pipe(z.custom<WorkspaceID>()),
})),
)
8 changes: 4 additions & 4 deletions packages/opencode/src/permission/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Bus } from "@/bus"
import { SessionID, MessageID } from "@/session/schema"
import z from "zod"
import { Log } from "../util/log"
import { Identifier } from "../id/id"
import { Plugin } from "../plugin"
import { Instance } from "../project/instance"
import { Wildcard } from "../util/wildcard"
import { PermissionID } from "./schema"

export namespace Permission {
const log = Log.create({ service: "permission" })
Expand All @@ -22,7 +22,7 @@ export namespace Permission {

export const Info = z
.object({
id: z.string(),
id: PermissionID.zod,
type: z.string(),
pattern: z.union([z.string(), z.array(z.string())]).optional(),
sessionID: SessionID.zod,
Expand All @@ -45,7 +45,7 @@ export namespace Permission {
"permission.replied",
z.object({
sessionID: SessionID.zod,
permissionID: z.string(),
permissionID: PermissionID.zod,
response: z.string(),
}),
),
Expand Down Expand Up @@ -118,7 +118,7 @@ export namespace Permission {
const keys = toKeys(input.pattern, input.type)
if (covered(keys, approvedForSession)) return
const info: Info = {
id: Identifier.ascending("permission"),
id: PermissionID.ascending(),
type: input.type,
pattern: input.pattern,
sessionID: input.sessionID,
Expand Down
10 changes: 5 additions & 5 deletions packages/opencode/src/permission/next.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Bus } from "@/bus"
import { BusEvent } from "@/bus/bus-event"
import { Config } from "@/config/config"
import { Identifier } from "@/id/id"
import { SessionID, MessageID } from "@/session/schema"
import { PermissionID } from "./schema"
import { Instance } from "@/project/instance"
import { Database, eq } from "@/storage/db"
import { PermissionTable } from "@/session/session.sql"
Expand Down Expand Up @@ -69,7 +69,7 @@ export namespace PermissionNext {

export const Request = z
.object({
id: Identifier.schema("permission"),
id: PermissionID.zod,
sessionID: SessionID.zod,
permission: z.string(),
patterns: z.string().array(),
Expand Down Expand Up @@ -102,7 +102,7 @@ export namespace PermissionNext {
"permission.replied",
z.object({
sessionID: SessionID.zod,
requestID: z.string(),
requestID: PermissionID.zod,
reply: Reply,
}),
),
Expand Down Expand Up @@ -143,7 +143,7 @@ export namespace PermissionNext {
if (rule.action === "deny")
throw new DeniedError(ruleset.filter((r) => Wildcard.match(request.permission, r.permission)))
if (rule.action === "ask") {
const id = input.id ?? Identifier.ascending("permission")
const id = input.id ?? PermissionID.ascending()
return new Promise<void>((resolve, reject) => {
const info: Request = {
id,
Expand All @@ -164,7 +164,7 @@ export namespace PermissionNext {

export const reply = fn(
z.object({
requestID: Identifier.schema("permission"),
requestID: PermissionID.zod,
reply: Reply,
message: z.string().optional(),
}),
Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/src/permission/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Schema } from "effect"
import z from "zod"

import { Identifier } from "@/id/id"
import { withStatics } from "@/util/schema"

const permissionIdSchema = Schema.String.pipe(Schema.brand("PermissionID"))

export type PermissionID = typeof permissionIdSchema.Type

export const PermissionID = permissionIdSchema.pipe(
withStatics((schema: typeof permissionIdSchema) => ({
make: (id: string) => schema.makeUnsafe(id),
ascending: (id?: string) => schema.makeUnsafe(Identifier.ascending("permission", id)),
zod: Identifier.schema("permission").pipe(z.custom<PermissionID>()),
})),
)
2 changes: 1 addition & 1 deletion packages/opencode/src/project/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import z from "zod"

import { withStatics } from "@/util/schema"

const projectIdSchema = Schema.String.pipe(Schema.brand("ProjectId"))
const projectIdSchema = Schema.String.pipe(Schema.brand("ProjectID"))

export type ProjectID = typeof projectIdSchema.Type

Expand Down
12 changes: 6 additions & 6 deletions packages/opencode/src/pty/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { BusEvent } from "@/bus/bus-event"
import { Bus } from "@/bus"
import { type IPty } from "bun-pty"
import z from "zod"
import { Identifier } from "../id/id"
import { Log } from "../util/log"
import { Instance } from "../project/instance"
import { lazy } from "@opencode-ai/util/lazy"
import { Shell } from "@/shell/shell"
import { Plugin } from "@/plugin"
import { PtyID } from "./schema"

export namespace Pty {
const log = Log.create({ service: "pty" })
Expand Down Expand Up @@ -40,7 +40,7 @@ export namespace Pty {

export const Info = z
.object({
id: Identifier.schema("pty"),
id: PtyID.zod,
title: z.string(),
command: z.string(),
args: z.array(z.string()),
Expand Down Expand Up @@ -77,8 +77,8 @@ export namespace Pty {
export const Event = {
Created: BusEvent.define("pty.created", z.object({ info: Info })),
Updated: BusEvent.define("pty.updated", z.object({ info: Info })),
Exited: BusEvent.define("pty.exited", z.object({ id: Identifier.schema("pty"), exitCode: z.number() })),
Deleted: BusEvent.define("pty.deleted", z.object({ id: Identifier.schema("pty") })),
Exited: BusEvent.define("pty.exited", z.object({ id: PtyID.zod, exitCode: z.number() })),
Deleted: BusEvent.define("pty.deleted", z.object({ id: PtyID.zod })),
}

interface ActiveSession {
Expand Down Expand Up @@ -118,7 +118,7 @@ export namespace Pty {
}

export async function create(input: CreateInput) {
const id = Identifier.create("pty", false)
const id = PtyID.ascending()
const command = input.command || Shell.preferred()
const args = input.args || []
if (command.endsWith("sh")) {
Expand Down Expand Up @@ -234,7 +234,7 @@ export namespace Pty {
}
}
session.subscribers.clear()
Bus.publish(Event.Deleted, { id })
Bus.publish(Event.Deleted, { id: session.info.id })
}

export function resize(id: string, cols: number, rows: number) {
Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/src/pty/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Schema } from "effect"
import z from "zod"

import { Identifier } from "@/id/id"
import { withStatics } from "@/util/schema"

const ptyIdSchema = Schema.String.pipe(Schema.brand("PtyID"))

export type PtyID = typeof ptyIdSchema.Type

export const PtyID = ptyIdSchema.pipe(
withStatics((schema: typeof ptyIdSchema) => ({
make: (id: string) => schema.makeUnsafe(id),
ascending: (id?: string) => schema.makeUnsafe(Identifier.ascending("pty", id)),
zod: Identifier.schema("pty").pipe(z.custom<PtyID>()),
})),
)
10 changes: 5 additions & 5 deletions packages/opencode/src/question/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Bus } from "@/bus"
import { BusEvent } from "@/bus/bus-event"
import { Identifier } from "@/id/id"
import { SessionID, MessageID } from "@/session/schema"
import { Instance } from "@/project/instance"
import { Log } from "@/util/log"
import z from "zod"
import { QuestionID } from "./schema"

export namespace Question {
const log = Log.create({ service: "question" })
Expand Down Expand Up @@ -34,7 +34,7 @@ export namespace Question {

export const Request = z
.object({
id: Identifier.schema("question"),
id: QuestionID.zod,
sessionID: SessionID.zod,
questions: z.array(Info).describe("Questions to ask"),
tool: z
Expand Down Expand Up @@ -67,15 +67,15 @@ export namespace Question {
"question.replied",
z.object({
sessionID: SessionID.zod,
requestID: z.string(),
requestID: QuestionID.zod,
answers: z.array(Answer),
}),
),
Rejected: BusEvent.define(
"question.rejected",
z.object({
sessionID: SessionID.zod,
requestID: z.string(),
requestID: QuestionID.zod,
}),
),
}
Expand All @@ -101,7 +101,7 @@ export namespace Question {
tool?: { messageID: MessageID; callID: string }
}): Promise<Answer[]> {
const s = await state()
const id = Identifier.ascending("question")
const id = QuestionID.ascending()

log.info("asking", { id, questions: input.questions.length })

Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/src/question/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Schema } from "effect"
import z from "zod"

import { Identifier } from "@/id/id"
import { withStatics } from "@/util/schema"

const questionIdSchema = Schema.String.pipe(Schema.brand("QuestionID"))

export type QuestionID = typeof questionIdSchema.Type

export const QuestionID = questionIdSchema.pipe(
withStatics((schema: typeof questionIdSchema) => ({
make: (id: string) => schema.makeUnsafe(id),
ascending: (id?: string) => schema.makeUnsafe(Identifier.ascending("question", id)),
zod: Identifier.schema("question").pipe(z.custom<QuestionID>()),
})),
)
3 changes: 2 additions & 1 deletion packages/opencode/src/server/routes/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Hono } from "hono"
import { describeRoute, validator, resolver } from "hono-openapi"
import z from "zod"
import { PermissionNext } from "@/permission/next"
import { PermissionID } from "@/permission/schema"
import { errors } from "../error"
import { lazy } from "../../util/lazy"

Expand All @@ -28,7 +29,7 @@ export const PermissionRoutes = lazy(() =>
validator(
"param",
z.object({
requestID: z.string(),
requestID: PermissionID.zod,
}),
),
validator("json", z.object({ reply: PermissionNext.Reply, message: z.string().optional() })),
Expand Down
11 changes: 6 additions & 5 deletions packages/opencode/src/server/routes/pty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describeRoute, validator, resolver } from "hono-openapi"
import { upgradeWebSocket } from "hono/bun"
import z from "zod"
import { Pty } from "@/pty"
import { PtyID } from "@/pty/schema"
import { NotFoundError } from "../../storage/db"
import { errors } from "../error"
import { lazy } from "../../util/lazy"
Expand Down Expand Up @@ -72,7 +73,7 @@ export const PtyRoutes = lazy(() =>
...errors(404),
},
}),
validator("param", z.object({ ptyID: z.string() })),
validator("param", z.object({ ptyID: PtyID.zod })),
async (c) => {
const info = Pty.get(c.req.valid("param").ptyID)
if (!info) {
Expand All @@ -99,7 +100,7 @@ export const PtyRoutes = lazy(() =>
...errors(400),
},
}),
validator("param", z.object({ ptyID: z.string() })),
validator("param", z.object({ ptyID: PtyID.zod })),
validator("json", Pty.UpdateInput),
async (c) => {
const info = await Pty.update(c.req.valid("param").ptyID, c.req.valid("json"))
Expand All @@ -124,7 +125,7 @@ export const PtyRoutes = lazy(() =>
...errors(404),
},
}),
validator("param", z.object({ ptyID: z.string() })),
validator("param", z.object({ ptyID: PtyID.zod })),
async (c) => {
await Pty.remove(c.req.valid("param").ptyID)
return c.json(true)
Expand All @@ -148,9 +149,9 @@ export const PtyRoutes = lazy(() =>
...errors(404),
},
}),
validator("param", z.object({ ptyID: z.string() })),
validator("param", z.object({ ptyID: PtyID.zod })),
upgradeWebSocket((c) => {
const id = c.req.param("ptyID")
const id = PtyID.zod.parse(c.req.param("ptyID"))
const cursor = (() => {
const value = c.req.query("cursor")
if (!value) return
Expand Down
5 changes: 3 additions & 2 deletions packages/opencode/src/server/routes/question.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from "hono"
import { describeRoute, validator } from "hono-openapi"
import { resolver } from "hono-openapi"
import { QuestionID } from "@/question/schema"
import { Question } from "../../question"
import z from "zod"
import { errors } from "../error"
Expand Down Expand Up @@ -51,7 +52,7 @@ export const QuestionRoutes = lazy(() =>
validator(
"param",
z.object({
requestID: z.string(),
requestID: QuestionID.zod,
}),
),
validator("json", Question.Reply),
Expand Down Expand Up @@ -86,7 +87,7 @@ export const QuestionRoutes = lazy(() =>
validator(
"param",
z.object({
requestID: z.string(),
requestID: QuestionID.zod,
}),
),
async (c) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/server/routes/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Agent } from "../../agent/agent"
import { Snapshot } from "@/snapshot"
import { Log } from "../../util/log"
import { PermissionNext } from "@/permission/next"
import { PermissionID } from "@/permission/schema"
import { errors } from "../error"
import { lazy } from "../../util/lazy"

Expand Down Expand Up @@ -957,7 +958,7 @@ export const SessionRoutes = lazy(() =>
"param",
z.object({
sessionID: SessionID.zod,
permissionID: z.string(),
permissionID: PermissionID.zod,
}),
),
validator("json", z.object({ response: PermissionNext.Reply })),
Expand Down
Loading
Loading