fix(schema): accept undefined actor and origin in prompt metadata

This commit is contained in:
Kit Langton 2026-11-03 16:27:01 -08:00
parent 19cd88fb5c
commit 6e35c721f2
3 changed files with 14 additions and 4 deletions

View file

@ -218,8 +218,11 @@ export type SessionPromptInput = {
readonly metadata?:
| ({
readonly "opencode.origin"?:
| { readonly from: string; readonly via?: never; readonly actor?: string }
| { readonly via: string; readonly from?: never; readonly actor?: string }
| (
| { readonly from: string; readonly via?: never; readonly actor?: string | undefined }
| { readonly via: string; readonly from?: never; readonly actor?: string | undefined }
)
| undefined
} & { readonly [x: string]: unknown })
| undefined
readonly delivery?: SessionInbox.Delivery | undefined

View file

@ -1,6 +1,7 @@
export * as MessageOrigin from "./message-origin.js"
import { Schema } from "effect"
import { optional } from "./schema.js"
/** The user-message metadata key that holds a {@link MessageOrigin.Info}. */
export const Key = "opencode.origin"
@ -17,7 +18,7 @@ export const Value = Schema.String.check(
).annotate({ identifier: "MessageOrigin.Value" })
// `actor` names whoever sent it when that isn't the person who owns the session.
const actor = Schema.optionalKey(Value)
const actor = Value.pipe(optional)
export interface From extends Schema.Schema.Type<typeof From> {}
/** A machine sent the message: another session, a schedule, a tool. */
@ -40,7 +41,7 @@ export const Info = Schema.Union([From, Via]).annotate({ identifier: "MessageOri
export type Info = From | Via
/** User-message metadata whose {@link Key} entry, when present, is a valid origin. */
export const Metadata = Schema.StructWithRest(Schema.Struct({ [Key]: Schema.optionalKey(Info) }), [
export const Metadata = Schema.StructWithRest(Schema.Struct({ [Key]: Info.pipe(optional) }), [
Schema.Record(Schema.String, Schema.Unknown),
]).annotate({ identifier: "MessageOrigin.Metadata" })
export type Metadata = typeof Metadata.Type

View file

@ -38,6 +38,12 @@ test("metadata keeps other keys and checks only the origin", () => {
[MessageOrigin.Key]: { via: "voice:ios" },
source: "api",
})
expect(Schema.encodeSync(MessageOrigin.Metadata)({ [MessageOrigin.Key]: { via: "seer", actor: undefined } })).toEqual(
{ [MessageOrigin.Key]: { via: "seer" } },
)
expect(Schema.encodeSync(MessageOrigin.Metadata)({ [MessageOrigin.Key]: undefined, source: "api" })).toEqual({
source: "api",
})
expect(() => decodeMetadata({ [MessageOrigin.Key]: { kind: "session", sessionID: "ses_1" } })).toThrow()
expect(() => decodeMetadata({ [MessageOrigin.Key]: { from: "a", via: "b" } })).toThrow()
})