Skip to content

Commit ab605a3

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 681156c commit ab605a3

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
@@ -587,6 +589,40 @@ export const make = Effect.fn("PluginHost.make")(function* (
587589
.pipe(Effect.map((interrupted) => ({ interrupted }))),
588590
wait: (input) => sessions.wait(input.sessionID),
589591
context: (input) => sessions.context(input.sessionID),
592+
form: {
593+
list: (input) => forms.list({ sessionID: input.sessionID }),
594+
create: (input) =>
595+
forms.create({
596+
id: input.id,
597+
sessionID: input.sessionID,
598+
title: input.title,
599+
metadata: input.metadata,
600+
fields: input.fields,
601+
}),
602+
get: (input) =>
603+
forms.get(input.formID).pipe(
604+
Effect.flatMap((info) => {
605+
if (info.sessionID !== input.sessionID) return Effect.fail(new Form.NotFoundError({ id: input.formID }))
606+
return Effect.map(forms.state(input.formID), (state) => ({ ...info, state }))
607+
}),
608+
),
609+
reply: (input) =>
610+
forms.get(input.formID).pipe(
611+
Effect.flatMap((info) =>
612+
info.sessionID !== input.sessionID
613+
? Effect.fail(new Form.NotFoundError({ id: input.formID }))
614+
: forms.reply({ id: input.formID, answer: input.answer }),
615+
),
616+
),
617+
cancel: (input) =>
618+
forms.get(input.formID).pipe(
619+
Effect.flatMap((info) =>
620+
info.sessionID !== input.sessionID
621+
? Effect.fail(new Form.NotFoundError({ id: input.formID }))
622+
: forms.cancel(input.formID),
623+
),
624+
),
625+
},
590626
},
591627
}
592628
return context
@@ -600,6 +636,7 @@ export const requirements = LayerNode.group([
600636
Model.node,
601637
Command.node,
602638
Bus.node,
639+
Form.node,
603640
Integration.node,
604641
KV.node,
605642
Mcp.node,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ export function host(overrides: Overrides = {}): Plugin.Context {
185185
interrupt: overrides.session?.interrupt ?? (() => Effect.die("unused session.interrupt")),
186186
wait: overrides.session?.wait ?? (() => Effect.die("unused session.wait")),
187187
context: overrides.session?.context ?? (() => Effect.die("unused session.context")),
188+
form: overrides.session?.form ?? {
189+
list: () => Effect.die("unused session.form.list"),
190+
create: () => Effect.die("unused session.form.create"),
191+
get: () => Effect.die("unused session.form.get"),
192+
reply: () => Effect.die("unused session.form.reply"),
193+
cancel: () => Effect.die("unused session.form.cancel"),
194+
},
188195
},
189196
}
190197
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export type SessionDomain = Pick<
169169
| "move"
170170
| "wait"
171171
| "context"
172+
| "form"
172173
> & {
173174
readonly hook: ModelHooks<SessionHooks>
174175
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,13 @@ export function fromPromise(plugin: Plugin) {
609609
move: adaptApiMethod(SessionEndpoints["session.move"], host.session.move),
610610
wait: adaptApiMethod(SessionEndpoints["session.wait"], host.session.wait),
611611
context: adaptApiMethod(SessionEndpoints["session.context"], host.session.context),
612+
form: {
613+
list: adaptApiMethod(SessionEndpoints["session.form.list"], host.session.form.list),
614+
create: adaptApiMethod(SessionEndpoints["session.form.create"], host.session.form.create),
615+
get: adaptApiMethod(SessionEndpoints["session.form.get"], host.session.form.get),
616+
reply: adaptApiMethod(SessionEndpoints["session.form.reply"], host.session.form.reply),
617+
cancel: adaptApiMethod(SessionEndpoints["session.form.cancel"], host.session.form.cancel),
618+
},
612619
},
613620
shell: {
614621
hook: (name, callback) =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export type SessionDomain = Pick<
169169
| "move"
170170
| "wait"
171171
| "context"
172+
| "form"
172173
> & {
173174
readonly hook: ModelHooks<SessionHooks>
174175
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ Inspect or resolve pending permission requests.
769769
```ts
770770
const pending = await ctx.permission.list({ sessionID })
771771
const request = await ctx.permission.get({ sessionID, requestID })
772-
await ctx.permission.reply({ sessionID, requestID, reply: "once" })
772+
await ctx.permission.reply({ sessionID, requestID, decision: "once" })
773773
```
774774

775775
Replace the session-scoped permission rules. They are evaluated after the agent's rules, and the
@@ -816,7 +816,7 @@ const command = await ctx.session.command({ sessionID, command: "review", argume
816816
const synthetic = await ctx.session.synthetic({ sessionID, text: "Deployment completed" })
817817
```
818818

819-
Rename, interrupt, or wait for a session.
819+
Update, interrupt, or wait for a session.
820820

821821
```ts
822822
await ctx.session.update({ sessionID, title: "Review" })
@@ -837,6 +837,14 @@ the next step boundary by default. Pass `delivery: "queue"` to preserve queue or
837837
const compaction = await ctx.session.compact({ sessionID })
838838
```
839839

840+
Answer `question` tool forms from an external system. A form appears when the agent asks a
841+
question; replying settles it and unblocks the turn.
842+
843+
```ts
844+
const pending = await ctx.session.form.list({ sessionID })
845+
await ctx.session.form.reply({ sessionID, formID: pending[0].id, answer: { choice: "yes" } })
846+
```
847+
840848
#### Reference
841849

842850
Schemas: [`Session.Info`](/api#schema-Session.Info), [`Model.Ref`](/api#schema-Model.Ref),
@@ -858,7 +866,15 @@ interface SessionContext {
858866
compact(input: SessionCompactInput, requestOptions?: RequestOptions): Promise<SessionInboxCompaction>
859867
synthetic(input: SessionSyntheticInput, requestOptions?: RequestOptions): Promise<SessionInboxSynthetic>
860868
interrupt(input: SessionInterruptInput, requestOptions?: RequestOptions): Promise<void>
869+
update(input: SessionUpdateInput, requestOptions?: RequestOptions): Promise<void>
861870
wait(input: SessionWaitInput, requestOptions?: RequestOptions): Promise<void>
871+
form: {
872+
list(input: SessionFormListInput, requestOptions?: RequestOptions): Promise<readonly FormInfo[]>
873+
create(input: SessionFormCreateInput, requestOptions?: RequestOptions): Promise<FormInfo>
874+
get(input: SessionFormGetInput, requestOptions?: RequestOptions): Promise<FormDetail>
875+
reply(input: SessionFormReplyInput, requestOptions?: RequestOptions): Promise<void>
876+
cancel(input: SessionFormCancelInput, requestOptions?: RequestOptions): Promise<void>
877+
}
862878
}
863879
```
864880

0 commit comments

Comments
 (0)