Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions packages/pi/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,11 @@ export function createPiActions(options: PiSessionOptions): PiActions {
await reloadCredentials(handle);
const { session } = handle;
requireCredential(session);
const abort = () => void session.abort();
c.abortSignal.addEventListener("abort", abort, { once: true });
try {
await traceRun(session, c.actorId, () =>
session.prompt(text, promptOptions),
);
} finally {
c.abortSignal.removeEventListener("abort", abort);
}
// The run outlives this action: a timeout, a caller that leaves, or
// a stopping actor never aborts it. Only the abort action does.
await traceRun(session, c.actorId, () =>
session.prompt(text, promptOptions),
);
}),
steer: (c, ...args) => mutate(c, ({ session }) => session.steer(...args)),
followUp: (c, ...args) =>
Expand Down
31 changes: 31 additions & 0 deletions packages/pi/src/actor-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** `Omit` that keeps each member of a union, so `state` and `createState` stay alternatives. */
export type DistributiveOmit<T, K extends PropertyKey> = T extends unknown
? Omit<T, K>
: never;

/** The app's actions as clients see them: the same arguments, without `c.pi`. */
export type ClientActions<T, TContext> = string extends keyof T
? Record<never, never>
: {
[K in keyof T]: T[K] extends (
c: any,
...args: infer TArgs
) => infer TResult
? (c: TContext, ...args: TArgs) => TResult
: ClientActions<T[K], TContext>;
};

/** Wraps each of the app's actions, including nested ones, so it runs inside `run`. */
export function wrapActions(
actions: Record<string, unknown>,
run: (c: any, action: () => unknown) => Promise<unknown>,
): Record<string, unknown> {
return Object.fromEntries(
Object.entries(actions).map(([name, action]) => [
name,
typeof action === "function"
? (c: unknown, ...args: unknown[]) => run(c, () => action(c, ...args))
: wrapActions(action as Record<string, unknown>, run),
]),
);
}
Loading
Loading