@fungi.computer/shiitake-extensions 0.13.13 → 0.13.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ import * as Schema from "effect/Schema";
8
8
  import { createHash } from "node:crypto";
9
9
  import { z } from "zod";
10
10
  import { catalog, done } from "../internal/catalog.js";
11
+ import { toolCall } from "./tool-call.js";
11
12
  import { questionContract, OpenAsk, Questions, Answers, HumanResult, AgentAnswerResult, AnsweredBy, EscalateResult, DeclineInput, ClosedStatus, AnswerFlowInput, } from "./contract.js";
12
13
  import { advanceAnswerFlow, closedQuestionNotice, } from "./answer-flow.js";
13
14
  const config = {
@@ -724,7 +725,7 @@ function pause(ms, signal) {
724
725
  }
725
726
  async function waitForResponse(ctx, askId, until, invocation) {
726
727
  for (;;) {
727
- const next = await Effect.runPromise(ctx.transact(Effect.gen(function* () {
728
+ const next = await toolCall("ask", ctx.transact(Effect.gen(function* () {
728
729
  const found = yield* ctx.state.asks.get(askId);
729
730
  if (invocation.signal.aborted) {
730
731
  if (Option.isSome(found))
@@ -732,11 +733,24 @@ async function waitForResponse(ctx, askId, until, invocation) {
732
733
  return { status: "aborted" };
733
734
  }
734
735
  if (Option.isNone(found)) {
736
+ // A question closed or removed while its call waited is an ordinary
737
+ // end (withdrawn, answered elsewhere, expired); a thrown defect
738
+ // would stop the whole extension for the Session.
735
739
  const closed = yield* ctx.state.closed.get(askId);
736
- if (Option.isSome(closed) &&
737
- closed.value.outcome.status === "withdrawn")
738
- return closed.value.outcome;
739
- throw new Error("Waiting question disappeared");
740
+ if (Option.isNone(closed) ||
741
+ closed.value.outcome.status === "cancelled")
742
+ return { status: "withdrawn" };
743
+ // A follow-up already carries the answer to the Agent; returning
744
+ // it here too would deliver it twice.
745
+ if (closed.value.delivery.via === "follow_up")
746
+ return { status: "parked", askId };
747
+ // Same bookkeeping as a replayed in-turn answer, so settlement
748
+ // can acknowledge this tool result.
749
+ if (closed.value.delivery.via === "in_turn" &&
750
+ closed.value.delivery.status === "pending" &&
751
+ invocation.toolCallId !== undefined)
752
+ yield* recordDelivery(ctx, closed.value, invocation.toolCallId);
753
+ return closed.value.outcome;
740
754
  }
741
755
  const row = found.value;
742
756
  if (row.pending !== null) {
@@ -1088,7 +1102,7 @@ export function questionExtensionFor(routesToSender) {
1088
1102
  description: "Withdraw a question that no longer needs a human answer. Repeating withdrawal preserves its closed outcome.",
1089
1103
  input: z.strictObject({ askId: z.string().min(1).max(256) }),
1090
1104
  output: WithdrawResult,
1091
- execute: async ({ askId }) => WithdrawResult.parse(await Effect.runPromise(ctx.transact(withdraw(ctx, sessionId, askId)))),
1105
+ execute: async ({ askId }) => WithdrawResult.parse(await toolCall("withdraw", ctx.transact(withdraw(ctx, sessionId, askId)))),
1092
1106
  }),
1093
1107
  ask: Mycelium.operation({
1094
1108
  replay: "unsafe",
@@ -1096,7 +1110,7 @@ export function questionExtensionFor(routesToSender) {
1096
1110
  input,
1097
1111
  output: ToolResult,
1098
1112
  execute: async (value, invocation) => {
1099
- const created = await Effect.runPromise(ctx.transact({
1113
+ const created = await toolCall("ask", ctx.transact({
1100
1114
  prepare: Effect.gen(function* () {
1101
1115
  const waitingOn = ctx.config.routesToSender
1102
1116
  ? yield* ctx.workSender({ sessionId, workId })
@@ -0,0 +1,6 @@
1
+ import * as Effect from "effect/Effect";
2
+ /**
3
+ * Run one question transaction for a tool call. A stopped extension reaches
4
+ * the Agent with its own reason, never as a bare "operation failed".
5
+ */
6
+ export declare function toolCall<A>(operation: string, run: Effect.Effect<A, unknown>): Promise<A>;
@@ -0,0 +1,25 @@
1
+ import { CapabilityFailed } from "@fungi.computer/mycelium";
2
+ import { ExtensionStop } from "@fungi.computer/shiitake/extensions";
3
+ import * as Cause from "effect/Cause";
4
+ import * as Effect from "effect/Effect";
5
+ import * as Exit from "effect/Exit";
6
+ /**
7
+ * Run one question transaction for a tool call. A stopped extension reaches
8
+ * the Agent with its own reason, never as a bare "operation failed".
9
+ */
10
+ export async function toolCall(operation, run) {
11
+ const exit = await Effect.runPromiseExit(run);
12
+ if (Exit.isSuccess(exit))
13
+ return exit.value;
14
+ const failure = Cause.squash(exit.cause);
15
+ if (failure instanceof ExtensionStop)
16
+ throw new CapabilityFailed({
17
+ namespace: "question",
18
+ operation,
19
+ cause: {
20
+ kind: "Unavailable",
21
+ message: `The question tool stopped working in this Session: ${failure.reason}`,
22
+ },
23
+ });
24
+ throw failure;
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/shiitake-extensions",
3
- "version": "0.13.13",
3
+ "version": "0.13.15",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "private": false,
6
6
  "description": "Botanical's product Shiitake extensions: goal, loop and Almanac on the generic extension surface",
@@ -124,9 +124,9 @@
124
124
  "dependencies": {
125
125
  "@fungi.computer/almanac": "0.2.1",
126
126
  "@fungi.computer/cairn": "0.1.1",
127
- "@fungi.computer/knapsack": "0.4.33",
128
- "@fungi.computer/mycelium": "0.5.2",
129
- "@fungi.computer/shiitake": "0.23.3",
127
+ "@fungi.computer/knapsack": "0.4.34",
128
+ "@fungi.computer/mycelium": "0.5.3",
129
+ "@fungi.computer/shiitake": "0.23.5",
130
130
  "@fungi.computer/watchdog": "0.1.7",
131
131
  "@fungi.computer/whistle": "0.3.0",
132
132
  "effect": "4.0.2",