Skip to content

Commit 69e00d1

Browse files
committed
fix(plugin): expose session forms to server plugins
SessionDomain picked a fixed key list from SessionApi that omitted form, so ctx.session.form was undefined on server plugins and a plugin could not answer question tool forms from outside the terminal (permission requests already had ctx.permission.reply; forms had no equivalent). This adds form to SessionDomain on the Promise and Effect plugin surfaces, bridges list/create/get/reply/cancel through Form.Service in the plugin host with a session-ownership check matching the server handlers, wires the promise adapter, and adds the form surface to the plugins docs (also fixing the ctx.session.rename and permission.reply reply/decision examples the report flagged). Fixes anomalyco#51108
1 parent f74512b commit 69e00d1

6 files changed

Lines changed: 71 additions & 2 deletions

File tree

‎packages/core/src/plugin/host.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { AISDK } from "../aisdk.js"
1212
import { Command } from "../command.js"
1313
import { Credential } from "../credential.js"
1414
import { Bus } from "../bus.js"
15+
import { Form } from "../form.js"
1516
import { Integration } from "../integration.js"
1617
import { KV } from "../kv.js"
1718
import { Location } from "../location.js"
@@ -69,6 +70,7 @@ export const make = Effect.fn("PluginHost.make")(function* (
6970
const permission = yield* Permission.Service
7071
const hooks = yield* PluginHooks.Service
7172
const sessions = yield* Session.Service
73+
const forms = yield* Form.Service
7274
const persistentPty = yield* PersistentPty.Service
7375
const locations = yield* LocationServiceMap.Service
7476
const worktrees = yield* Worktree.Service
@@ -566,6 +568,40 @@ export const make = Effect.fn("PluginHost.make")(function* (
566568
.pipe(Effect.map((interrupted) => ({ interrupted }))),
567569
wait: (input) => sessions.wait(input.sessionID),
568570
context: (input) => sessions.context(input.sessionID),
571+
form: {
572+
list: (input) => forms.list({ sessionID: input.sessionID }),
573+
create: (input) =>
574+
forms.create({
575+
id: input.id,
576+
sessionID: input.sessionID,
577+
title: input.title,
578+
metadata: input.metadata,
579+
fields: input.fields,
580+
}),
581+
get: (input) =>
582+
forms.get(input.formID).pipe(
583+
Effect.flatMap((info) => {
584+
if (info.sessionID !== input.sessionID) return Effect.fail(new Form.NotFoundError({ id: input.formID }))
585+
return Effect.map(forms.state(input.formID), (state) => ({ ...info, state }))
586+
}),
587+
),
588+
reply: (input) =>
589+
forms.get(input.formID).pipe(
590+
Effect.flatMap((info) =>
591+
info.sessionID !== input.sessionID
592+
? Effect.fail(new Form.NotFoundError({ id: input.formID }))
593+
: forms.reply({ id: input.formID, answer: input.answer }),
594+
),
595+
),
596+
cancel: (input) =>
597+
forms.get(input.formID).pipe(
598+
Effect.flatMap((info) =>
599+
info.sessionID !== input.sessionID
600+
? Effect.fail(new Form.NotFoundError({ id: input.formID }))
601+
: forms.cancel(input.formID),
602+
),
603+
),
604+
},
569605
},
570606
}
571607
return context
@@ -579,6 +615,7 @@ export const requirements = LayerNode.group([
579615
Model.node,
580616
Command.node,
581617
Bus.node,
618+
Form.node,
582619
Integration.node,
583620
KV.node,
584621
Mcp.node,

‎packages/core/test/plugin/host.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ export function host(overrides: Overrides = {}): Plugin.Context {
179179
interrupt: overrides.session?.interrupt ?? (() => Effect.die("unused session.interrupt")),
180180
wait: overrides.session?.wait ?? (() => Effect.die("unused session.wait")),
181181
context: overrides.session?.context ?? (() => Effect.die("unused session.context")),
182+
form: overrides.session?.form ?? {
183+
list: () => Effect.die("unused session.form.list"),
184+
create: () => Effect.die("unused session.form.create"),
185+
get: () => Effect.die("unused session.form.get"),
186+
reply: () => Effect.die("unused session.form.reply"),
187+
cancel: () => Effect.die("unused session.form.cancel"),
188+
},
182189
},
183190
}
184191
}

‎packages/plugin/src/effect/session.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export type SessionDomain = Pick<
167167
| "move"
168168
| "wait"
169169
| "context"
170+
| "form"
170171
> & {
171172
readonly hook: ModelHooks<SessionHooks>
172173
}

‎packages/plugin/src/promise/adapter.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,13 @@ export function fromPromise(plugin: Plugin) {
596596
move: adaptApiMethod(SessionEndpoints["session.move"], host.session.move),
597597
wait: adaptApiMethod(SessionEndpoints["session.wait"], host.session.wait),
598598
context: adaptApiMethod(SessionEndpoints["session.context"], host.session.context),
599+
form: {
600+
list: adaptApiMethod(SessionEndpoints["session.form.list"], host.session.form.list),
601+
create: adaptApiMethod(SessionEndpoints["session.form.create"], host.session.form.create),
602+
get: adaptApiMethod(SessionEndpoints["session.form.get"], host.session.form.get),
603+
reply: adaptApiMethod(SessionEndpoints["session.form.reply"], host.session.form.reply),
604+
cancel: adaptApiMethod(SessionEndpoints["session.form.cancel"], host.session.form.cancel),
605+
},
599606
},
600607
shell: {
601608
hook: (name, callback) =>

‎packages/plugin/src/promise/session.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export type SessionDomain = Pick<
167167
| "move"
168168
| "wait"
169169
| "context"
170+
| "form"
170171
> & {
171172
readonly hook: ModelHooks<SessionHooks>
172173
}

‎services/www/src/docs/content/build/plugins/index.mdx‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ Inspect or resolve pending permission requests.
727727
```ts
728728
const pending = await ctx.permission.list({ sessionID })
729729
const request = await ctx.permission.get({ sessionID, requestID })
730-
await ctx.permission.reply({ sessionID, requestID, reply: "once" })
730+
await ctx.permission.reply({ sessionID, requestID, decision: "once" })
731731
```
732732

733733
Replace the session-scoped permission rules. They are evaluated after the agent's rules, and the
@@ -766,7 +766,7 @@ const command = await ctx.session.command({ sessionID, command: "review", argume
766766
const synthetic = await ctx.session.synthetic({ sessionID, text: "Deployment completed" })
767767
```
768768

769-
Rename, interrupt, or wait for a session.
769+
Update, interrupt, or wait for a session.
770770

771771
```ts
772772
await ctx.session.update({ sessionID, title: "Review" })
@@ -787,6 +787,14 @@ the next step boundary by default. Pass `delivery: "queue"` to preserve queue or
787787
const compaction = await ctx.session.compact({ sessionID })
788788
```
789789

790+
Answer `question` tool forms from an external system. A form appears when the agent asks a
791+
question; replying settles it and unblocks the turn.
792+
793+
```ts
794+
const pending = await ctx.session.form.list({ sessionID })
795+
await ctx.session.form.reply({ sessionID, formID: pending[0].id, answer: { choice: "yes" } })
796+
```
797+
790798
#### Reference
791799

792800
Schemas: [`Session.Info`](/api#schema-Session.Info), [`Model.Ref`](/api#schema-Model.Ref),
@@ -808,7 +816,15 @@ interface SessionContext {
808816
compact(input: SessionCompactInput, requestOptions?: RequestOptions): Promise<SessionInboxCompaction>
809817
synthetic(input: SessionSyntheticInput, requestOptions?: RequestOptions): Promise<SessionInboxSynthetic>
810818
interrupt(input: SessionInterruptInput, requestOptions?: RequestOptions): Promise<void>
819+
update(input: SessionUpdateInput, requestOptions?: RequestOptions): Promise<void>
811820
wait(input: SessionWaitInput, requestOptions?: RequestOptions): Promise<void>
821+
form: {
822+
list(input: SessionFormListInput, requestOptions?: RequestOptions): Promise<readonly FormInfo[]>
823+
create(input: SessionFormCreateInput, requestOptions?: RequestOptions): Promise<FormInfo>
824+
get(input: SessionFormGetInput, requestOptions?: RequestOptions): Promise<FormDetail>
825+
reply(input: SessionFormReplyInput, requestOptions?: RequestOptions): Promise<void>
826+
cancel(input: SessionFormCancelInput, requestOptions?: RequestOptions): Promise<void>
827+
}
812828
}
813829
```
814830

0 commit comments

Comments
 (0)