mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(core): keep nested permission metadata JSON-encodable (#66391)
This commit is contained in:
parent
f91949daee
commit
b5ae67aecb
3 changed files with 28 additions and 9 deletions
|
|
@ -1041,7 +1041,7 @@ const layer = Layer.effect(
|
|||
if (Option.isNone(at)) return yield* new InvalidError({ reason: `Invalid time: ${trigger.at}` })
|
||||
if (at.value <= now) return yield* new InvalidError({ reason: `Time is in the past: ${trigger.at}` })
|
||||
const stored: Trigger = RELATIVE.test(trigger.at.trim())
|
||||
? { type: "at", at: new Date(at.value).toISOString(), tz: trigger.tz }
|
||||
? { ...trigger, at: new Date(at.value).toISOString() }
|
||||
: trigger
|
||||
return { trigger: stored, due: at.value }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
export * as Permission from "./permission.js"
|
||||
|
||||
import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import { isRecord } from "@opencode/util/record"
|
||||
import { Context, Deferred, Effect, Layer, Schema, Scope } from "effect"
|
||||
import { Permission } from "@opencode/schema/permission"
|
||||
import type { Tool } from "@opencode/schema/tool"
|
||||
|
|
@ -246,11 +247,7 @@ const layer = Layer.effect(
|
|||
action: input.action,
|
||||
resources: input.resources,
|
||||
save: input.save,
|
||||
// Tools copy optional inputs straight into metadata; undefined values are not JSON and would
|
||||
// fail the HttpApi response encoder when clients list pending requests.
|
||||
metadata:
|
||||
input.metadata &&
|
||||
Object.fromEntries(Object.entries(input.metadata).filter(([, value]) => value !== undefined)),
|
||||
metadata: input.metadata && withoutUndefined(input.metadata),
|
||||
source: row(input.source),
|
||||
message,
|
||||
}
|
||||
|
|
@ -412,6 +409,16 @@ const layer = Layer.effect(
|
|||
}),
|
||||
)
|
||||
|
||||
// Tools copy optional inputs straight into metadata, nested objects included; undefined values are not JSON
|
||||
// and would fail the HttpApi response encoder when clients list pending requests.
|
||||
function withoutUndefined(record: Record<string, unknown>): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(record)
|
||||
.filter(([, value]) => value !== undefined)
|
||||
.map(([key, value]) => [key, isRecord(value) ? withoutUndefined(value) : value]),
|
||||
)
|
||||
}
|
||||
|
||||
export const node = makeLocationNode({
|
||||
service: Service,
|
||||
layer,
|
||||
|
|
|
|||
|
|
@ -650,9 +650,18 @@ describe("Permission", () => {
|
|||
const { service, request } = yield* waitForRequest({
|
||||
action: "glob",
|
||||
resources: ["/project"],
|
||||
metadata: { root: "/project", path: undefined, hidden: undefined, limit: undefined },
|
||||
metadata: {
|
||||
root: "/project",
|
||||
path: undefined,
|
||||
hidden: undefined,
|
||||
limit: undefined,
|
||||
trigger: { type: "at", at: "2026-11-25T12:00:00.000Z", tz: undefined },
|
||||
},
|
||||
})
|
||||
expect(request.metadata).toStrictEqual({
|
||||
root: "/project",
|
||||
trigger: { type: "at", at: "2026-11-25T12:00:00.000Z" },
|
||||
})
|
||||
expect(request.metadata).toStrictEqual({ root: "/project" })
|
||||
expect(yield* service.forSession(request.sessionID)).toEqual([request])
|
||||
// HttpApi encodes JSON success bodies through Schema.toCodecJson, which rejects undefined values in Unknown.
|
||||
const encode = Schema.encodeEffect(Schema.toCodecJson(Schema.Struct({ data: Schema.Array(Permission.Request) })))
|
||||
|
|
@ -663,7 +672,10 @@ describe("Permission", () => {
|
|||
sessionID: request.sessionID,
|
||||
action: "glob",
|
||||
resources: ["/project"],
|
||||
metadata: { root: "/project" },
|
||||
metadata: {
|
||||
root: "/project",
|
||||
trigger: { type: "at", at: "2026-11-25T12:00:00.000Z" },
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue