Skip to content

Commit 10998d6

Browse files
committed
core: improve session API reliability with proper input validation
1 parent aee2401 commit 10998d6

6 files changed

Lines changed: 139 additions & 118 deletions

File tree

‎packages/opencode/src/cli/cmd/run.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const RunCommand = cmd({
106106

107107
if (args.session) return Session.get(args.session)
108108

109-
return Session.create()
109+
return Session.create({})
110110
})()
111111

112112
if (!session) {

‎packages/opencode/src/server/server.ts‎

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import { SessionRevert } from "../session/revert"
3131
import { lazy } from "../util/lazy"
3232
import { Todo } from "../session/todo"
3333
import { InstanceBootstrap } from "../project/bootstrap"
34-
import { Identifier } from "@/id/id"
3534

3635
const ERRORS = {
3736
400: {
@@ -308,7 +307,7 @@ export namespace Server {
308307
validator(
309308
"param",
310309
z.object({
311-
id: z.string(),
310+
id: Session.get.schema,
312311
}),
313312
),
314313
async (c) => {
@@ -336,7 +335,7 @@ export namespace Server {
336335
validator(
337336
"param",
338337
z.object({
339-
id: z.string(),
338+
id: Session.children.schema,
340339
}),
341340
),
342341
async (c) => {
@@ -390,18 +389,10 @@ export namespace Server {
390389
},
391390
},
392391
}),
393-
validator(
394-
"json",
395-
z
396-
.object({
397-
parentID: z.string().optional(),
398-
title: z.string().optional(),
399-
})
400-
.optional(),
401-
),
392+
validator("json", Session.create.schema.optional()),
402393
async (c) => {
403394
const body = c.req.valid("json") ?? {}
404-
const session = await Session.create(body.parentID, body.title)
395+
const session = await Session.create(body)
405396
return c.json(session)
406397
},
407398
)
@@ -424,7 +415,7 @@ export namespace Server {
424415
validator(
425416
"param",
426417
z.object({
427-
id: z.string(),
418+
id: Session.remove.schema,
428419
}),
429420
),
430421
async (c) => {
@@ -495,14 +486,7 @@ export namespace Server {
495486
id: z.string().meta({ description: "Session ID" }),
496487
}),
497488
),
498-
validator(
499-
"json",
500-
z.object({
501-
messageID: z.string(),
502-
providerID: z.string(),
503-
modelID: z.string(),
504-
}),
505-
),
489+
validator("json", Session.initialize.schema.omit({ sessionID: true })),
506490
async (c) => {
507491
const sessionID = c.req.valid("param").id
508492
const body = c.req.valid("json")
@@ -529,7 +513,7 @@ export namespace Server {
529513
validator(
530514
"param",
531515
z.object({
532-
id: Identifier.schema("session").meta({ description: "Session ID" }),
516+
id: Session.fork.schema.shape.sessionID,
533517
}),
534518
),
535519
validator("json", Session.fork.schema.omit({ sessionID: true })),
@@ -614,7 +598,7 @@ export namespace Server {
614598
validator(
615599
"param",
616600
z.object({
617-
id: z.string(),
601+
id: Session.unshare.schema,
618602
}),
619603
),
620604
async (c) => {
@@ -717,7 +701,7 @@ export namespace Server {
717701
),
718702
async (c) => {
719703
const params = c.req.valid("param")
720-
const message = await Session.getMessage(params.id, params.messageID)
704+
const message = await Session.getMessage({ sessionID: params.id, messageID: params.messageID })
721705
return c.json(message)
722706
},
723707
)

‎packages/opencode/src/session/compaction.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export namespace SessionCompaction {
144144
},
145145
],
146146
})
147-
const usage = Session.getUsage(model.info, generated.usage, generated.providerMetadata)
147+
const usage = Session.getUsage({ model: model.info, usage: generated.usage, metadata: generated.providerMetadata })
148148
msg.cost += usage.cost
149149
msg.tokens = usage.tokens
150150
msg.summary = true

0 commit comments

Comments
 (0)