@chrischall/hellofresh-mcp 0.1.0 → 0.2.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.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +19 -9
- package/dist/bundle.js +1754 -713
- package/dist/client.js +123 -1
- package/dist/cooking.js +123 -0
- package/dist/index.js +5 -4
- package/dist/normalize.js +32 -0
- package/dist/planner.js +71 -16
- package/dist/session.js +47 -3
- package/dist/tools/cooking.js +150 -0
- package/dist/tools/favorites.js +164 -0
- package/dist/tools/index.js +7 -1
- package/dist/tools/planner.js +276 -83
- package/dist/tools/promo.js +108 -0
- package/dist/tools/reads.js +71 -40
- package/dist/tools/shared.js +210 -24
- package/dist/tools/writes/index.js +9 -0
- package/dist/tools/writes/plan.js +177 -0
- package/dist/tools/writes/shared.js +137 -0
- package/dist/tools/{writes.js → writes/week.js} +8 -290
- package/dist/transport.js +13 -1
- package/dist/version.js +1 -1
- package/manifest.json +43 -4
- package/mint.yaml +6 -0
- package/package.json +1 -1
- package/server.json +8 -2
- package/skills/hellofresh/SKILL.md +49 -12
- package/skills/hellofresh-fpx/SKILL.md +13 -5
- package/skills/hellofresh-fpx/references/requests.md +30 -10
package/dist/tools/planner.js
CHANGED
|
@@ -6,13 +6,15 @@
|
|
|
6
6
|
// pure and lives in src/planner.ts; profiles load from local config
|
|
7
7
|
// (src/profiles.ts) on first use.
|
|
8
8
|
import { z } from 'zod';
|
|
9
|
-
import { minifiedResult, toolAnnotations } from '@chrischall/mcp-utils';
|
|
9
|
+
import { McpToolError, minifiedResult, toolAnnotations } from '@chrischall/mcp-utils';
|
|
10
10
|
import { projectDelivery } from '../normalize.js';
|
|
11
|
-
import { suggestMeals, summaryText } from '../planner.js';
|
|
11
|
+
import { LOW_RATING, suggestMeals, summaryText } from '../planner.js';
|
|
12
12
|
import { BUILTIN_PROFILES, DEFAULT_PROFILES_FILE, PROFILES_ENV, PROFILES_FILE_ENV, createProfileStore, profileLabel, profileSku, resolveProfile, } from '../profiles.js';
|
|
13
13
|
import { shiftWeek } from '../weeks.js';
|
|
14
14
|
import { foodMeta } from './hints.js';
|
|
15
|
-
import { boxNumbering, boxNumbers,
|
|
15
|
+
import { boxNumbering, boxNumbers, calculatorGate, compactSelection, dateOf, estimateFor, estimateInDollars, hellofreshPrice, isUnresizableFirstBox, loadAccount, nextBoxNumberParam, premiumCentsOf, readMenu, MenuNotPublishedError, readPersonal, readPicks, weekParam, } from './shared.js';
|
|
16
|
+
/** The most weeks hf_plan_weeks plans in one call. */
|
|
17
|
+
const MAX_PLANNED_WEEKS = 6;
|
|
16
18
|
const readOnly = (title) => toolAnnotations({ title, readOnly: true, idempotent: true, openWorld: true });
|
|
17
19
|
// ---- parameters -----------------------------------------------------------------------
|
|
18
20
|
const builtinNames = BUILTIN_PROFILES.map((p) => `"${p.name}"`).join(', ');
|
|
@@ -52,23 +54,36 @@ const suggestInput = {
|
|
|
52
54
|
.optional()
|
|
53
55
|
.describe('Highest premium upcharge per serving, in US cents (default: the profile’s premiumCapCents; 0 = no premiums).'),
|
|
54
56
|
};
|
|
55
|
-
async function plan(client, account, delivery, profile, args
|
|
57
|
+
async function plan(client, account, delivery, profile, args,
|
|
58
|
+
/** Recipe names to avoid on top of recent picks (hf_plan_weeks: those planned for earlier weeks). */
|
|
59
|
+
alsoAvoid = []) {
|
|
56
60
|
const back = args.avoid_recent_weeks ?? profile.avoidRepeatWeeks;
|
|
57
61
|
const weeks = Array.from({ length: back }, (_, i) => shiftWeek(args.week, i - back));
|
|
58
|
-
const [menu, picks] = await Promise.all([
|
|
62
|
+
const [menu, picks, mine] = await Promise.all([
|
|
59
63
|
readMenu(client, account, delivery, args.week),
|
|
60
64
|
back > 0 ? readPicks(client, account, weeks) : undefined,
|
|
65
|
+
readPersonal(client),
|
|
61
66
|
]);
|
|
62
67
|
const suggestion = suggestMeals(menu, {
|
|
63
68
|
profile,
|
|
64
69
|
people: args.people,
|
|
65
70
|
avoidIndexes: args.avoid_indexes,
|
|
66
|
-
avoidNames: picks?.recipeNames,
|
|
71
|
+
avoidNames: [...(picks?.recipeNames ?? []), ...alsoAvoid],
|
|
67
72
|
maxPremiumPerServingCents: args.max_premium_per_serving_cents,
|
|
73
|
+
favoriteRecipeIds: mine.favoriteRecipeIds,
|
|
74
|
+
ratings: mine.ratings,
|
|
68
75
|
});
|
|
76
|
+
const lowRated = mine.ratings.filter((r) => r.rating <= LOW_RATING).length;
|
|
69
77
|
return {
|
|
70
78
|
menu,
|
|
71
79
|
suggestion,
|
|
80
|
+
personal: {
|
|
81
|
+
...(mine.favoriteRecipeIds ? { favorites: mine.favoriteRecipeIds.length } : {}),
|
|
82
|
+
ratings: mine.ratings.length,
|
|
83
|
+
...(mine.ratingsSkipped > 0 ? { ratingsSkipped: mine.ratingsSkipped } : {}),
|
|
84
|
+
...(lowRated > 0 ? { lowRated } : {}),
|
|
85
|
+
...(mine.unreadable.length > 0 ? { unreadable: mine.unreadable } : {}),
|
|
86
|
+
},
|
|
72
87
|
...(picks
|
|
73
88
|
? {
|
|
74
89
|
recent: {
|
|
@@ -87,6 +102,7 @@ function compactCandidate(c) {
|
|
|
87
102
|
category: c.category,
|
|
88
103
|
protein: c.protein,
|
|
89
104
|
...(c.baseIndex !== undefined ? { baseIndex: c.baseIndex } : {}),
|
|
105
|
+
...(c.favorite ? { favorite: true } : {}),
|
|
90
106
|
kidFriendly: c.kidFriendly,
|
|
91
107
|
totalMinutes: c.totalMinutes,
|
|
92
108
|
...(c.premiumPerServingCents > 0 ? { premiumPerServingCents: c.premiumPerServingCents, premiumBoxCents: c.premiumBoxCents } : {}),
|
|
@@ -118,6 +134,22 @@ function priceFor(sku, delivery, upcoming, account) {
|
|
|
118
134
|
return { priceCents: product.unitPrice, ...fees, priceFrom: 'the plan price' };
|
|
119
135
|
return undefined;
|
|
120
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* The local estimate for `sku` (the fallback when HelloFresh's price cannot be
|
|
139
|
+
* read): the upcoming list prices the size and numbers the box, as if the week
|
|
140
|
+
* runs (an unskip makes it count).
|
|
141
|
+
*/
|
|
142
|
+
async function localEstimate(client, account, delivery, sku, picks, args) {
|
|
143
|
+
const upcoming = ((await client.listDeliveries()).items ?? []).map(projectDelivery);
|
|
144
|
+
const numbering = boxNumbering(account.subscription, args.next_box_number);
|
|
145
|
+
const boxNumber = args.box_number ??
|
|
146
|
+
boxNumbers(upcoming.map((d) => (d.week === args.week ? { ...d, status: 'running' } : d)), numbering.next).get(args.week);
|
|
147
|
+
const price = priceFor(sku, delivery, upcoming, account);
|
|
148
|
+
const estimate = price
|
|
149
|
+
? estimateFor({ ...delivery, ...price }, picks.map((p) => ({ premiumBoxCents: p.premiumBoxCents, quantity: 1 })), account, boxNumber)
|
|
150
|
+
: undefined;
|
|
151
|
+
return { estimate, priceFrom: price?.priceFrom, numberingNote: numbering.note, boxNumber };
|
|
152
|
+
}
|
|
121
153
|
/** Why the week can no longer be changed, if it cannot. */
|
|
122
154
|
function lockedReason(delivery, week, now) {
|
|
123
155
|
if (delivery.status !== 'running' && delivery.status !== 'skipped')
|
|
@@ -127,6 +159,134 @@ function lockedReason(delivery, week, now) {
|
|
|
127
159
|
}
|
|
128
160
|
return undefined;
|
|
129
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* One week's plan — hf_weekly_plan's whole answer, shared by hf_plan_weeks.
|
|
164
|
+
* `alsoAvoid` adds recipe names never to suggest (those planned for earlier weeks).
|
|
165
|
+
*/
|
|
166
|
+
async function weeklyPlan(client, account, delivery, profile, args, now, gate, alsoAvoid = []) {
|
|
167
|
+
const { week } = args;
|
|
168
|
+
// The first box cannot be resized (a HelloFresh rule): plan for the box as it is, and say so.
|
|
169
|
+
const wanted = profileSku(profile, args.people ?? profile.people);
|
|
170
|
+
const firstBox = isUnresizableFirstBox(account.subscription, week);
|
|
171
|
+
const firstBoxKnown = !!delivery.sku && !!delivery.servings && !!delivery.meals;
|
|
172
|
+
const firstBoxFixed = firstBox && firstBoxKnown;
|
|
173
|
+
/** The first box with no full size read: it cannot be resized, and its size cannot be compared. */
|
|
174
|
+
const firstBoxUnknown = firstBox && !firstBoxKnown;
|
|
175
|
+
const firstBoxDiffers = firstBoxFixed && delivery.sku !== wanted;
|
|
176
|
+
const planProfile = firstBoxDiffers ? { ...profile, meals: delivery.meals } : profile;
|
|
177
|
+
const planArgs = firstBoxDiffers || firstBoxUnknown ? { ...args, people: delivery.servings } : args;
|
|
178
|
+
const { menu, suggestion, recent, personal } = await plan(client, account, delivery, planProfile, planArgs, alsoAvoid);
|
|
179
|
+
const { picks, people } = suggestion;
|
|
180
|
+
// A first box keeps its size; the menu read above needs `delivery.sku`, so it is set here.
|
|
181
|
+
const sku = firstBoxDiffers || firstBoxUnknown ? delivery.sku : profileSku(profile, people);
|
|
182
|
+
const notes = firstBoxDiffers
|
|
183
|
+
? [
|
|
184
|
+
`${week} is the first box of this subscription, which HelloFresh says cannot be resized: it keeps ` +
|
|
185
|
+
`${delivery.sku} (the ${profile.name} profile would want ${wanted}), so the picks and estimate are for it as it is.`,
|
|
186
|
+
]
|
|
187
|
+
: firstBoxUnknown
|
|
188
|
+
? [
|
|
189
|
+
`${week} is the first box of this subscription, which HelloFresh says cannot be resized, and HelloFresh ` +
|
|
190
|
+
`returned no full box size for it (box ${delivery.sku} for ${delivery.servings} people, no meal count): no size ` +
|
|
191
|
+
`change is proposed (hf_set_week_size would refuse it), and the picks follow the ${profile.name} profile’s meal count for the box’s ${delivery.servings} servings.`,
|
|
192
|
+
]
|
|
193
|
+
: [];
|
|
194
|
+
const sizeChangeNeeded = delivery.sku !== sku;
|
|
195
|
+
const skipped = delivery.status === 'skipped';
|
|
196
|
+
const locked = lockedReason(delivery, week, now);
|
|
197
|
+
// HelloFresh's own price for the box the plan needs, with the picks; else the local estimate.
|
|
198
|
+
const real = await gate(() => hellofreshPrice(client, account, {
|
|
199
|
+
week,
|
|
200
|
+
sku,
|
|
201
|
+
people,
|
|
202
|
+
deliveryOption: delivery.deliveryOption,
|
|
203
|
+
deliveryDate: delivery.deliveryDate,
|
|
204
|
+
recipeIndexes: picks.map((p) => p.index),
|
|
205
|
+
premiumCents: premiumCentsOf(picks),
|
|
206
|
+
}, now));
|
|
207
|
+
let estimate;
|
|
208
|
+
let priceFields;
|
|
209
|
+
if (real.ok) {
|
|
210
|
+
priceFields = { price: real.value };
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
const local = await localEstimate(client, account, delivery, sku, picks, args);
|
|
214
|
+
estimate = local.estimate;
|
|
215
|
+
const why = `HelloFresh’s price calculator could not be used (${real.reason})`;
|
|
216
|
+
priceFields = estimate
|
|
217
|
+
? {
|
|
218
|
+
price: {
|
|
219
|
+
source: 'estimate',
|
|
220
|
+
reason: `${why}, so this is a local estimate.`,
|
|
221
|
+
...estimateInDollars(estimate, false),
|
|
222
|
+
...(estimate.voucher ? { voucher: estimate.voucher.code } : {}),
|
|
223
|
+
priceFrom: local.priceFrom,
|
|
224
|
+
},
|
|
225
|
+
...(args.box_number === undefined
|
|
226
|
+
? {
|
|
227
|
+
boxNumberNote: skipped && local.boxNumber !== undefined
|
|
228
|
+
? `${local.numberingNote} ${week} is skipped, but it was counted as a running box (box ${local.boxNumber}) because the plan unskips it; later weeks shift up by one once it is unskipped.`
|
|
229
|
+
: local.numberingNote,
|
|
230
|
+
}
|
|
231
|
+
: {}),
|
|
232
|
+
}
|
|
233
|
+
: { priceWarning: `${why}, and HelloFresh shows no price for ${sku} on this account, so the box total is not estimated.` };
|
|
234
|
+
}
|
|
235
|
+
const chosen = new Set(picks.map((p) => p.index));
|
|
236
|
+
const alreadyPicked = !sizeChangeNeeded &&
|
|
237
|
+
!skipped &&
|
|
238
|
+
picks.length > 0 &&
|
|
239
|
+
menu.selected.length === picks.length &&
|
|
240
|
+
menu.selected.every((s) => chosen.has(s.index));
|
|
241
|
+
const followUp = locked
|
|
242
|
+
? []
|
|
243
|
+
: [
|
|
244
|
+
...(skipped ? [{ tool: 'hf_unskip_week', arguments: { week }, why: `${week} is skipped.` }] : []),
|
|
245
|
+
...(sizeChangeNeeded
|
|
246
|
+
? [
|
|
247
|
+
{
|
|
248
|
+
tool: 'hf_set_week_size',
|
|
249
|
+
arguments: { week, people, meals: profile.meals },
|
|
250
|
+
why: `${week} is ${delivery.sku}; the ${profile.name} profile needs ${sku}.`,
|
|
251
|
+
},
|
|
252
|
+
]
|
|
253
|
+
: []),
|
|
254
|
+
...(picks.length > 0 && !alreadyPicked
|
|
255
|
+
? [{ tool: 'hf_select_meals', arguments: { week, meals: picks.map((p) => p.index) }, why: 'Pick the suggested meals.' }]
|
|
256
|
+
: []),
|
|
257
|
+
];
|
|
258
|
+
const result = {
|
|
259
|
+
week,
|
|
260
|
+
profile: profile.name,
|
|
261
|
+
servings: people,
|
|
262
|
+
delivery: { deliveryDate: dateOf(delivery.deliveryDate), cutoffDate: delivery.cutoffDate, status: delivery.status },
|
|
263
|
+
box: { current: delivery.sku, required: sku, sizeChangeNeeded },
|
|
264
|
+
currentPicks: menu.selected.map(compactSelection),
|
|
265
|
+
...suggestionFields(suggestion),
|
|
266
|
+
...priceFields,
|
|
267
|
+
summary: summaryText({
|
|
268
|
+
profile,
|
|
269
|
+
people,
|
|
270
|
+
deliveryDate: delivery.deliveryDate,
|
|
271
|
+
picks,
|
|
272
|
+
estimate,
|
|
273
|
+
...(real.ok ? { price: { ...real.value, grandTotal: real.value.grandTotal } } : {}),
|
|
274
|
+
}),
|
|
275
|
+
...(locked ? { locked } : {}),
|
|
276
|
+
...(alreadyPicked ? { note: 'The suggested meals are already picked for this week; nothing to change.' } : {}),
|
|
277
|
+
...(notes.length > 0 ? { notes } : {}),
|
|
278
|
+
followUp,
|
|
279
|
+
personal,
|
|
280
|
+
...(followUp.length > 0
|
|
281
|
+
? {
|
|
282
|
+
followUpNote: 'Run these in order once the user approves the summary. Each is confirm-gated: call it without ' +
|
|
283
|
+
'confirmToken to preview, then again with the token. A size change may reset the picks, so pick last.',
|
|
284
|
+
}
|
|
285
|
+
: {}),
|
|
286
|
+
...(recent ? { recentPicks: recent } : {}),
|
|
287
|
+
};
|
|
288
|
+
return { result, real, estimate, followUp };
|
|
289
|
+
}
|
|
130
290
|
// ---- registration ------------------------------------------------------------------------------
|
|
131
291
|
export function registerPlannerTools(server, deps) {
|
|
132
292
|
const { client } = deps;
|
|
@@ -156,8 +316,9 @@ export function registerPlannerTools(server, deps) {
|
|
|
156
316
|
description: 'Suggest the dinners to pick for a HelloFresh delivery week for a planner profile (hf_list_profiles): ranks ' +
|
|
157
317
|
'every dish and protein swap on the week’s menu in the profile’s allowed categories by its preferences ' +
|
|
158
318
|
'(kid friendly, quick, adventurous) and premium cap, skips recipes picked in recent weeks, prefers varied ' +
|
|
159
|
-
'proteins (e.g. one shrimp + one fish),
|
|
160
|
-
'reasons. Read-only: pick with
|
|
319
|
+
'proteins (e.g. one shrimp + one fish), favours the user’s favourite recipes, never suggests one they rated 2 ' +
|
|
320
|
+
'or lower, and returns the top picks plus 2 alternates per slot, each with its reasons. Read-only: pick with ' +
|
|
321
|
+
'hf_select_meals.',
|
|
161
322
|
annotations: readOnly('HelloFresh: suggest meals'),
|
|
162
323
|
_meta: foodMeta('other', { aliases: ['what should we have', 'hellofresh suggestions', 'seafood dinners', 'pick dinners'] }),
|
|
163
324
|
inputSchema: z.object(suggestInput),
|
|
@@ -165,21 +326,23 @@ export function registerPlannerTools(server, deps) {
|
|
|
165
326
|
const profile = profileFor(args.profile);
|
|
166
327
|
const account = await loadAccount(client);
|
|
167
328
|
const delivery = projectDelivery(await client.getDelivery(account.subscriptionId, args.week));
|
|
168
|
-
const { suggestion, recent } = await plan(client, account, delivery, profile, args);
|
|
329
|
+
const { suggestion, recent, personal } = await plan(client, account, delivery, profile, args);
|
|
169
330
|
return minifiedResult({
|
|
170
331
|
week: args.week,
|
|
171
332
|
profile: profile.name,
|
|
172
333
|
servings: suggestion.people,
|
|
173
334
|
box: { current: delivery.sku, required: profileSku(profile, suggestion.people) },
|
|
174
335
|
...suggestionFields(suggestion),
|
|
336
|
+
personal,
|
|
175
337
|
...(recent ? { recentPicks: recent } : {}),
|
|
176
338
|
next: `hf_weekly_plan {week: "${args.week}", profile: "${profile.name}"} prices it and lists the writes; hf_select_meals picks.`,
|
|
177
339
|
});
|
|
178
340
|
});
|
|
179
341
|
server.registerTool('hf_weekly_plan', {
|
|
180
342
|
description: 'Plan one HelloFresh delivery week for a planner profile (hf_list_profiles — e.g. a 4-person family week) in ' +
|
|
181
|
-
'one read: the box size the week needs versus what it has, suggested picks with alternates, the
|
|
182
|
-
'
|
|
343
|
+
'one read: the box size the week needs versus what it has, suggested picks with alternates, the box price ' +
|
|
344
|
+
'(HelloFresh’s own checkout total for that size plus the picks’ premium upcharge, or a labelled local ' +
|
|
345
|
+
'estimate when that cannot be read), a one-line summary ' +
|
|
183
346
|
'for the user to approve, and the exact follow-up write calls (hf_unskip_week, hf_set_week_size, ' +
|
|
184
347
|
'hf_select_meals) in order. The subscription’s first box cannot be resized, so for it no size change is ' +
|
|
185
348
|
'proposed (notes say so). Read-only: it changes nothing.',
|
|
@@ -193,89 +356,119 @@ export function registerPlannerTools(server, deps) {
|
|
|
193
356
|
.min(1)
|
|
194
357
|
.max(1000)
|
|
195
358
|
.optional()
|
|
196
|
-
.describe('This box’s number, counting every box ever delivered — overrides the number derived from next_box_number.'
|
|
359
|
+
.describe('This box’s number, counting every box ever delivered — overrides the number derived from next_box_number. ' +
|
|
360
|
+
'Only used by the local estimate (when HelloFresh’s price cannot be read).'),
|
|
197
361
|
next_box_number: nextBoxNumberParam,
|
|
198
362
|
}),
|
|
199
363
|
}, async (args) => {
|
|
200
|
-
const { week } = args;
|
|
201
364
|
const profile = profileFor(args.profile);
|
|
202
365
|
const account = await loadAccount(client);
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
const
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
366
|
+
const delivery = projectDelivery(await client.getDelivery(account.subscriptionId, args.week));
|
|
367
|
+
return minifiedResult((await weeklyPlan(client, account, delivery, profile, args, now(), calculatorGate())).result);
|
|
368
|
+
});
|
|
369
|
+
server.registerTool('hf_plan_weeks', {
|
|
370
|
+
description: 'Plan up to 6 HelloFresh delivery weeks in one read, each for the planner profile the caller names ' +
|
|
371
|
+
'(hf_list_profiles): every week gets the same answer as hf_weekly_plan (box size needed, picks with ' +
|
|
372
|
+
'alternates, price, summary, follow-up writes), and no recipe planned for an earlier week is suggested ' +
|
|
373
|
+
'again in a later one (on top of the recent-picks rule). Returns a combined summary (one line per week), ' +
|
|
374
|
+
'the total of HelloFresh’s own box prices (weeks with only a local estimate are named, not added), and the ' +
|
|
375
|
+
'follow-up write calls per week in order (hf_set_week_size where needed, then hf_select_meals). A skipped ' +
|
|
376
|
+
'week is reported as skipped, with no suggestions; a week whose menu HelloFresh has not published yet is reported as menu_not_published. Which profile each week gets (e.g. from a calendar) is ' +
|
|
377
|
+
'the caller’s decision: this tool does not read a calendar. Read-only: it changes nothing.',
|
|
378
|
+
annotations: readOnly('HelloFresh: plan several weeks'),
|
|
379
|
+
_meta: foodMeta('other', { aliases: ['plan next few hellofresh weeks', 'multi-week meal plan', 'plan the month of hellofresh'] }),
|
|
380
|
+
inputSchema: z.object({
|
|
381
|
+
weeks: z
|
|
382
|
+
.array(z.object({ week: weekParam, profile: profileParam }))
|
|
383
|
+
.min(1)
|
|
384
|
+
.max(MAX_PLANNED_WEEKS)
|
|
385
|
+
.describe(`The weeks to plan (1–${MAX_PLANNED_WEEKS}), each with the profile the caller decided applies to it; planned in week order.`),
|
|
386
|
+
}),
|
|
387
|
+
}, async (args) => {
|
|
388
|
+
const ordered = [...args.weeks].sort((a, b) => (a.week < b.week ? -1 : 1));
|
|
389
|
+
const repeated = ordered.find((w, i) => i > 0 && ordered[i - 1].week === w.week);
|
|
390
|
+
if (repeated)
|
|
391
|
+
throw new McpToolError(`${repeated.week} is listed more than once: name each week once.`);
|
|
392
|
+
// Every profile resolves before any HelloFresh call.
|
|
393
|
+
const requested = ordered.map((w) => ({ ...w, resolved: profileFor(w.profile) }));
|
|
394
|
+
const account = await loadAccount(client);
|
|
395
|
+
const gate = calculatorGate(); // one per call: a down bridge is tried once, not per week
|
|
396
|
+
const planned = [];
|
|
397
|
+
const weeks = [];
|
|
398
|
+
const lines = [];
|
|
399
|
+
const followUp = [];
|
|
400
|
+
const priced = [];
|
|
401
|
+
const notIncluded = [];
|
|
402
|
+
for (const { week, profile, resolved } of requested) {
|
|
403
|
+
const delivery = projectDelivery(await client.getDelivery(account.subscriptionId, week));
|
|
404
|
+
if (delivery.status === 'skipped') {
|
|
405
|
+
weeks.push({
|
|
406
|
+
week,
|
|
407
|
+
profile,
|
|
408
|
+
status: 'skipped',
|
|
409
|
+
delivery: { deliveryDate: dateOf(delivery.deliveryDate), cutoffDate: delivery.cutoffDate, status: delivery.status },
|
|
410
|
+
note: `${week} is skipped, so it is not planned. To plan an unskip, use hf_weekly_plan for it.`,
|
|
411
|
+
});
|
|
412
|
+
lines.push(`${week}: skipped — not planned.`);
|
|
413
|
+
notIncluded.push({ week, reason: 'skipped' });
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
const avoid = [...planned];
|
|
417
|
+
let one;
|
|
418
|
+
try {
|
|
419
|
+
one = await weeklyPlan(client, account, delivery, resolved, { week, profile }, now(), gate, avoid);
|
|
420
|
+
}
|
|
421
|
+
catch (err) {
|
|
422
|
+
if (!(err instanceof MenuNotPublishedError))
|
|
423
|
+
throw err;
|
|
424
|
+
weeks.push({
|
|
425
|
+
week,
|
|
426
|
+
profile: resolved.name,
|
|
427
|
+
status: 'menu_not_published',
|
|
428
|
+
delivery: { deliveryDate: dateOf(delivery.deliveryDate), cutoffDate: delivery.cutoffDate, status: delivery.status },
|
|
429
|
+
note: `${err.message} Plan it once the menu is out.`,
|
|
430
|
+
});
|
|
431
|
+
lines.push(`${week}: menu not published yet — not planned.`);
|
|
432
|
+
notIncluded.push({ week, reason: 'menu not published yet' });
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const picks = one.result.picks.map((p) => p.name).filter((n) => !!n);
|
|
436
|
+
planned.push(...picks);
|
|
437
|
+
weeks.push({ ...one.result, ...(avoid.length > 0 ? { avoidedFromEarlierWeeks: avoid } : {}) });
|
|
438
|
+
lines.push(`${week}: ${one.result.summary}`);
|
|
439
|
+
if (one.followUp.length > 0)
|
|
440
|
+
followUp.push({ week, calls: one.followUp });
|
|
441
|
+
if (one.real.ok) {
|
|
442
|
+
// weeklyPlan always prices the picks' premiums, so totalWithPremiums is set.
|
|
443
|
+
priced.push({ week, amount: one.real.value.totalWithPremiums });
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
notIncluded.push({ week, reason: one.estimate ? 'local estimate only' : 'no price' });
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
const amount = Math.round(priced.reduce((sum, p) => sum + p.amount, 0) * 100) / 100;
|
|
450
|
+
const left = notIncluded.map((n) => `${n.week} (${n.reason})`).join(', ');
|
|
451
|
+
lines.push(priced.length > 0
|
|
452
|
+
? `Total for ${priced.length} week${priced.length === 1 ? '' : 's'} (HelloFresh prices, premiums included): ` +
|
|
453
|
+
`$${amount.toFixed(2)}${left ? ` — not included: ${left}` : ''}.`
|
|
454
|
+
: `No week has HelloFresh’s own price (${notIncluded.map((n) => n.week).join(', ')}: ` +
|
|
455
|
+
`${[...new Set(notIncluded.map((n) => n.reason))].join(', ')}), so there is no real total.`);
|
|
255
456
|
return minifiedResult({
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
...(estimate
|
|
264
|
-
? { estimate: { ...compactEstimate(estimate), ...(estimate.voucher ? { voucher: estimate.voucher.code } : {}), priceFrom: price.priceFrom } }
|
|
265
|
-
: { estimateWarning: `HelloFresh shows no price for ${sku} on this account, so the box total is not estimated.` }),
|
|
266
|
-
...(estimate && args.box_number === undefined ? { boxNumberNote: numbering.note } : {}),
|
|
267
|
-
summary: summaryText({ profile, people, deliveryDate: delivery.deliveryDate, picks, estimate }),
|
|
268
|
-
...(locked ? { locked } : {}),
|
|
269
|
-
...(alreadyPicked ? { note: 'The suggested meals are already picked for this week; nothing to change.' } : {}),
|
|
270
|
-
...(notes.length > 0 ? { notes } : {}),
|
|
457
|
+
weeks,
|
|
458
|
+
summary: lines.join('\n'),
|
|
459
|
+
total: {
|
|
460
|
+
...(priced.length > 0 ? { amount, currency: 'USD' } : {}),
|
|
461
|
+
pricedWeeks: priced.map((p) => p.week),
|
|
462
|
+
...(notIncluded.length > 0 ? { notIncluded } : {}),
|
|
463
|
+
},
|
|
271
464
|
followUp,
|
|
272
465
|
...(followUp.length > 0
|
|
273
466
|
? {
|
|
274
|
-
followUpNote: 'Run
|
|
275
|
-
'confirmToken to preview, then again with the token. A size change may reset the picks, so pick last.',
|
|
467
|
+
followUpNote: 'Run each week’s calls in order once the user approves the summary. Each is confirm-gated: call it ' +
|
|
468
|
+
'without confirmToken to preview, then again with the token. A size change may reset the picks, so pick last.',
|
|
276
469
|
}
|
|
277
470
|
: {}),
|
|
278
|
-
|
|
471
|
+
note: 'Which profile each week gets is the caller’s decision (e.g. from a calendar); this tool only plans what it was given.',
|
|
279
472
|
});
|
|
280
473
|
});
|
|
281
474
|
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// The promo tracker (hf_promo_status): the subscription's voucher, how many of
|
|
2
|
+
// its discounted boxes are used and left, and — for each upcoming running week
|
|
3
|
+
// — the REAL discount from HelloFresh's own price calculator (POST /gw/calculate,
|
|
4
|
+
// in-tab), with warnings for the things that silently cost money: a running
|
|
5
|
+
// week with no discount, a coupon percent that changes week to week, an
|
|
6
|
+
// inactive voucher, few discounted boxes left. Read-only. The coupon code is
|
|
7
|
+
// the user's own data and is returned; ids and the address never are.
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { mapWithConcurrency, minifiedResult, toolAnnotations } from '@chrischall/mcp-utils';
|
|
10
|
+
import { projectDelivery } from '../normalize.js';
|
|
11
|
+
import { foodMeta } from './hints.js';
|
|
12
|
+
import { PRICE_CONCURRENCY, attempt, boxNumbering, boxNumbers, byWeek, calculatorGate, dateOf, hellofreshPrice, loadAccount, nextBoxNumberParam, readMenu, } from './shared.js';
|
|
13
|
+
/** Fewer discounted boxes left than this is worth a warning. */
|
|
14
|
+
export const LOW_BOXES_LEFT = 4;
|
|
15
|
+
export function registerPromoTools(server, deps) {
|
|
16
|
+
const { client } = deps;
|
|
17
|
+
const now = deps.now ?? Date.now;
|
|
18
|
+
server.registerTool('hf_promo_status', {
|
|
19
|
+
description: 'Track the HelloFresh promo/voucher: the coupon code, whether it is active, its box limit, boxes used and ' +
|
|
20
|
+
'left, and for each upcoming running week the REAL discount from HelloFresh’s own price calculator (coupon ' +
|
|
21
|
+
'percent, discount and grand total, US dollars) — with warnings when a running week gets no discount, the ' +
|
|
22
|
+
'coupon percent changes from the week before, the voucher is inactive, or fewer than 4 discounted boxes are ' +
|
|
23
|
+
'left. Answers "is my HelloFresh discount still applying?".',
|
|
24
|
+
annotations: toolAnnotations({ title: 'HelloFresh: promo status', readOnly: true, idempotent: true, openWorld: true }),
|
|
25
|
+
_meta: foodMeta('lookup', { aliases: ['hellofresh promo', 'hellofresh voucher', 'hellofresh discount', 'coupon'] }),
|
|
26
|
+
inputSchema: z.object({ next_box_number: nextBoxNumberParam }),
|
|
27
|
+
}, async ({ next_box_number }) => {
|
|
28
|
+
const account = await loadAccount(client);
|
|
29
|
+
const { subscription } = account;
|
|
30
|
+
const voucher = subscription.voucherInfo ?? undefined;
|
|
31
|
+
const active = voucher?.is_active === true;
|
|
32
|
+
const boxLimit = voucher?.discount_settings?.box_count;
|
|
33
|
+
// Boxes used = every box before the next running one (the documented numbering).
|
|
34
|
+
const numbering = boxNumbering(subscription, next_box_number);
|
|
35
|
+
const boxesUsed = numbering.next - 1;
|
|
36
|
+
const boxesUsedBasis = next_box_number !== undefined ? 'given' : subscription.firstBoxDelivered === false ? 'exact' : 'assumed';
|
|
37
|
+
const boxesRemaining = boxLimit !== undefined ? Math.max(0, boxLimit - boxesUsed) : undefined;
|
|
38
|
+
const deliveries = ((await client.listDeliveries()).items ?? []).map(projectDelivery).sort(byWeek);
|
|
39
|
+
const numbers = boxNumbers(deliveries, numbering.next);
|
|
40
|
+
const runningWeeks = deliveries.filter((d) => d.status === 'running' && !!d.week);
|
|
41
|
+
const gate = calculatorGate();
|
|
42
|
+
const perWeek = await mapWithConcurrency(runningWeeks, PRICE_CONCURRENCY, async (d) => {
|
|
43
|
+
// Every running week is numbered (boxNumbers numbers exactly these).
|
|
44
|
+
const boxNumber = numbers.get(d.week);
|
|
45
|
+
const head = {
|
|
46
|
+
week: d.week,
|
|
47
|
+
deliveryDate: dateOf(d.deliveryDate),
|
|
48
|
+
boxNumber,
|
|
49
|
+
...(boxLimit !== undefined && boxNumber > boxLimit ? { pastVoucherLimit: true } : {}),
|
|
50
|
+
};
|
|
51
|
+
// The calculator is sent the week's picks, as the site does; an unreadable menu sends none.
|
|
52
|
+
const menu = await attempt(() => readMenu(client, account, d, d.week));
|
|
53
|
+
const priced = await gate(() => hellofreshPrice(client, account, {
|
|
54
|
+
week: d.week,
|
|
55
|
+
sku: d.sku,
|
|
56
|
+
people: d.servings,
|
|
57
|
+
deliveryOption: d.deliveryOption,
|
|
58
|
+
deliveryDate: d.deliveryDate,
|
|
59
|
+
recipeIndexes: menu.ok ? menu.value.selected.map((m) => m.index) : [],
|
|
60
|
+
premiumCents: undefined,
|
|
61
|
+
}, now()));
|
|
62
|
+
if (!priced.ok)
|
|
63
|
+
return { ...head, price: 'unavailable', reason: priced.reason };
|
|
64
|
+
const { couponPercent, discount, grandTotal } = priced.value;
|
|
65
|
+
const price = Object.fromEntries(Object.entries({ couponPercent, discount, grandTotal }).filter(([, v]) => v !== undefined));
|
|
66
|
+
return { ...head, price };
|
|
67
|
+
});
|
|
68
|
+
const warnings = [];
|
|
69
|
+
if (!voucher)
|
|
70
|
+
warnings.push('This subscription has no voucher, so no discount should be expected.');
|
|
71
|
+
else if (!active)
|
|
72
|
+
warnings.push('The voucher is not active, so no discount should be expected.');
|
|
73
|
+
let previous;
|
|
74
|
+
for (const w of perWeek) {
|
|
75
|
+
if (w.price === 'unavailable')
|
|
76
|
+
continue;
|
|
77
|
+
const box = ` (box ${w.boxNumber})`;
|
|
78
|
+
if (!((w.price.discount ?? 0) > 0)) {
|
|
79
|
+
const past = w.pastVoucherLimit ? ` — box ${w.boxNumber} is past the voucher’s ${boxLimit}-box limit` : '';
|
|
80
|
+
warnings.push(`${w.week}${box}: HelloFresh’s calculator applies no discount to this running week${past}.`);
|
|
81
|
+
}
|
|
82
|
+
const percent = w.price.couponPercent;
|
|
83
|
+
if (percent === undefined)
|
|
84
|
+
continue;
|
|
85
|
+
if (previous && previous.percent !== percent) {
|
|
86
|
+
warnings.push(`${w.week}: the coupon percent changes from ${previous.percent}% (${previous.week}) to ${percent}%.`);
|
|
87
|
+
}
|
|
88
|
+
previous = { week: w.week, percent };
|
|
89
|
+
}
|
|
90
|
+
if (boxesRemaining !== undefined && boxesRemaining < LOW_BOXES_LEFT) {
|
|
91
|
+
const assumed = boxesUsedBasis === 'assumed' ? ' (boxes used is an assumption — see boxesUsedNote)' : '';
|
|
92
|
+
warnings.push(`Only ${boxesRemaining} discounted boxes left on the voucher, of ${boxLimit}${assumed}.`);
|
|
93
|
+
}
|
|
94
|
+
return minifiedResult({
|
|
95
|
+
couponCode: subscription.couponCode || voucher?.code || undefined,
|
|
96
|
+
active,
|
|
97
|
+
boxLimit,
|
|
98
|
+
boxesUsed,
|
|
99
|
+
boxesUsedBasis,
|
|
100
|
+
boxesUsedNote: numbering.note,
|
|
101
|
+
boxesRemaining,
|
|
102
|
+
perWeek,
|
|
103
|
+
warnings,
|
|
104
|
+
priceNote: 'perWeek prices come from HelloFresh’s own price calculator (its checkout price, US dollars; premium recipe ' +
|
|
105
|
+
'upcharges are not part of it). "unavailable" means the calculator could not be read for that week (reason given).',
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
}
|