Skip to content

Commit 57edb0d

Browse files
committed
sync
1 parent a614b78 commit 57edb0d

19 files changed

Lines changed: 340 additions & 251 deletions

File tree

AGENTS.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919

2020
### Naming
2121

22-
Prefer single word variable names. Only use multiple words if necessary.
22+
Prefer single word names for variables and functions. Only use multiple words if necessary.
2323

2424
```ts
2525
// Good
2626
const foo = 1
27-
const bar = 2
27+
function journal(dir: string) {}
2828

2929
// Bad
3030
const fooBar = 1
31-
const barBaz = 2
31+
function prepareJournal(dir: string) {}
3232
```
3333

3434
Reduce total variable count by inlining when a value is only used once.
@@ -87,6 +87,26 @@ function foo() {
8787
}
8888
```
8989

90+
### Schema Definitions (Drizzle)
91+
92+
Use snake_case for field names so column names don't need to be redefined as strings.
93+
94+
```ts
95+
// Good
96+
const table = sqliteTable("session", {
97+
id: text().primaryKey(),
98+
project_id: text().notNull(),
99+
created_at: integer().notNull(),
100+
})
101+
102+
// Bad
103+
const table = sqliteTable("session", {
104+
id: text("id").primaryKey(),
105+
projectID: text("project_id").notNull(),
106+
createdAt: integer("created_at").notNull(),
107+
})
108+
```
109+
90110
## Testing
91111

92112
- Avoid mocks as much as possible

packages/opencode/src/cli/cmd/database.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const ExportCommand = cmd({
6565
// Export sessions (organized by projectID)
6666
const sessionDir = path.join(outDir, "session")
6767
for (const row of Database.use((db) => db.select().from(SessionTable).all())) {
68-
const dir = path.join(sessionDir, row.projectID)
68+
const dir = path.join(sessionDir, row.project_id)
6969
await fs.mkdir(dir, { recursive: true })
7070
await Bun.write(path.join(dir, `${row.id}.json`), JSON.stringify(Session.fromRow(row), null, 2))
7171
stats.sessions++
@@ -74,7 +74,7 @@ const ExportCommand = cmd({
7474
// Export messages (organized by sessionID)
7575
const messageDir = path.join(outDir, "message")
7676
for (const row of Database.use((db) => db.select().from(MessageTable).all())) {
77-
const dir = path.join(messageDir, row.sessionID)
77+
const dir = path.join(messageDir, row.session_id)
7878
await fs.mkdir(dir, { recursive: true })
7979
await Bun.write(path.join(dir, `${row.id}.json`), JSON.stringify(row.data, null, 2))
8080
stats.messages++
@@ -83,7 +83,7 @@ const ExportCommand = cmd({
8383
// Export parts (organized by messageID)
8484
const partDir = path.join(outDir, "part")
8585
for (const row of Database.use((db) => db.select().from(PartTable).all())) {
86-
const dir = path.join(partDir, row.messageID)
86+
const dir = path.join(partDir, row.message_id)
8787
await fs.mkdir(dir, { recursive: true })
8888
await Bun.write(path.join(dir, `${row.id}.json`), JSON.stringify(row.data, null, 2))
8989
stats.parts++
@@ -93,39 +93,39 @@ const ExportCommand = cmd({
9393
const diffDir = path.join(outDir, "session_diff")
9494
await fs.mkdir(diffDir, { recursive: true })
9595
for (const row of Database.use((db) => db.select().from(SessionDiffTable).all())) {
96-
await Bun.write(path.join(diffDir, `${row.sessionID}.json`), JSON.stringify(row.data, null, 2))
96+
await Bun.write(path.join(diffDir, `${row.session_id}.json`), JSON.stringify(row.data, null, 2))
9797
stats.diffs++
9898
}
9999

100100
// Export todos
101101
const todoDir = path.join(outDir, "todo")
102102
await fs.mkdir(todoDir, { recursive: true })
103103
for (const row of Database.use((db) => db.select().from(TodoTable).all())) {
104-
await Bun.write(path.join(todoDir, `${row.sessionID}.json`), JSON.stringify(row.data, null, 2))
104+
await Bun.write(path.join(todoDir, `${row.session_id}.json`), JSON.stringify(row.data, null, 2))
105105
stats.todos++
106106
}
107107

108108
// Export permissions
109109
const permDir = path.join(outDir, "permission")
110110
await fs.mkdir(permDir, { recursive: true })
111111
for (const row of Database.use((db) => db.select().from(PermissionTable).all())) {
112-
await Bun.write(path.join(permDir, `${row.projectID}.json`), JSON.stringify(row.data, null, 2))
112+
await Bun.write(path.join(permDir, `${row.project_id}.json`), JSON.stringify(row.data, null, 2))
113113
stats.permissions++
114114
}
115115

116116
// Export session shares
117117
const sessionShareDir = path.join(outDir, "session_share")
118118
await fs.mkdir(sessionShareDir, { recursive: true })
119119
for (const row of Database.use((db) => db.select().from(SessionShareTable).all())) {
120-
await Bun.write(path.join(sessionShareDir, `${row.sessionID}.json`), JSON.stringify(row.data, null, 2))
120+
await Bun.write(path.join(sessionShareDir, `${row.session_id}.json`), JSON.stringify(row.data, null, 2))
121121
stats.sessionShares++
122122
}
123123

124124
// Export shares
125125
const shareDir = path.join(outDir, "share")
126126
await fs.mkdir(shareDir, { recursive: true })
127127
for (const row of Database.use((db) => db.select().from(ShareTable).all())) {
128-
await Bun.write(path.join(shareDir, `${row.sessionID}.json`), JSON.stringify(row.data, null, 2))
128+
await Bun.write(path.join(shareDir, `${row.session_id}.json`), JSON.stringify(row.data, null, 2))
129129
stats.shares++
130130
}
131131

packages/opencode/src/cli/cmd/import.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export const ImportCommand = cmd({
9090
.insert(MessageTable)
9191
.values({
9292
id: msg.info.id,
93-
sessionID: exportData.info.id,
94-
createdAt: msg.info.time?.created ?? Date.now(),
93+
session_id: exportData.info.id,
94+
created_at: msg.info.time?.created ?? Date.now(),
9595
data: msg.info,
9696
})
9797
.onConflictDoNothing()
@@ -104,8 +104,8 @@ export const ImportCommand = cmd({
104104
.insert(PartTable)
105105
.values({
106106
id: part.id,
107-
messageID: msg.info.id,
108-
sessionID: exportData.info.id,
107+
message_id: msg.info.id,
108+
session_id: exportData.info.id,
109109
data: part,
110110
})
111111
.onConflictDoNothing()

packages/opencode/src/permission/next.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export namespace PermissionNext {
109109
const state = Instance.state(() => {
110110
const projectID = Instance.project.id
111111
const row = Database.use((db) =>
112-
db.select().from(PermissionTable).where(eq(PermissionTable.projectID, projectID)).get(),
112+
db.select().from(PermissionTable).where(eq(PermissionTable.project_id, projectID)).get(),
113113
)
114114
const stored = row?.data ?? ([] as Ruleset)
115115

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
22

33
export const ProjectTable = sqliteTable("project", {
4-
id: text("id").primaryKey(),
5-
worktree: text("worktree").notNull(),
6-
vcs: text("vcs"),
7-
name: text("name"),
8-
icon_url: text("icon_url"),
9-
icon_color: text("icon_color"),
10-
time_created: integer("time_created").notNull(),
11-
time_updated: integer("time_updated").notNull(),
12-
time_initialized: integer("time_initialized"),
13-
sandboxes: text("sandboxes", { mode: "json" }).notNull().$type<string[]>(),
4+
id: text().primaryKey(),
5+
worktree: text().notNull(),
6+
vcs: text(),
7+
name: text(),
8+
icon_url: text(),
9+
icon_color: text(),
10+
time_created: integer().notNull(),
11+
time_updated: integer().notNull(),
12+
time_initialized: integer(),
13+
sandboxes: text({ mode: "json" }).notNull().$type<string[]>(),
1414
})

packages/opencode/src/project/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export namespace Project {
299299
if (!globalRow) return
300300

301301
const globalSessions = Database.use((db) =>
302-
db.select().from(SessionTable).where(eq(SessionTable.projectID, "global")).all(),
302+
db.select().from(SessionTable).where(eq(SessionTable.project_id, "global")).all(),
303303
)
304304
if (globalSessions.length === 0) return
305305

@@ -311,7 +311,7 @@ export namespace Project {
311311

312312
log.info("migrating session", { sessionID: row.id, from: "global", to: newProjectID })
313313
Database.use((db) =>
314-
db.update(SessionTable).set({ projectID: newProjectID }).where(eq(SessionTable.id, row.id)).run(),
314+
db.update(SessionTable).set({ project_id: newProjectID }).where(eq(SessionTable.id, row.id)).run(),
315315
)
316316
}).catch((error) => {
317317
log.error("failed to migrate sessions from global to project", { error, projectId: newProjectID })

packages/opencode/src/server/routes/session.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,15 @@ export const SessionRoutes = lazy(() =>
276276
const sessionID = c.req.valid("param").sessionID
277277
const updates = c.req.valid("json")
278278

279-
const updatedSession = await Session.update(
280-
sessionID,
281-
(session) => {
282-
if (updates.title !== undefined) {
283-
session.title = updates.title
284-
}
285-
if (updates.time?.archived !== undefined) session.time.archived = updates.time.archived
286-
},
287-
{ touch: false },
288-
)
279+
let session = await Session.get(sessionID)
280+
if (updates.title !== undefined) {
281+
session = await Session.setTitle({ sessionID, title: updates.title })
282+
}
283+
if (updates.time?.archived !== undefined) {
284+
session = await Session.setArchived({ sessionID, time: updates.time.archived })
285+
}
289286

290-
return c.json(updatedSession)
287+
return c.json(session)
291288
},
292289
)
293290
.post(

0 commit comments

Comments
 (0)