@fungi.computer/shiitake-extensions 0.6.15 → 0.7.0

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.
@@ -1,9 +1,14 @@
1
+ import { CapabilityFailed, Mycelium, } from "@fungi.computer/mycelium";
1
2
  import { defineCollection, defineExtension, ExtensionCommandError, } from "@fungi.computer/shiitake/extensions";
3
+ import * as Cause from "effect/Cause";
2
4
  import * as Clock from "effect/Clock";
3
5
  import * as Effect from "effect/Effect";
6
+ import * as Exit from "effect/Exit";
4
7
  import * as Result from "effect/Result";
5
8
  import * as Option from "effect/Option";
6
9
  import { afterAdmitted, afterRefused, cancelled, occurrenceKey, paused, resumed, settled, } from "@fungi.computer/almanac";
10
+ import { z } from "zod";
11
+ import { freshRequestId } from "../internal/catalog.js";
7
12
  import { nextOccurrence } from "../internal/cron.js";
8
13
  import { almanacContract, Schedule } from "./contract.js";
9
14
  /** Hashed policy: every constant Almanac reads comes from here. */
@@ -12,6 +17,8 @@ const config = {
12
17
  retryBaseMs: 30_000,
13
18
  /** Longest retry delay; one poisoned schedule never blocks the others. */
14
19
  retryMaxMs: 3_600_000,
20
+ /** The one idle prompt line; the rules load with forage.describe. */
21
+ idle: "Daily, weekly or one-time work? forage.describe({path:'schedule'}) first.",
15
22
  };
16
23
  const collections = {
17
24
  schedules: defineCollection(Schedule, {
@@ -49,6 +56,50 @@ function create(ctx, schedule) {
49
56
  return schedule;
50
57
  });
51
58
  }
59
+ /** Create a one-shot schedule due at one instant. */
60
+ function createOneShot(ctx, input) {
61
+ return create(ctx, {
62
+ id: input.requestId,
63
+ sessionId: input.sessionId,
64
+ prompt: input.prompt,
65
+ cron: null,
66
+ nextDueAt: input.dueAt,
67
+ state: "active",
68
+ lastCommandId: null,
69
+ lastError: null,
70
+ attempt: 0,
71
+ });
72
+ }
73
+ /** Create a recurring cron schedule; its first occurrence follows now. */
74
+ function createRecurring(ctx, input) {
75
+ return Effect.gen(function* () {
76
+ const cron = { expression: input.expression, timeZone: input.timeZone };
77
+ const now = yield* Clock.currentTimeMillis;
78
+ return yield* create(ctx, {
79
+ id: input.requestId,
80
+ sessionId: input.sessionId,
81
+ prompt: input.prompt,
82
+ cron,
83
+ nextDueAt: yield* nextOccurrence(cron, now),
84
+ state: "active",
85
+ lastCommandId: null,
86
+ lastError: null,
87
+ attempt: 0,
88
+ });
89
+ });
90
+ }
91
+ /** Cancel future occurrences; a settled schedule is returned as it is. */
92
+ function cancelSchedule(ctx, scheduleId) {
93
+ return Effect.gen(function* () {
94
+ const schedule = yield* requireSchedule(ctx, scheduleId);
95
+ if (settled(schedule))
96
+ return schedule;
97
+ const next = cancelled(schedule);
98
+ yield* ctx.state.schedules.put(next);
99
+ yield* ctx.timers.cancel(schedule.id);
100
+ return next;
101
+ });
102
+ }
52
103
  /**
53
104
  * Send the due occurrence through the door. The occurrence request id is
54
105
  * `id:occurrenceAt`, so a retried admission replays instead of duplicating.
@@ -86,47 +137,129 @@ function fire(ctx, id, now) {
86
137
  yield* ctx.timers.set(schedule.id, next.nextDueAt);
87
138
  });
88
139
  }
140
+ /** What the model reads from `schedule.describe`; also the module's description. */
141
+ const moduleDescription = "Durable schedules that send you a prompt in this Session when due: schedule.create({prompt,when}), schedule.list({}), schedule.cancel({scheduleId}). " +
142
+ "when is {kind:'once',at:ISO-8601 instant with offset} or {kind:'cron',expression,timeZone?}; a cron expression has five fields, so every morning at 8 is '0 8 * * *' and Mondays at 9:30 is '30 9 * * 1', evaluated in timeZone (IANA, default UTC; use the person's zone when you know it). " +
143
+ "Use a schedule for a daily, weekly or other cadence that must outlast a loop's 7 days, or for one future time. Schedules persist until cancelled; tell the person what you scheduled and its id so they can cancel it.";
144
+ /** Run one tool transaction and keep the owner's own diagnostic. */
145
+ function toolCall(ctx, operation, body) {
146
+ return Effect.runPromiseExit(ctx.transact(body)).then((exit) => {
147
+ if (Exit.isSuccess(exit))
148
+ return exit.value;
149
+ const failure = Cause.squash(exit.cause);
150
+ return Promise.reject(new CapabilityFailed({
151
+ namespace: "schedule",
152
+ operation,
153
+ cause: {
154
+ kind: "OperationFailed",
155
+ message: failure instanceof Error ? failure.message : "Schedule tool failed",
156
+ },
157
+ }));
158
+ });
159
+ }
160
+ // Mycelium's maintained operation interface takes Zod for model-tool I/O.
161
+ const toolSchedule = z.strictObject({
162
+ id: z.string(),
163
+ prompt: z.string(),
164
+ cron: z
165
+ .strictObject({ expression: z.string(), timeZone: z.string() })
166
+ .nullable(),
167
+ nextDueAt: z.number(),
168
+ state: z.enum(["active", "paused", "cancelled", "finished"]),
169
+ lastError: z.string().nullable(),
170
+ });
171
+ function projected(schedule) {
172
+ return {
173
+ id: schedule.id,
174
+ prompt: schedule.prompt,
175
+ cron: schedule.cron,
176
+ nextDueAt: schedule.nextDueAt,
177
+ state: schedule.state,
178
+ lastError: schedule.lastError,
179
+ };
180
+ }
181
+ /** The model-facing create/list/cancel, over the same commands the Hub and Local read. */
182
+ function scheduleModule(ctx, sessionId) {
183
+ return Mycelium.module({
184
+ id: "botanical.almanac",
185
+ name: "schedule",
186
+ description: moduleDescription,
187
+ operations: {
188
+ create: Mycelium.operation({
189
+ replay: "unsafe",
190
+ description: "Schedule a prompt for this Session, once or on a cron cadence. Returns the schedule with its id and next due time.",
191
+ input: z.strictObject({
192
+ prompt: z.string().trim().min(1).max(4_000),
193
+ when: z.union([
194
+ z.strictObject({
195
+ kind: z.literal("once"),
196
+ at: z.iso.datetime({ offset: true }),
197
+ }),
198
+ z.strictObject({
199
+ kind: z.literal("cron"),
200
+ expression: z.string().trim().min(1).max(256),
201
+ timeZone: z.string().trim().min(1).max(128).default("UTC"),
202
+ }),
203
+ ]),
204
+ }),
205
+ output: toolSchedule,
206
+ execute: ({ prompt, when }) => toolCall(ctx, "create", Effect.gen(function* () {
207
+ const requestId = yield* freshRequestId;
208
+ if (when.kind === "cron")
209
+ return projected(yield* createRecurring(ctx, {
210
+ requestId,
211
+ sessionId,
212
+ prompt,
213
+ expression: when.expression,
214
+ timeZone: when.timeZone,
215
+ }));
216
+ const dueAt = Date.parse(when.at);
217
+ const now = yield* Clock.currentTimeMillis;
218
+ if (dueAt <= now)
219
+ return yield* Effect.fail(rejected("invalid", "A one-shot schedule must be in the future"));
220
+ return projected(yield* createOneShot(ctx, {
221
+ requestId,
222
+ sessionId,
223
+ prompt,
224
+ dueAt,
225
+ }));
226
+ })),
227
+ }),
228
+ list: Mycelium.operation({
229
+ replay: "safe",
230
+ description: "List this Agent's schedules, with ids, cadence, state and next due time.",
231
+ input: z.strictObject({}),
232
+ output: z.array(toolSchedule),
233
+ execute: () => toolCall(ctx, "list", ctx.state.schedules
234
+ .page({ limit: 64 })
235
+ .pipe(Effect.map((rows) => rows.map(projected)))),
236
+ }),
237
+ cancel: Mycelium.operation({
238
+ replay: "unsafe",
239
+ description: "Cancel a schedule's future occurrences by id; a finished or cancelled one is returned unchanged.",
240
+ input: z.strictObject({ scheduleId: z.string() }),
241
+ output: toolSchedule,
242
+ execute: ({ scheduleId }) => toolCall(ctx, "cancel", cancelSchedule(ctx, scheduleId).pipe(Effect.map(projected))),
243
+ }),
244
+ },
245
+ });
246
+ }
89
247
  /** Botanical's Almanac on Shiitake timers and the one admission door. */
90
248
  export const almanacExtension = defineExtension({
91
249
  id: almanacContract.id,
92
- version: "1.1.0",
250
+ version: "1.2.0",
93
251
  })
94
252
  .config(config)
95
253
  .state({ version: 1, migrations: {}, collections })
96
254
  .define({
255
+ prompt: () => Effect.succeed([{ name: "schedule", text: config.idle }]),
256
+ tools: (ctx, { sessionId }) => Effect.succeed(Option.some(scheduleModule(ctx, sessionId))),
97
257
  hooks: { onTimer: (ctx, timer) => fire(ctx, timer.key, timer.firedAt) },
98
258
  commands: {
99
259
  specs: almanacContract.commands,
100
260
  handlers: (ctx) => ({
101
- createOneShot: ({ input }) => create(ctx, {
102
- id: input.requestId,
103
- sessionId: input.sessionId,
104
- prompt: input.prompt,
105
- cron: null,
106
- nextDueAt: input.dueAt,
107
- state: "active",
108
- lastCommandId: null,
109
- lastError: null,
110
- attempt: 0,
111
- }),
112
- createRecurring: ({ input }) => Effect.gen(function* () {
113
- const cron = {
114
- expression: input.expression,
115
- timeZone: input.timeZone,
116
- };
117
- const now = yield* Clock.currentTimeMillis;
118
- return yield* create(ctx, {
119
- id: input.requestId,
120
- sessionId: input.sessionId,
121
- prompt: input.prompt,
122
- cron,
123
- nextDueAt: yield* nextOccurrence(cron, now),
124
- state: "active",
125
- lastCommandId: null,
126
- lastError: null,
127
- attempt: 0,
128
- });
129
- }),
261
+ createOneShot: ({ input }) => createOneShot(ctx, input),
262
+ createRecurring: ({ input }) => createRecurring(ctx, input),
130
263
  read: ({ input }) => ctx.state.schedules
131
264
  .get(input.scheduleId)
132
265
  .pipe(Effect.map(Option.getOrNull)),
@@ -157,15 +290,7 @@ export const almanacExtension = defineExtension({
157
290
  yield* ctx.timers.set(schedule.id, back.success.nextDueAt);
158
291
  return back.success;
159
292
  }),
160
- cancel: ({ input }) => Effect.gen(function* () {
161
- const schedule = yield* requireSchedule(ctx, input.scheduleId);
162
- if (settled(schedule))
163
- return schedule;
164
- const next = cancelled(schedule);
165
- yield* ctx.state.schedules.put(next);
166
- yield* ctx.timers.cancel(schedule.id);
167
- return next;
168
- }),
293
+ cancel: ({ input }) => cancelSchedule(ctx, input.scheduleId),
169
294
  }),
170
295
  },
171
296
  });
package/dist/loop/host.js CHANGED
@@ -13,7 +13,7 @@ import { nextOccurrence } from "../internal/cron.js";
13
13
  import { Loop, LoopCommandInput, loopContract } from "./contract.js";
14
14
  import { loopMenuPrompt, stopConfirmation } from "./prompts.js";
15
15
  /** The rules the model follows; the describe page and a live loop's prompt share them. */
16
- const rules = "You may start loops yourself; one live loop per Session. Failed loop ticks retry with backoff; loop.get shows consecutiveFailures, lastError, and nextDueAt. schedule_next is allowed only in the active self-paced loop's own run, clamped to 60–3600 seconds. Omitting it ends that loop. stop works in any Session run. Human pause/resume/stop and Esc remain user controls. A fixed every-N cadence ends after 7 days; tell the person when you start one, and start a new loop to continue.";
16
+ const rules = "You may start loops yourself; one live loop per Session. Failed loop ticks retry with backoff; loop.get shows consecutiveFailures, lastError, and nextDueAt. schedule_next is allowed only in the active self-paced loop's own run, clamped to 60–3600 seconds. Omitting it ends that loop. stop works in any Session run. Human pause/resume/stop and Esc remain user controls. A fixed every-N cadence ends after 7 days; for a daily or weekly cadence, or anything longer, use schedule.create (forage.describe({path:'schedule'})) instead, and otherwise tell the person when you start one.";
17
17
  /** Hashed prose and policy: every constant loop reads comes from here. */
18
18
  const config = {
19
19
  minDelaySeconds: 60,
@@ -380,7 +380,7 @@ function loopModule(ctx, sessionId, workId) {
380
380
  }),
381
381
  start: Mycelium.operation({
382
382
  replay: "unsafe",
383
- description: "Start a Session loop, including autonomously. One live loop per Session; the first prompt runs after the current run. A fixed every-N cadence ends after 7 days.",
383
+ description: "Start a Session loop, including autonomously. One live loop per Session; the first prompt runs after the current run. A fixed every-N cadence ends after 7 days; use schedule.create for longer ones.",
384
384
  input: z.strictObject({
385
385
  prompt: z
386
386
  .string()
package/dist/roster.js CHANGED
@@ -67,7 +67,14 @@ export const botanicalAgentExtensions = Object.freeze({
67
67
  },
68
68
  {
69
69
  id: almanacContract.id,
70
- capabilities: ["state", "send", "timers", "commands"],
70
+ capabilities: [
71
+ "state",
72
+ "send",
73
+ "timers",
74
+ "commands",
75
+ "run_tools",
76
+ "prompt",
77
+ ],
71
78
  },
72
79
  {
73
80
  id: planContract.id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/shiitake-extensions",
3
- "version": "0.6.15",
3
+ "version": "0.7.0",
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",
@@ -116,9 +116,9 @@
116
116
  "dependencies": {
117
117
  "@fungi.computer/almanac": "0.2.1",
118
118
  "@fungi.computer/cairn": "0.1.0",
119
- "@fungi.computer/knapsack": "0.4.9",
120
- "@fungi.computer/mycelium": "0.3.8",
121
- "@fungi.computer/shiitake": "0.14.1",
119
+ "@fungi.computer/knapsack": "0.4.10",
120
+ "@fungi.computer/mycelium": "0.3.9",
121
+ "@fungi.computer/shiitake": "0.14.2",
122
122
  "@fungi.computer/watchdog": "0.1.3",
123
123
  "@fungi.computer/whistle": "0.3.0",
124
124
  "effect": "4.0.2",