@allyhub/sdk 0.15.3 → 0.17.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/CHANGELOG.md +17 -0
- package/dist/node_modules/@allyhub/audio/dist/index.d.ts +1 -0
- package/dist/node_modules/@allyhub/audio/dist/index.js +1 -1
- package/dist/node_modules/@allyhub/audio/dist/place.d.ts +26 -0
- package/dist/node_modules/@allyhub/audio/dist/place.js +2 -0
- package/dist/node_modules/@allyhub/audio/package.json +1 -1
- package/dist/node_modules/@allyhub/chart/package.json +1 -1
- package/dist/node_modules/@allyhub/diagnostics/package.json +1 -1
- package/dist/node_modules/@allyhub/formats/dist/data/slides.d.ts +15 -3
- package/dist/node_modules/@allyhub/formats/dist/data/slides.js +1 -1
- package/dist/node_modules/@allyhub/formats/dist/data/website.js +1 -1
- package/dist/node_modules/@allyhub/formats/dist/items.d.ts +70 -11
- package/dist/node_modules/@allyhub/formats/dist/items.js +1 -1
- package/dist/node_modules/@allyhub/formats/dist/layout.d.ts +15 -4
- package/dist/node_modules/@allyhub/formats/dist/layout.js +1 -1
- package/dist/node_modules/@allyhub/formats/dist/schemas.d.ts +4 -0
- package/dist/node_modules/@allyhub/formats/package.json +1 -1
- package/dist/node_modules/@allyhub/image-draw/dist/stage.js +1 -1
- package/dist/node_modules/@allyhub/image-draw/package.json +1 -1
- package/dist/node_modules/@allyhub/image-measure/package.json +1 -1
- package/dist/node_modules/@allyhub/image-sdk/package.json +1 -1
- package/dist/node_modules/@allyhub/measure/package.json +1 -1
- package/dist/node_modules/@allyhub/ooxml/package.json +1 -1
- package/dist/node_modules/@allyhub/piece/dist/layer.d.ts +15 -0
- package/dist/node_modules/@allyhub/piece/dist/page/index.d.ts +1 -0
- package/dist/node_modules/@allyhub/piece/dist/page/index.js +1 -1
- package/dist/node_modules/@allyhub/piece/dist/page/runtimes.d.ts +9 -0
- package/dist/node_modules/@allyhub/piece/dist/page/runtimes.js +2 -0
- package/dist/node_modules/@allyhub/piece/dist/source.d.ts +53 -10
- package/dist/node_modules/@allyhub/piece/dist/source.js +1 -1
- package/dist/node_modules/@allyhub/piece/package.json +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/account.d.ts +10 -11
- package/dist/node_modules/@allyhub/protocol/dist/account.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/auth.d.ts +7 -0
- package/dist/node_modules/@allyhub/protocol/dist/auth.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/blobs.d.ts +9 -0
- package/dist/node_modules/@allyhub/protocol/dist/blobs.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/cli.d.ts +2 -0
- package/dist/node_modules/@allyhub/protocol/dist/cli.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/cloud-computer.d.ts +77 -0
- package/dist/node_modules/@allyhub/protocol/dist/cloud-computer.js +2 -0
- package/dist/node_modules/@allyhub/protocol/dist/cloud.d.ts +39 -9
- package/dist/node_modules/@allyhub/protocol/dist/cloud.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/editor.d.ts +15 -15
- package/dist/node_modules/@allyhub/protocol/dist/events.d.ts +441 -5
- package/dist/node_modules/@allyhub/protocol/dist/events.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/files.d.ts +2 -2
- package/dist/node_modules/@allyhub/protocol/dist/frame.d.ts +4 -1
- package/dist/node_modules/@allyhub/protocol/dist/frame.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/index.d.ts +8 -0
- package/dist/node_modules/@allyhub/protocol/dist/index.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/jobs.d.ts +5 -5
- package/dist/node_modules/@allyhub/protocol/dist/kinds.d.ts +45 -0
- package/dist/node_modules/@allyhub/protocol/dist/kinds.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/paywall.d.ts +134 -0
- package/dist/node_modules/@allyhub/protocol/dist/paywall.js +2 -0
- package/dist/node_modules/@allyhub/protocol/dist/place.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/presence.d.ts +3 -3
- package/dist/node_modules/@allyhub/protocol/dist/purposes.d.ts +47 -0
- package/dist/node_modules/@allyhub/protocol/dist/purposes.js +2 -0
- package/dist/node_modules/@allyhub/protocol/dist/routes.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/shares.d.ts +52 -5
- package/dist/node_modules/@allyhub/protocol/dist/shares.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/sidebar-deletion.d.ts +34 -0
- package/dist/node_modules/@allyhub/protocol/dist/sidebar-deletion.js +2 -0
- package/dist/node_modules/@allyhub/protocol/dist/sites.d.ts +1 -0
- package/dist/node_modules/@allyhub/protocol/dist/snapshots.d.ts +21 -52
- package/dist/node_modules/@allyhub/protocol/dist/snapshots.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/studio-server.d.ts +264 -0
- package/dist/node_modules/@allyhub/protocol/dist/studio-server.js +2 -0
- package/dist/node_modules/@allyhub/protocol/dist/studio-tool-calls.d.ts +18 -0
- package/dist/node_modules/@allyhub/protocol/dist/studio-tool-calls.js +2 -0
- package/dist/node_modules/@allyhub/protocol/dist/studio-window-requests.d.ts +97 -0
- package/dist/node_modules/@allyhub/protocol/dist/studio-window-requests.js +2 -0
- package/dist/node_modules/@allyhub/protocol/dist/teams.d.ts +3 -2
- package/dist/node_modules/@allyhub/protocol/dist/teams.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/tools.d.ts +26 -20
- package/dist/node_modules/@allyhub/protocol/dist/tools.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/turns.d.ts +71 -0
- package/dist/node_modules/@allyhub/protocol/dist/turns.js +2 -0
- package/dist/node_modules/@allyhub/protocol/package.json +1 -1
- package/dist/node_modules/@allyhub/runtimes/package.json +1 -1
- package/dist/node_modules/@allyhub/scenery/package.json +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/coerce.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/engine.d.ts +12 -0
- package/dist/node_modules/@allyhub/sheet/dist/calc/engine.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/criteria.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/date.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/dynamic.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/info.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/logical.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/lookup.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/text.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/formula/parse.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.d.ts +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/import.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/read.js +2 -2
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/write.js +1 -1
- package/dist/node_modules/@allyhub/sheet/package.json +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/animation.d.ts +10 -0
- package/dist/node_modules/@allyhub/slides-core/dist/animation.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/api.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/components.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/model.d.ts +3 -1
- package/dist/node_modules/@allyhub/slides-core/dist/resolve.d.ts +5 -0
- package/dist/node_modules/@allyhub/slides-core/dist/resolve.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/package.json +1 -1
- package/dist/node_modules/@allyhub/video-core/package.json +1 -1
- package/dist/node_modules/@allyhub/video-sdk/dist/free.d.ts +1 -1
- package/dist/node_modules/@allyhub/video-sdk/package.json +1 -1
- package/docs/batches.md +4 -4
- package/docs/contract.md +5 -5
- package/docs/defaults.md +2 -2
- package/docs/docs.md +16 -5
- package/docs/footage.md +8 -8
- package/docs/image.md +5 -5
- package/docs/layers.md +12 -10
- package/docs/linkedin.md +1 -1
- package/docs/modeling.md +3 -3
- package/docs/pictures.md +4 -4
- package/docs/project.md +11 -7
- package/docs/scenery.md +3 -3
- package/docs/sheets.md +5 -5
- package/docs/slides.md +3 -3
- package/docs/sound.md +7 -7
- package/docs/speech.md +7 -7
- package/docs/video.md +3 -3
- package/docs/websites.md +3 -3
- package/docs/x.md +2 -2
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{z as t}from"zod";import{openEnum as e}from"./open.js";export const ORGANIZATIONS_PATH="/api/organizations",OrganizationsSchema=t.object({organizations:t.array(t.object({id:t.string(),name:t.string(),slug:t.string(),role:t.string(),dissolvedAt:t.iso.datetime().nullable()}))}),MEMBERS_PATH="/api/organization/members",MembersSchema=t.object({members:t.array(t.object({userId:t.string(),email:t.string(),name:t.string(),role:t.string(),limit:t.number().nullable(),used:t.number()}))}),MemberLimitSchema=t.strictObject({monthly:t.number().nonnegative().nullable()}),ORGANIZATION_PATH="/api/organization",STUDIO_PATH="/api/studio",STUDIO_STATES=["running","paused"],StudioSchema=t.object({studio:t.object({state:e(STUDIO_STATES),organizationId:t.string()}).nullable(),creditsPerHour:t.number(),idle:t.object({minutes:t.number(),chosen:t.number().nullable(),min:t.number(),max:t.number()}).optional(),update:t.object({since:t.string(),by:t.string()}).optional()}),STUDIO_RESTART_PATH="/api/studio/restart",StudioSettingsSchema=t.strictObject({idleMinutes:t.number().int().nullable()}),STUDIO_POLICY_PATH="/api/organization/studio",StudioPolicySchema=t.strictObject({idleMaxMinutes:t.number().int().nullable()}),STUDIO_OPEN_PATH="/api/studio/open",STUDIO_OPEN_STAGES=["balance","hold","create","resume","health","release","install","start","charge","ticket"],StudioOpenStagesSchema=t.record(t.string(),t.number()),StudioOpenedSchema=t.object({origin:t.url(),ticket:t.string().min(1),stages:StudioOpenStagesSchema.optional()}),NDJSON="application/x-ndjson",STUDIO_OPEN_STEPS=["machine","install","start"],STUDIO_START_ENV="ALLYHUB_STUDIO_START",StudioOpenLineSchema=t.union([t.object({step:t.enum(STUDIO_OPEN_STEPS),first:t.boolean().optional()}),t.object({status:t.number(),error:t.object({code:t.string(),message:t.string(),data:t.record(t.string(),t.unknown()).optional()})}),StudioOpenedSchema]),STUDIO_TICKET_PATH="/api/studio/ticket",StudioTicketSchema=t.object({ticket:t.string().min(1)}),STUDIO_PREPARE_PATH="/api/studio/prepare",StudioPreparedSchema=t.object({prepared:t.boolean()}),STUDIO_PAUSE_PATH="/api/studio/pause",StudioPausedSchema=t.object({paused:t.boolean()}),STUDIO_WAKES_PATH="/api/studio/wakes",STUDIO_WAKE_OUTCOMES=["live","failed"],StudioWakeReportSchema=t.strictObject({outcome:t.enum(STUDIO_WAKE_OUTCOMES),error:t.string().max(500).optional(),server:StudioOpenStagesSchema.optional(),window:t.strictObject({open:t.number().optional(),connect:t.number().optional(),enter:t.number().optional(),total:t.number()}),first:t.boolean().optional()}),STUDIO_WAKE_PATH="/api/studio/wake",StudioWakeSchema=t.strictObject({at:t.iso.datetime().nullable()});
|
|
2
|
+
import{z as t}from"zod";import{openEnum as e}from"./open.js";export const ORGANIZATIONS_PATH="/api/organizations",OrganizationsSchema=t.object({organizations:t.array(t.object({id:t.string(),name:t.string(),slug:t.string(),role:t.string(),dissolvedAt:t.iso.datetime().nullable()}))}),MEMBERS_PATH="/api/organization/members",MembersSchema=t.object({members:t.array(t.object({userId:t.string(),email:t.string(),name:t.string(),role:t.string(),limit:t.number().nullable(),used:t.number(),spent:t.number().optional()}))}),MemberLimitSchema=t.strictObject({monthly:t.number().nonnegative().nullable()}),ORGANIZATION_PATH="/api/organization",STUDIO_PATH="/api/studio",STUDIO_STATES=["running","paused"],StudioSchema=t.object({studio:t.object({state:e(STUDIO_STATES),organizationId:t.string()}).nullable(),creditsPerHour:t.number(),idle:t.object({minutes:t.number(),chosen:t.number().nullable(),min:t.number(),max:t.number()}).optional(),update:t.object({since:t.string(),by:t.string().optional()}).optional()}),STUDIO_RESTART_PATH="/api/studio/restart",StudioSettingsSchema=t.strictObject({idleMinutes:t.number().int().nullable()}),STUDIO_POLICY_PATH="/api/organization/studio",StudioPolicySchema=t.strictObject({idleMaxMinutes:t.number().int().nullable()}),STUDIO_OPEN_PATH="/api/studio/open",STUDIO_OPEN_STAGES=["balance","hold","create","resume","health","release","install","start","charge","ticket"],StudioOpenStagesSchema=t.record(t.string(),t.number()),StudioOpenedSchema=t.object({origin:t.url(),ticket:t.string().min(1),stages:StudioOpenStagesSchema.optional()}),NDJSON="application/x-ndjson",STUDIO_OPEN_STEPS=["machine","install","start"],STUDIO_START_ENV="ALLYHUB_STUDIO_START",StudioOpenLineSchema=t.union([t.object({step:t.enum(STUDIO_OPEN_STEPS),first:t.boolean().optional()}),t.object({status:t.number(),error:t.object({code:t.string(),message:t.string(),data:t.record(t.string(),t.unknown()).optional()})}),StudioOpenedSchema]),STUDIO_TICKET_PATH="/api/studio/ticket",StudioTicketSchema=t.object({ticket:t.string().min(1)}),STUDIO_PREPARE_PATH="/api/studio/prepare",StudioPreparedSchema=t.object({prepared:t.boolean()}),STUDIO_PAUSE_PATH="/api/studio/pause",StudioPausedSchema=t.object({paused:t.boolean()}),STUDIO_WAKES_PATH="/api/studio/wakes",STUDIO_WAKE_OUTCOMES=["live","failed"],StudioWakeReportSchema=t.strictObject({outcome:t.enum(STUDIO_WAKE_OUTCOMES),error:t.string().max(500).optional(),server:StudioOpenStagesSchema.optional(),window:t.strictObject({open:t.number().optional(),connect:t.number().optional(),enter:t.number().optional(),total:t.number()}),first:t.boolean().optional()}),STUDIO_WAKE_PATH="/api/studio/wake",StudioWakeSchema=t.strictObject({at:t.iso.datetime().nullable()});
|
|
@@ -29,10 +29,10 @@ export declare const CHECK_STATES: readonly ["off", "waiting", "running", "done"
|
|
|
29
29
|
/** The check the server runs after edits, as it stands. */
|
|
30
30
|
export declare const CheckStateSchema: z.ZodObject<{
|
|
31
31
|
status: z.ZodEnum<{
|
|
32
|
-
|
|
32
|
+
done: "done";
|
|
33
33
|
failed: "failed";
|
|
34
|
+
off: "off";
|
|
34
35
|
running: "running";
|
|
35
|
-
done: "done";
|
|
36
36
|
waiting: "waiting";
|
|
37
37
|
}>;
|
|
38
38
|
revision: z.ZodInt;
|
|
@@ -71,11 +71,11 @@ export declare const ExportJobSchema: z.ZodObject<{
|
|
|
71
71
|
types: z.ZodArray<z.ZodString>;
|
|
72
72
|
options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>>;
|
|
73
73
|
state: z.ZodEnum<{
|
|
74
|
+
done: "done";
|
|
74
75
|
failed: "failed";
|
|
76
|
+
cancelled: "cancelled";
|
|
75
77
|
queued: "queued";
|
|
76
78
|
running: "running";
|
|
77
|
-
done: "done";
|
|
78
|
-
cancelled: "cancelled";
|
|
79
79
|
}>;
|
|
80
80
|
progress: z.ZodNumber;
|
|
81
81
|
files: z.ZodArray<z.ZodString>;
|
|
@@ -86,9 +86,9 @@ export type ExportJob = z.infer<typeof ExportJobSchema>;
|
|
|
86
86
|
export declare const IndexJobSchema: z.ZodObject<{
|
|
87
87
|
file: z.ZodString;
|
|
88
88
|
status: z.ZodEnum<{
|
|
89
|
+
done: "done";
|
|
89
90
|
failed: "failed";
|
|
90
91
|
running: "running";
|
|
91
|
-
done: "done";
|
|
92
92
|
}>;
|
|
93
93
|
message: z.ZodString;
|
|
94
94
|
share: z.ZodOptional<z.ZodNumber>;
|
|
@@ -141,6 +141,8 @@ export declare const PROJECT_TOOLS: {
|
|
|
141
141
|
files: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
142
142
|
entry: z.ZodString;
|
|
143
143
|
}, z.core.$strip>>;
|
|
144
|
+
order: z.ZodOptional<z.ZodNumber>;
|
|
145
|
+
turnInto: z.ZodOptional<z.ZodBoolean>;
|
|
144
146
|
}, z.core.$strip>>;
|
|
145
147
|
}, z.core.$strict>>;
|
|
146
148
|
kinds: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
@@ -165,6 +167,8 @@ export declare const PROJECT_TOOLS: {
|
|
|
165
167
|
files: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
166
168
|
entry: z.ZodString;
|
|
167
169
|
}, z.core.$strip>>;
|
|
170
|
+
order: z.ZodOptional<z.ZodNumber>;
|
|
171
|
+
turnInto: z.ZodOptional<z.ZodBoolean>;
|
|
168
172
|
id: z.ZodString;
|
|
169
173
|
}, z.core.$strip>>>;
|
|
170
174
|
name: z.ZodString;
|
|
@@ -203,6 +207,8 @@ export declare const PROJECT_TOOLS: {
|
|
|
203
207
|
files: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
204
208
|
entry: z.ZodString;
|
|
205
209
|
}, z.core.$strip>>;
|
|
210
|
+
order: z.ZodOptional<z.ZodNumber>;
|
|
211
|
+
turnInto: z.ZodOptional<z.ZodBoolean>;
|
|
206
212
|
}, z.core.$strip>>;
|
|
207
213
|
}, z.core.$strict>;
|
|
208
214
|
}, z.core.$strict>>;
|
|
@@ -274,10 +280,10 @@ export declare const PROJECT_TOOLS: {
|
|
|
274
280
|
}, z.core.$strict>>;
|
|
275
281
|
readonly "check/results": MethodSpec<z.ZodObject<{}, z.core.$strict>, z.ZodObject<{
|
|
276
282
|
status: z.ZodEnum<{
|
|
277
|
-
|
|
283
|
+
done: "done";
|
|
278
284
|
failed: "failed";
|
|
285
|
+
off: "off";
|
|
279
286
|
running: "running";
|
|
280
|
-
done: "done";
|
|
281
287
|
waiting: "waiting";
|
|
282
288
|
}>;
|
|
283
289
|
revision: z.ZodInt;
|
|
@@ -317,11 +323,11 @@ export declare const PROJECT_TOOLS: {
|
|
|
317
323
|
types: z.ZodArray<z.ZodString>;
|
|
318
324
|
options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>>;
|
|
319
325
|
state: z.ZodEnum<{
|
|
326
|
+
done: "done";
|
|
320
327
|
failed: "failed";
|
|
328
|
+
cancelled: "cancelled";
|
|
321
329
|
queued: "queued";
|
|
322
330
|
running: "running";
|
|
323
|
-
done: "done";
|
|
324
|
-
cancelled: "cancelled";
|
|
325
331
|
}>;
|
|
326
332
|
progress: z.ZodNumber;
|
|
327
333
|
files: z.ZodArray<z.ZodString>;
|
|
@@ -339,11 +345,11 @@ export declare const PROJECT_TOOLS: {
|
|
|
339
345
|
types: z.ZodArray<z.ZodString>;
|
|
340
346
|
options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>>;
|
|
341
347
|
state: z.ZodEnum<{
|
|
348
|
+
done: "done";
|
|
342
349
|
failed: "failed";
|
|
350
|
+
cancelled: "cancelled";
|
|
343
351
|
queued: "queued";
|
|
344
352
|
running: "running";
|
|
345
|
-
done: "done";
|
|
346
|
-
cancelled: "cancelled";
|
|
347
353
|
}>;
|
|
348
354
|
progress: z.ZodNumber;
|
|
349
355
|
files: z.ZodArray<z.ZodString>;
|
|
@@ -418,10 +424,10 @@ export declare const TOOL_NOTIFICATIONS: {
|
|
|
418
424
|
}, z.core.$strict>>;
|
|
419
425
|
readonly "check/changed": import("./jsonrpc.js").NotificationSpec<z.ZodObject<{
|
|
420
426
|
status: z.ZodEnum<{
|
|
421
|
-
|
|
427
|
+
done: "done";
|
|
422
428
|
failed: "failed";
|
|
429
|
+
off: "off";
|
|
423
430
|
running: "running";
|
|
424
|
-
done: "done";
|
|
425
431
|
waiting: "waiting";
|
|
426
432
|
}>;
|
|
427
433
|
revision: z.ZodInt;
|
|
@@ -455,11 +461,11 @@ export declare const TOOL_NOTIFICATIONS: {
|
|
|
455
461
|
types: z.ZodArray<z.ZodString>;
|
|
456
462
|
options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>>;
|
|
457
463
|
state: z.ZodEnum<{
|
|
464
|
+
done: "done";
|
|
458
465
|
failed: "failed";
|
|
466
|
+
cancelled: "cancelled";
|
|
459
467
|
queued: "queued";
|
|
460
468
|
running: "running";
|
|
461
|
-
done: "done";
|
|
462
|
-
cancelled: "cancelled";
|
|
463
469
|
}>;
|
|
464
470
|
progress: z.ZodNumber;
|
|
465
471
|
files: z.ZodArray<z.ZodString>;
|
|
@@ -470,9 +476,9 @@ export declare const TOOL_NOTIFICATIONS: {
|
|
|
470
476
|
jobs: z.ZodArray<z.ZodObject<{
|
|
471
477
|
file: z.ZodString;
|
|
472
478
|
status: z.ZodEnum<{
|
|
479
|
+
done: "done";
|
|
473
480
|
failed: "failed";
|
|
474
481
|
running: "running";
|
|
475
|
-
done: "done";
|
|
476
482
|
}>;
|
|
477
483
|
message: z.ZodString;
|
|
478
484
|
share: z.ZodOptional<z.ZodNumber>;
|
|
@@ -483,8 +489,8 @@ export declare const TOOL_NOTIFICATIONS: {
|
|
|
483
489
|
id: z.ZodString;
|
|
484
490
|
about: z.ZodObject<{
|
|
485
491
|
kind: z.ZodEnum<{
|
|
486
|
-
studio: "studio";
|
|
487
492
|
agent: "agent";
|
|
493
|
+
studio: "studio";
|
|
488
494
|
project: "project";
|
|
489
495
|
piece: "piece";
|
|
490
496
|
plugin: "plugin";
|
|
@@ -504,12 +510,12 @@ export declare const TOOL_NOTIFICATIONS: {
|
|
|
504
510
|
}>;
|
|
505
511
|
name: z.ZodOptional<z.ZodString>;
|
|
506
512
|
state: z.ZodEnum<{
|
|
513
|
+
done: "done";
|
|
507
514
|
failed: "failed";
|
|
515
|
+
cancelled: "cancelled";
|
|
516
|
+
paused: "paused";
|
|
508
517
|
queued: "queued";
|
|
509
518
|
running: "running";
|
|
510
|
-
paused: "paused";
|
|
511
|
-
done: "done";
|
|
512
|
-
cancelled: "cancelled";
|
|
513
519
|
}>;
|
|
514
520
|
completed: z.ZodNumber;
|
|
515
521
|
total: z.ZodOptional<z.ZodNumber>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{z as e}from"zod";import{FolderSchema as n,PathSchema as t}from"./files.js";import{JobSchema as d}from"./jobs.js";import{PreviewSchema as h}from"./app.js";import{ListedKindSchema as
|
|
2
|
+
import{z as e}from"zod";import{FolderSchema as n,PathSchema as t}from"./files.js";import{JobSchema as d}from"./jobs.js";import{PreviewSchema as h}from"./app.js";import{ListedKindSchema as c,ProjectKindSchema as g}from"./kinds.js";import{RPC_ERROR as b,RpcError as f,method as s,notification as o}from"./jsonrpc.js";export const VersionRef={item:t,version:e.string().optional()};export{ImageSchema}from"./image.js";export const ProblemSchema=e.looseObject({code:e.string(),level:e.enum(["error","warn"]),message:e.string(),piece:e.string().optional(),part:e.string().optional(),t:e.number().optional()}),FactSchema=e.looseObject({kind:e.string(),data:e.unknown()}),CHECK_STATES=["off","waiting","running","done","failed"],CheckStateSchema=e.strictObject({status:e.enum(CHECK_STATES),revision:e.int().nonnegative(),problems:e.array(ProblemSchema),facts:e.array(FactSchema),versions:e.strictObject({total:e.int(),loaded:e.int(),reused:e.int()}).optional(),error:e.string().optional()}),EXPORT_STATES=["queued","running","done","failed","cancelled"],ExportOptionsSchema=e.record(e.string(),e.union([e.string(),e.boolean()])),ExportJobSchema=e.strictObject({id:e.string(),item:t,versions:e.array(e.string()),types:e.array(e.string()),options:ExportOptionsSchema.optional(),state:e.enum(EXPORT_STATES),progress:e.number().min(0).max(1),files:e.array(t),error:e.string().optional()}),IndexJobSchema=e.strictObject({file:t,status:e.enum(["running","done","failed"]),message:e.string(),share:e.number().min(0).max(1).optional()}),FramedPageSchema=e.strictObject({url:e.string(),probe:e.string()}),COMPONENT_STILLS_MAX=64,PROJECT_TOOLS={"project/items":s(e.strictObject({}),e.strictObject({items:e.array(e.strictObject({id:t,kind:e.string().min(1).optional(),dir:n,entry:t,title:e.string().optional(),groups:e.array(e.string()),files:e.array(e.object({path:e.string().min(1),role:e.string().min(1).optional()})).optional(),kindInfo:c.optional()})),kinds:e.array(g).optional(),name:e.string(),scope:e.string(),revision:e.int().nonnegative()}),"The project's items, as its list (allyhub.yaml) names them, in its order: each a file of a kind, or a file an editor opens by its name."),"project/file":s(e.strictObject({path:t}),e.strictObject({item:e.strictObject({id:t,kind:e.string().min(1).optional(),dir:n,entry:t,title:e.string().optional(),groups:e.array(e.string()),kindInfo:c.optional()})}),"A file of the project as it opens in an editor, whether the list names it or not: the item it is, else the file in the editor that opens it by its name (an `editors` contribution's `files`); not found for a file that is not there or that no editor opens."),"items/preview":s(e.strictObject({item:t}),e.strictObject({preview:h.optional()}),"What an item looks like (named by its id or its file), as its kind's provider makes it from its files now (a video's still, an image's boards, a deck's slide, a doc's first paragraphs, a sheet's first rows); none where its kind gives none."),"items/page":s(e.strictObject({item:t,folder:n.optional(),file:t.optional()}),e.strictObject({url:e.string()}),"The address of an item's page, for a frame (a report's page, a website built as files): `folder` served as the root of an origin of its own, the project's files alone (no other address loads), every file the page loads recorded as the item's input, so the item's editors hear `items/changed` when one changes."),"work/add":s(e.strictObject({kind:e.string().min(1),file:t,title:e.string().min(1).optional()}),e.strictObject({}),"Adds a piece to the end of the project's list (allyhub.yaml), the rest of it as it was; nothing when its file is listed already."),"components/list":s(e.strictObject({item:t}),e.strictObject({components:e.array(e.strictObject({of:e.string(),name:e.string(),label:e.union([e.string(),e.record(e.string(),e.string())]).optional()}))}),"The components an item may place: every one the project's files define, by the reference it would write."),"components/stage":s(e.strictObject(VersionRef),FramedPageSchema,"The address of a page that draws an item's components one at a time, for a sandboxed frame (docs/design/components-everywhere.md): a deck's by the name its module gives it, a doc's by the reference its block writes. Its parent posts `{ type: \"component\", name, props, width, height, t }` and hears `component-ready`, or `error` with what went wrong."),"components/stills":s(e.strictObject({...VersionRef,components:e.array(e.strictObject({name:e.string(),props:e.record(e.string(),e.unknown()),width:e.number().positive(),height:e.number().positive(),t:e.number().nonnegative()})).min(1).max(COMPONENT_STILLS_MAX)}),e.strictObject({stills:e.array(e.strictObject({key:e.string(),url:e.url()}).nullable())}),"An item's components (a deck's, a doc's) drawn alone, transparent, as the render draws them: pictures kept by what they show, given as addresses."),"check/results":s(e.strictObject({}),CheckStateSchema,"The check run after edits, as it stands."),"check/run":s(e.strictObject({}),e.strictObject({}),"Runs the check again now."),"export/start":s(e.strictObject({item:t,versions:e.array(e.string()).optional(),types:e.array(e.string().min(1)).max(10).optional(),options:ExportOptionsSchema.optional()}),e.strictObject({job:ExportJobSchema}),"Exports an item (its versions, as file types) into .allyhub/out/; `export/changed` says how it goes."),"export/cancel":s(e.strictObject({job:e.string()}),e.strictObject({}),"Stops an export."),"export/list":s(e.strictObject({}),e.strictObject({jobs:e.array(ExportJobSchema),reveal:e.boolean()}),"The exports asked for since the server started."),"export/reveal":s(e.strictObject({path:t}),e.strictObject({}),"Shows an exported file in the person's file manager, where the server runs on their computer (`export/list`'s `reveal`).")},PROJECT_TOOL_SOURCES={"project/items":"project","project/file":"asleep","items/preview":"asleep","items/page":"asleep","work/add":"machine","components/list":"asleep","components/stage":"asleep","components/stills":"project","check/results":"project","check/run":"machine","export/start":"machine","export/cancel":"machine","export/list":"asleep","export/reveal":"machine"},AGENT_DOES={"project/items":{files:"allyhub.yaml"},"project/file":{files:"the project's files"},"items/preview":{command:"frames"},"items/page":{command:"frames"},"work/add":{files:"allyhub.yaml"},"components/list":{files:"the project's components (defineComponent)"},"components/stage":{command:"frames"},"components/stills":{command:"frames"},"check/results":{command:"inspect"},"check/run":{command:"check"},"export/start":{command:"render"},"export/cancel":{command:"render"},"export/list":{files:".allyhub/out/"},"export/reveal":{files:".allyhub/out/"}};export function toolSpec(r){return Object.hasOwn(PROJECT_TOOLS,r)?PROJECT_TOOLS[r]:void 0}export function toolCaller(r){return async(p,i,l)=>{const{result:m}=await p["tools/call"]({name:i,arguments:l}),a=r[i]?.result.safeParse(m);if(!a?.success)throw new f(b.internal,`The answer to the tool ${i} is not what the protocol says.`);return a.data}}export const TOOL_NOTIFICATIONS={"project/changed":o(e.strictObject({revision:e.int().nonnegative()}),"The build saw a change: a stage or a timeline asked for now is the new one's."),"items/changed":o(e.strictObject({item:t,paths:e.array(e.string())}),`One of an item's inputs changed (docs/design/item-files.md, "An item's inputs"): a file its build lists, or one its pages loaded.`),"check/changed":o(CheckStateSchema,"The check started, ended or failed."),"export/changed":o(e.strictObject({job:ExportJobSchema}),"An export moved on."),"index/changed":o(e.strictObject({jobs:e.array(IndexJobSchema)}),"Making what the media index lacks moved on."),"jobs/changed":o(e.strictObject({job:d,item:t}),"A job of the process the server runs in (docs/design/jobs.md: a website starting, an export) moved on, about a thing in this project: what the window lists, for the editor of that thing to show where the person waits on it.")};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** Where a turn is recorded, by its trace id: `PUT` as it begins, `PATCH` as it ends. */
|
|
3
|
+
export declare const TURN_PATH = "/api/turns/:traceId";
|
|
4
|
+
export declare const turnPath: (traceId: string) => string;
|
|
5
|
+
/** The most characters of a prompt kept; a longer one is cut there. */
|
|
6
|
+
export declare const TURN_QUERY_AT_MOST = 32000;
|
|
7
|
+
/** The most characters of an error kept. */
|
|
8
|
+
export declare const TURN_ERROR_AT_MOST = 2000;
|
|
9
|
+
/** A turn began: the Host's `PUT turnPath(traceId)`. Strict, as everything the services receive. */
|
|
10
|
+
export declare const TurnStartedSchema: z.ZodObject<{
|
|
11
|
+
at: z.ZodISODateTime;
|
|
12
|
+
agent: z.ZodString;
|
|
13
|
+
sessionId: z.ZodString;
|
|
14
|
+
projectId: z.ZodOptional<z.ZodString>;
|
|
15
|
+
query: z.ZodString;
|
|
16
|
+
from: z.ZodEnum<{
|
|
17
|
+
person: "person";
|
|
18
|
+
schedule: "schedule";
|
|
19
|
+
hook: "hook";
|
|
20
|
+
}>;
|
|
21
|
+
}, z.core.$strict>;
|
|
22
|
+
export type TurnStarted = z.infer<typeof TurnStartedSchema>;
|
|
23
|
+
/** The most characters of the agent's answer kept; a longer one is cut there. */
|
|
24
|
+
export declare const TURN_ANSWER_AT_MOST = 64000;
|
|
25
|
+
/** The most steps a turn's record keeps; past them, only how many more there were. */
|
|
26
|
+
export declare const TURN_STEPS_AT_MOST = 1000;
|
|
27
|
+
/** The most characters of a step's title kept. */
|
|
28
|
+
export declare const TURN_STEP_TITLE_AT_MOST = 200;
|
|
29
|
+
/**
|
|
30
|
+
* A step the agent took in a turn, as ACP told it (`tool_call`, then its
|
|
31
|
+
* `tool_call_update`s): its kind (`read`, `edit`, `execute`, … or `other`),
|
|
32
|
+
* its title as the agent wrote it, how it ended, when it began and how long
|
|
33
|
+
* it took. Never its input, its output or a file's content.
|
|
34
|
+
*/
|
|
35
|
+
export declare const TurnStepSchema: z.ZodObject<{
|
|
36
|
+
at: z.ZodISODateTime;
|
|
37
|
+
kind: z.ZodString;
|
|
38
|
+
title: z.ZodString;
|
|
39
|
+
status: z.ZodString;
|
|
40
|
+
ms: z.ZodOptional<z.ZodNumber>;
|
|
41
|
+
}, z.core.$strict>;
|
|
42
|
+
export type TurnStep = z.infer<typeof TurnStepSchema>;
|
|
43
|
+
/** A turn ended: the Host's `PATCH turnPath(traceId)`. */
|
|
44
|
+
export declare const TurnEndedSchema: z.ZodObject<{
|
|
45
|
+
at: z.ZodISODateTime;
|
|
46
|
+
durationMs: z.ZodNumber;
|
|
47
|
+
ended: z.ZodString;
|
|
48
|
+
error: z.ZodOptional<z.ZodString>;
|
|
49
|
+
changed: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
50
|
+
pieces: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
51
|
+
answer: z.ZodOptional<z.ZodString>;
|
|
52
|
+
steps: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
53
|
+
at: z.ZodISODateTime;
|
|
54
|
+
kind: z.ZodString;
|
|
55
|
+
title: z.ZodString;
|
|
56
|
+
status: z.ZodString;
|
|
57
|
+
ms: z.ZodOptional<z.ZodNumber>;
|
|
58
|
+
}, z.core.$strict>>>;
|
|
59
|
+
moreSteps: z.ZodOptional<z.ZodNumber>;
|
|
60
|
+
}, z.core.$strict>;
|
|
61
|
+
export type TurnEnded = z.infer<typeof TurnEndedSchema>;
|
|
62
|
+
/** What the server answers either: that it kept it. */
|
|
63
|
+
export declare const TurnAnswerSchema: z.ZodObject<{
|
|
64
|
+
ok: z.ZodBoolean;
|
|
65
|
+
}, z.core.$strip>;
|
|
66
|
+
export type TurnAnswer = z.infer<typeof TurnAnswerSchema>;
|
|
67
|
+
/** Cuts a prompt to what is kept. */
|
|
68
|
+
export declare const keptQuery: (text: string) => string;
|
|
69
|
+
/** The trace id a turn's route names. */
|
|
70
|
+
export declare const TurnTraceIdSchema: z.ZodString;
|
|
71
|
+
//# sourceMappingURL=turns.d.ts.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
+
import{z as t}from"zod";import{EventIdSchema as n,EventTraceIdSchema as s,EventWordSchema as o}from"./events.js";export const TURN_PATH="/api/turns/:traceId",turnPath=e=>TURN_PATH.replace(":traceId",e),TURN_QUERY_AT_MOST=32e3,TURN_ERROR_AT_MOST=2e3;const r=t.record(o,t.number().int().min(0).max(1e6)).refine(e=>Object.keys(e).length<=32,"At most 32 words.");export const TurnStartedSchema=t.strictObject({at:t.iso.datetime(),agent:o,sessionId:n,projectId:n.optional(),query:t.string().max(TURN_QUERY_AT_MOST),from:t.enum(["person","schedule","hook"])}),TURN_ANSWER_AT_MOST=64e3,TURN_STEPS_AT_MOST=1e3,TURN_STEP_TITLE_AT_MOST=200,TurnStepSchema=t.strictObject({at:t.iso.datetime(),kind:o,title:t.string().max(TURN_STEP_TITLE_AT_MOST),status:o,ms:t.number().int().min(0).optional()}),TurnEndedSchema=t.strictObject({at:t.iso.datetime(),durationMs:t.number().int().min(0).max(10080*60*1e3),ended:o,error:t.string().max(TURN_ERROR_AT_MOST).optional(),changed:r,pieces:r,answer:t.string().max(TURN_ANSWER_AT_MOST).optional(),steps:t.array(TurnStepSchema).max(TURN_STEPS_AT_MOST).optional(),moreSteps:t.number().int().min(0).optional()}),TurnAnswerSchema=t.object({ok:t.boolean()}),keptQuery=e=>e.length<=TURN_QUERY_AT_MOST?e:e.slice(0,TURN_QUERY_AT_MOST),TurnTraceIdSchema=s;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{parseValue as
|
|
2
|
+
import{parseValue as y}from"numfmt";import{CellError as f,ERR as i,Matrix as h,numberText as M,readTyped as b}from"../values.js";const l=new Map,w=5e4;export function numberOfText(e){if(!/\d/u.test(e))return;if(l.has(e))return l.get(e);l.size>=w&&l.clear();const r=E(e);return l.set(e,r),r}function E(e){const r=b(e.trim());if(typeof r.value=="number")return r.value;if(e.trim()==="")return;const t=y(e);if(typeof t?.value=="number")return t.value;const n=/^(\s*\d{1,2})([A-Za-z]{3,}\s+\d{2,4}\s*)$/u.exec(e);if(!n)return;const o=y(`${n[1]??""} ${n[2]??""}`);return typeof o?.value=="number"?o.value:void 0}export function toNumber(e){return typeof e=="number"?e:e==null?0:typeof e=="boolean"?e?1:0:e instanceof f?e:numberOfText(e)??i.VALUE}export function toText(e){return typeof e=="string"?e:e==null?"":typeof e=="number"?M(e):typeof e=="boolean"?e?"TRUE":"FALSE":e}export function toBool(e){if(typeof e=="boolean")return e;if(e==null)return!1;if(typeof e=="number")return e!==0;if(e instanceof f)return e;const r=e.trim().toUpperCase();return r==="TRUE"?!0:r==="FALSE"?!1:i.VALUE}const x=e=>typeof e=="number"?0:typeof e=="string"?1:2;export const sameNumber=(e,r)=>e===r?!0:Math.abs(e-r)>1e-13*Math.max(Math.abs(e),Math.abs(r))?!1:Number(e.toPrecision(15))===Number(r.toPrecision(15));export function compare(e,r){const t=e??(typeof r=="string"?"":typeof r=="boolean"?!1:0),n=r??(typeof e=="string"?"":typeof e=="boolean"?!1:0),o=x(t),c=x(n);if(o!==c)return o<c?-1:1;if(typeof t=="number"&&typeof n=="number")return sameNumber(t,n)?0:t<n?-1:1;if(typeof t=="string"&&typeof n=="string"){const a=t.toUpperCase(),u=n.toUpperCase();return a===u?0:a<u?-1:1}return t===n?0:t===!1?-1:1}export const finite=e=>Number.isFinite(e)?e:i.NUM;export function snap(e,r,t){return Math.abs(e)<=Math.max(Math.abs(r),Math.abs(t))*89e-17?0:e}export function arithmetic(e,r,t){const n=toNumber(r);if(n instanceof f)return n;const o=toNumber(t);if(o instanceof f)return o;switch(e){case"+":return finite(snap(n+o,n,o));case"-":return finite(snap(n-o,n,o));case"*":return finite(n*o);case"/":return o===0?i.DIV0:finite(n/o);default:return n===0&&o===0?i.NUM:n===0&&o<0?i.DIV0:finite(n**o)}}export function binary(e,r,t){if(r instanceof f)return r;if(t instanceof f)return t;switch(e){case"&":{const n=toText(r);if(n instanceof f)return n;const o=toText(t);return o instanceof f?o:`${n}${o}`}case"=":return compare(r,t)===0;case"<>":return compare(r,t)!==0;case"<":return compare(r,t)<0;case"<=":return compare(r,t)<=0;case">":return compare(r,t)>0;case">=":return compare(r,t)>=0;default:return arithmetic(e,r,t)}}export function mapBoth(e,r,t){const n=Math.max(e.rows,r.rows),o=Math.max(e.cols,r.cols),c=new Array(n*o),a=(u,s,d)=>{const p=u.rows===1?0:s,m=u.cols===1?0:d;return p<u.rows&&m<u.cols?u.at(p,m):void 0};for(let u=0;u<n;u++)for(let s=0;s<o;s++){const d=a(e,u,s),p=a(r,u,s);c[u*o+s]=d===void 0||p===void 0?i.NA:t(d,p)}return new h(n,o,c)}
|
|
@@ -63,6 +63,18 @@ export declare class Calc {
|
|
|
63
63
|
private lambdaSlots;
|
|
64
64
|
private readonly stack;
|
|
65
65
|
private readonly inCycle;
|
|
66
|
+
/**
|
|
67
|
+
* Cells the outermost compute set aside, each until one a read of it had
|
|
68
|
+
* to wait for is computed: read meanwhile, one is in a circular reference
|
|
69
|
+
* longer than the stack, as a cell on it would be.
|
|
70
|
+
*/
|
|
71
|
+
private readonly waiting;
|
|
72
|
+
/** The outermost compute's cells, each waiting for the next: the last is on the stack. */
|
|
73
|
+
private work;
|
|
74
|
+
/** What was on the stack when a cell of `work` was set aside: from it to the read that waits. */
|
|
75
|
+
private readonly unwound;
|
|
76
|
+
/** The cells of `work` whose cells a circular reference counted already: each is counted once. */
|
|
77
|
+
private readonly counted;
|
|
66
78
|
/** Cells around a circular reference a pass found, where the workbook iterates. */
|
|
67
79
|
private readonly looping;
|
|
68
80
|
/** The data tables' cells, by their formula; made when first asked. */
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{MAX_COLUMNS as v,MAX_ROWS as C,overlaps as N}from"../address.js";import{CellError as b,ERR as h,Matrix as m,serialOfDate as G}from"../values.js";import{filteredRows as W}from"../filter.js";import{FormulaCell as k,SharedFormula as P}from"../workbook.js";import{analyse as q}from"./analyse.js";import{Graph as H}from"./graph.js";import{fromWire as $,matrixFromWire as j,matrixToWire as K,toWire as X}from"./remote.js";import{RangeCache as U}from"./indexes.js";import{binary as O,mapBoth as Y,toNumber as Z}from"./coerce.js";import{FUNCTIONS as _}from"./functions/index.js";import{fillsGroup as J,paramAt as Q}from"./functions/define.js";import{Lambda as S,MISSING as T,Missing as L,Reference as d,Union as x,pushAll as M}from"./types.js";export function seededRandom(l){let t=2166136261;for(let e=0;e<l.length;e++)t=Math.imul(t^l.charCodeAt(e),16777619);return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const tt=new Set,et=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,I=2e3,st=50,nt=1024;class V extends Error{cell;constructor(t){super("computed first"),this.cell=t}}const B=12;export const ITERATION_DEFAULTS={max_iterations:100,max_change:.001};function ot(l,t){return typeof l=="number"&&typeof t=="number"?Math.abs(t-l):l===t||l instanceof b&&t instanceof b&&l.code===t.code?0:1/0}const it=(l,t)=>!l||!t?l===t:l.rows!==t.rows||l.cols!==t.cols?!1:l.data.every((e,s)=>e===t.data[s]),rt=(l,t)=>l.row<t.row||l.row===t.row&&l.col<t.col;export class Calc{cycles=[];computed=0;iterated;nowSerial;random;pass=0;lambdaSlots=0;stack=[];inCycle=new Set;looping=new Set;tablesOf;inTables=!1;loop=new Set;around=new Set;namesAt=new Set;graph=new H({readsOf:t=>this.readsOf(t),readsAt:(t,e,s,o)=>this.readsAt(t,e,s,o)});graphMade=!1;reports;replica;settled=new Set;moved=[];unsettled=new Set;hiddenBy=new Map;volatiles=new Set;indexes=new U;results=new U;wb;constructor(t,e={}){this.wb=t;const{now:s,random:o}=e;this.reports=e.report===!0,this.replica=e.replica===!0,this.nowSerial=()=>G(s??new Date),this.random=o??Math.random;for(const n of t.sheets)n.expand()}allCells(){return this.wb.sheets.flatMap(t=>[...t.formulas])}filtered(t){return this.hiddenBy.get(t)??tt}calculate(){if(this.replica){for(const e of this.wb.sheets)e.clearSpills();for(const e of this.allCells())e.state=0;return this}this.hiddenBy.clear(),this.indexes.clear(),this.results.clear();for(const e of this.wb.sheets)e.clearSpills();this.moved=[],this.unsettled.clear();const t=this.allCells();for(const e of t)e.state=1;return this.cycles=[],this.inCycle.clear(),this.looping.clear(),this.loop=new Set,this.around.clear(),this.tablesOf=void 0,this.iterated=void 0,this.run(t),this.applyFilters(t),this}iteration(){const t=this.wb.file.iteration;if(t!==void 0)return{max_iterations:(t===!0?void 0:t.max_iterations)??ITERATION_DEFAULTS.max_iterations,max_change:(t===!0?void 0:t.max_change)??ITERATION_DEFAULTS.max_change}}applyFilters(t){for(const e of this.wb.sheets){if(e.filters.every(o=>o.columns.size===0))continue;const s=W(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>et.test(e.shared.text))){for(const e of t)e.state=1;this.cycles=[],this.inCycle.clear(),this.run([...t])}}value(t,e,s){return this.read(t,e,s)}read(t,e,s){const o=t.at(e,s);return o===void 0?this.ghost(t,e,s):typeof o!="object"||o instanceof b?o:(o.state===1?this.computeNow(o):o.state===2&&this.circular(o),o.value)}ghost(t,e,s){const o=t.spillAt(e,s);if(!o)return null;if(o.state===1)this.computeNow(o);else if(o.state===2)return this.circular(o),null;const{array:n}=o;return n&&t.spillAt(e,s)===o?n.at(e-o.row,s-o.col):null}circular(t){const e=this.stack.indexOf(t),s=e<0?[t]:this.stack.slice(e);if(this.iteration()){for(const o of s)this.looping.add(o);return}s.some(o=>this.inCycle.has(o))||this.cycles.push(s);for(const o of s)this.inCycle.add(o)}forget(t,e){this.indexes.changed(t,e),this.results.changed(t,e)}run(t){for(const e of t){const{sheet:s,row:o,col:n}=e;this.forget(s,{top:o,left:n,bottom:o,right:n}),e.spill&&this.forget(s,e.spill)}t.some(e=>this.loop.has(e))&&(this.iterated=void 0),this.computed=0;for(const e of this.order(t))e.state===1&&this.computeNow(e);this.spread(),this.iterate(),this.tables()}tableCells(){if(this.tablesOf)return this.tablesOf;const t=new Map;for(const e of this.allCells())if(R(e.shared.node)){const s=t.get(e.shared);s?s.push(e):t.set(e.shared,[e])}return this.tablesOf=t,t}tables(){if(this.replica||this.inTables)return;const t=this.tableCells();if(t.size!==0){this.inTables=!0;try{const e=[];for(const[n,i]of t)this.table(n,i),e.push(...i);const s=new Set(e),o=new Set;for(const n of e)for(const i of this.readers(n.sheet,n.row,n.col))s.has(i)||o.add(i);o.size>0&&this.mark([...o])}finally{this.inTables=!1}}}operations(t){const e=new Map,s=(n,i)=>{const r=n===void 0?void 0:this.eval(n,i);return r instanceof d&&r.single?r:void 0},o=t.map(n=>{const i=n.shared.node;if(i?.kind!=="call")return h.REF;const r=F(n),a=s(i.args[0],r);if(!a)return h.REF;const c=[];for(let f=1;f+1<i.args.length;f+=2){const u=s(i.args[f],r),w=s(i.args[f+1],r);if(!u||!w)return h.REF;const E=`${String(u.sheet.index)}:${String(u.top)}:${String(u.left)}`;e.has(E)||e.set(E,{sheet:u.sheet,row:u.top,col:u.left,slot:u.sheet.at(u.top,u.left)});const A=this.read(w.sheet,w.top,w.left);c.push({sheet:u.sheet,row:u.top,col:u.left,slot:A??void 0})}return this.change(c),this.read(a.sheet,a.top,a.left)});e.size>0&&this.change([...e.values()]),t.forEach((n,i)=>{n.value=o[i]??null,this.reports&&this.settled.add(n)})}table(t,e){const[s]=e,o=t.node;if(!s||o?.kind!=="call")return;if(o.args.length>2){this.operations(e);return}const{sheet:n}=s,i=t.row,r=t.col,a={sheet:n,row:i,col:r,dr:0,dc:0},c=p=>{if(p===void 0||p.kind==="missing")return null;const g=this.eval(p,a);return g instanceof d&&g.single?g:void 0},f=c(o.args[0]),u=c(o.args[1]),w=p=>{for(const g of e)g.value=p,this.reports&&this.settled.add(g)};if(f===void 0||u===void 0||f===null&&u===null||i===0||r===0){w(h.REF);return}const A=[f,u].filter(p=>p!==null).map(p=>({sheet:p.sheet,row:p.top,col:p.left,slot:p.sheet.at(p.top,p.left)})),y=e.map(p=>{const g=[];f&&g.push({sheet:f.sheet,row:f.top,col:f.left,slot:this.read(n,i-1,p.col)??void 0}),u&&g.push({sheet:u.sheet,row:u.top,col:u.left,slot:this.read(n,p.row,r-1)??void 0}),this.change(g);const[z,D]=f&&u?[i-1,r-1]:u?[i-1,p.col]:[p.row,r-1];return this.read(n,z,D)});this.change(A),e.forEach((p,g)=>{p.value=y[g]??null,this.reports&&this.settled.add(p)})}iterate(){const t=this.iteration();if(!t||this.looping.size===0){this.looping.clear();return}for(const i of this.looping)this.around.add(i);const e=this.downstream([...this.looping]),s=[...e];let o=1,n=1/0;for(;o<t.max_iterations;){const i=s.map(r=>r.value);this.looping.clear(),this.indexes.clear(),this.results.clear();for(const r of s)r.state=1;for(const r of this.order(s))r.state===1&&this.compute(r);if(o+=1,n=0,s.forEach((r,a)=>{n=Math.max(n,ot(i[a]??null,r.value))}),n<t.max_change)break}this.looping.clear(),this.spread(),this.looping.clear(),this.loop=e,this.iterated={cells:s.length,iterations:o,change:n}}iterates(t){return this.loop.has(t)}circularCells(){return[...this.around]}downstream(t){const e=new Set(t),s=[...t];for(;;){const o=s.pop();if(!o)break;for(const n of this.readers(o.sheet,o.row,o.col))e.has(n)||(e.add(n),s.push(n))}return e}spread(){for(let t=0;this.moved.length>0;t++){const e=this.moved;if(this.moved=[],t>=B*2)break;if(t>=B)for(const{sheet:n,area:i}of e)for(const r of n.spillsIn(i))this.unsettled.add(r);this.build();const s=new Set,o=[...this.unsettled];for(const{sheet:n,area:i}of e)M(o,this.readersIn(n,i)),M(o,n.blockedBy(i));for(;;){const n=o.pop();if(!n)break;s.has(n)||(s.add(n),M(o,this.readers(n.sheet,n.row,n.col)))}if(s.size!==0){for(const n of s)n.state=1,this.inCycle.delete(n);this.cycles=this.cycles.filter(n=>!n.some(i=>s.has(i))),this.indexes.clear(),this.results.clear();for(const n of this.order([...s]))n.state===1&&this.computeNow(n)}}for(const t of this.wb.sheets)t.tighten()}spillOut(t,e){const{sheet:s,row:o,col:n}=t,i={top:o,left:n,bottom:o+e.rows-1,right:n+e.cols-1},r=s.spillsIn(i).filter(c=>c!==t);if(i.bottom>=C||i.right>=v||s.holds(i,t)||s.tables.some(c=>N(c.area,i))||s.merges.some(c=>N(c,i))||r.some(c=>rt(c,t)))return s.blockSpill(t,i),!1;for(const c of r)this.evict(c,i);return s.placeSpill(t,i),!0}evict(t,e){const{sheet:s,spill:o}=t;o&&(s.clearSpill(t),s.blockSpill(t,e),t.array=void 0,t.value=h.SPILL,this.moved.push({sheet:s,area:o}),this.reports&&this.settled.add(t))}analysisOf(t){return t.analysis??=q(this.wb,t)}readsOf(t){const e=[],s=F(t);for(const o of this.analysisOf(t.shared).reads){const n=o.kind==="ref"?this.resolveRef(o,s):this.structured(o,s);n instanceof d?e.push(n):n instanceof x&&e.push(...n.parts)}return e}readsAt(t,e,s,o){const n={sheet:e,row:s,col:o,dr:s-t.row,dc:o-t.col},i=[];for(const r of this.analysisOf(t).reads){const a=r.kind==="ref"?this.resolveRef(r,n):this.structured(r,n);a instanceof d?i.push(a):a instanceof x?i.push(...a.parts):i.push(void 0)}return i}readsOfFormula(t,e){const s=[],o={sheet:e,row:t.row,col:t.col,dr:0,dc:0};for(const n of this.analysisOf(t).reads){const i=n.kind==="ref"?this.resolveRef(n,o):this.structured(n,o);i instanceof d?s.push(i):i instanceof x&&s.push(...i.parts)}return s}order(t){this.pass+=1;const{pass:e}=this,s=[],o=new Set,n=[],i=r=>{r.mark=e,n.push({cell:r,reads:this.pending(r,o),next:0})};for(const r of t)if(r.mark!==e)for(i(r);;){const a=n[n.length-1];if(!a)break;const c=a.reads[a.next];a.next+=1,c===void 0?(s.push(a.cell),n.pop()):c.mark!==e&&c.state===1&&i(c)}return s}pending(t,e){const s=[];for(const o of this.readsOf(t)){const{sheet:n}=o;if(o.single){const r=n.at(o.top,o.left);r instanceof k&&s.push(r);continue}if(e.has(o.key))continue;e.add(o.key);const i=Math.min(o.right,n.columnsUsed-1);for(let r=o.left;r<=i;r++){if(!n.hasFormulas(r))continue;const[a,c]=n.span(r,o.top,o.bottom);for(let f=a;f<=c;f++){const u=n.at(f,r);u instanceof k&&s.push(u)}}}return s}computeNow(t){if(this.stack.length>0){if(this.stack.length>=st&&this.stack.length<I)throw new V(t);this.compute(t);return}const e=[t],s=new Set([t]);for(;e.length>0;){const o=e[e.length-1];if(o===void 0)break;if(o.state!==1){e.pop();continue}try{this.compute(o),e.pop()}catch(n){if(!(n instanceof V))throw n;for(const i of this.stack)i.state=1;if(this.stack.length=0,s.has(n.cell)){n.cell.value=h.CALC,n.cell.state=0;continue}s.add(n.cell),e.push(n.cell)}}}compute(t){if(this.stack.length>=I){t.value=h.CALC,t.state=0;return}t.state=2,this.stack.push(t),this.computed+=1;const{node:e}=t.shared,s=R(e)?t.value:e?this.top(this.eval(e,F(t))):h.NAME;this.stack.pop(),this.unsettled.has(t)&&!this.inCycle.has(t)&&(this.inCycle.add(t),this.cycles.push([t])),this.settleResult(t,s),t.state=0,this.reports&&this.settled.add(t)}settleResult(t,e){const{sheet:s}=t,o=t.spill,n=t.array;if(s.clearSpill(t),t.array=void 0,e instanceof m){const r=ct(e);r.rows*r.cols<=1||this.spillOut(t,r)?(t.array=r,t.value=r.at(0,0)??0):t.value=h.SPILL}else t.value=e;this.inCycle.has(t)&&(s.clearSpill(t),t.value=h.CYCLE,t.array=void 0);const i=t.spill;(!it(n,t.array)||!at(o,i))&&(o&&this.moved.push({sheet:s,area:o}),i&&this.moved.push({sheet:s,area:i}))}top(t){const e=this.flat(t);return e===null?0:e}flat(t){return t instanceof d?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof x?h.VALUE:t instanceof S?h.CALC:t instanceof L?null:t}matrix(t){const e=t.bottom===C-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===v-1?Math.min(t.right,t.sheet.columnsUsed-1):t.right,o=Math.max(0,e-t.top+1),n=Math.max(0,s-t.left+1),i=new Array(o*n);for(let r=0;r<o;r++)for(let a=0;a<n;a++)i[r*n+a]=this.read(t.sheet,t.top+r,t.left+a);return new m(o,n,i)}each(t,e){const{sheet:s}=t,o=Math.min(t.right,s.columnsUsed-1);for(let n=t.left;n<=o;n++){const[i,r]=s.span(n,t.top,t.bottom);for(let a=i;a<=r;a++){const c=s.at(a,n);if(c===void 0){s.spillAt(a,n)&&e(this.read(s,a,n),a,n);continue}e(typeof c=="object"&&!(c instanceof b)?this.read(s,a,n):c,a,n)}}}evaluate(t,e){const{node:s}=t;return s?this.top(this.eval(s,{sheet:e,row:t.row,col:t.col,dr:0,dc:0})):h.NAME}evaluateAt(t,e,s,o){const{node:n}=t;return n?this.top(this.eval(n,{sheet:e,row:s,col:o,dr:s-t.row,dc:o-t.col})):h.NAME}bound(t,e,s){const o=t.abs?t.index:t.index+e;return o<0||o>=s?void 0:o}resolveRef(t,e){const s=t.sheet===void 0?e.sheet:this.wb.sheet(t.sheet);if(!s)return h.REF;let o=0,n=v-1,i=0,r=C-1;if(t.col){const a=this.bound(t.col,e.dc,v),c=this.bound(t.col2??t.col,e.dc,v);if(a===void 0||c===void 0)return h.REF;o=Math.min(a,c),n=Math.max(a,c)}if(t.row){const a=this.bound(t.row,e.dr,C),c=this.bound(t.row2??t.row,e.dr,C);if(a===void 0||c===void 0)return h.REF;i=Math.min(a,c),r=Math.max(a,c)}if(t.lastSheet!==void 0){const a=this.wb.sheet(t.lastSheet);if(!a)return h.REF;const[c,f]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new x(this.wb.sheets.slice(c,f+1).map(u=>new d(u,i,o,r,n)))}return new d(s,i,o,r,n)}structured(t,e){const s=t.table===void 0?e.sheet.tableAt(e.row,e.col):this.wb.table(t.table);return s?lt(s,t,e):t.table===void 0?h.REF:h.NAME}nameValue(t,e,s){const o=t.toLowerCase();if(s===void 0)for(let c=e.scope;c;c=c.parent){const f=c.names.get(o);if(f!==void 0)return f}const n=s===void 0?e.sheet:this.wb.sheet(s);if(!n)return h.REF;const i=this.wb.name(t,n);if(i){if(!i.formula)return i.value??null;const{node:c}=i.formula;if(!c)return h.NAME;if(this.namesAt.has(o))return h.CYCLE;this.namesAt.add(o);try{return this.eval(c,{sheet:e.sheet,row:0,col:0,dr:0,dc:0})}finally{this.namesAt.delete(o)}}const r=this.wb.table(t);if(r){const{top:c,left:f,bottom:u,right:w}=r.data;return new d(r.sheet,c,f,u,w)}const a=_.get(t.toUpperCase());return a&&a.kind!=="lazy"?ht(a,t,e):h.NAME}intersect(t,e){return!(t instanceof d)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new d(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new d(t.sheet,t.top,e.col,t.top,e.col):h.VALUE}unary(t,e){const s=this.flat(t);return s instanceof m?new m(s.rows,s.cols,s.data.map(e)):e(s)}operate(t,e,s){const o=this.flat(e),n=this.flat(s);if(o instanceof m||n instanceof m){const i=(r,a)=>O(t,r,a);return Y(o instanceof m?o:new m(1,1,[o]),n instanceof m?n:new m(1,1,[n]),i)}return O(t,o,n)}rangeOf(t,e){return t instanceof b?t:e instanceof b?e:!(t instanceof d)||!(e instanceof d)||t.sheet!==e.sheet?h.REF:new d(t.sheet,Math.min(t.top,e.top),Math.min(t.left,e.left),Math.max(t.bottom,e.bottom),Math.max(t.right,e.right))}intersection(t,e){if(t instanceof b)return t;if(e instanceof b)return e;if(!(t instanceof d)||!(e instanceof d))return h.REF;const s=Math.max(t.top,e.top),o=Math.max(t.left,e.left),n=Math.min(t.bottom,e.bottom),i=Math.min(t.right,e.right);return t.sheet!==e.sheet||s>n||o>i?h.NULL:new d(t.sheet,s,o,n,i)}union(t,e){if(t instanceof b)return t;if(e instanceof b)return e;const s=n=>n instanceof d?[n]:n instanceof x?n.parts:[],o=[...s(t),...s(e)];return o.length===0?h.VALUE:new x(o)}eval(t,e){switch(t.kind){case"number":case"string":case"boolean":return t.value;case"error":return b.of(t.code);case"missing":return T;case"ref":return this.resolveRef(t,e);case"structured":return this.structured(t,e);case"name":return this.nameValue(t.name,e,t.sheet);case"paren":return this.eval(t.arg,e);case"unary":{const s=this.eval(t.arg,e);return t.op==="@"?this.intersect(s,e):t.op==="+"?s:this.unary(s,o=>O("*",o,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,o=>O("/",o,100));if(s instanceof d&&s.single){const o=s.sheet.at(s.top,s.left);if(o instanceof k){this.read(s.sheet,s.top,s.left);const{spill:n}=o;if(n)return new d(s.sheet,n.top,n.left,n.bottom,n.right);if(o.array)return s}}return h.REF}case"binary":{const s=this.eval(t.left,e),o=this.eval(t.right,e);return t.op===":"?this.rangeOf(s,o):t.op===" "?this.intersection(s,o):t.op===","?this.union(s,o):this.operate(t.op,s,o)}case"array":return new m(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(o=>{const n=this.eval(o,e);return n instanceof m||n instanceof d||n instanceof x||n instanceof S||n instanceof L?h.VALUE:n})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof S?this.invoke(s,t.args,e):s instanceof b?s:h.VALUE}case"call":return this.call(t.name,t.args,e)}}invoke(t,e,s){return this.apply(t,e.map(o=>this.eval(o,s)))}apply(t,e){if(t.named!==void 0){const n=_.get(t.named);return!n||n.kind==="lazy"?h.NAME:e.length<n.min||e.length>n.max?h.VALUE:this.applyFunction(n,e,t.frame)}if(e.length>t.params.length)return h.VALUE;const s=t.params.length+1;if(this.lambdaSlots+s>nt)return h.NUM;const o=new Map;t.params.forEach((n,i)=>{o.set(n,e[i]??T)}),this.lambdaSlots+=s;try{return this.eval(t.body,{...t.frame,scope:{names:o,...t.frame.scope?{parent:t.frame.scope}:{}}})}finally{this.lambdaSlots-=s}}call(t,e,s){const o=_.get(t);if(!o){const n=this.nameValue(t,s);return n instanceof S?this.invoke(n,e,s):h.NAME}return e.length<o.min||e.length>o.max?h.VALUE:o.kind==="lazy"?o.call(e,{calc:this,frame:s}):this.applyFunction(o,e.map(n=>this.eval(n,s)),s)}applyFunction(t,e,s){const o={calc:this,frame:s};if(t.kind==="lazy")return h.VALUE;if(t.kind==="raw")return J(t.params,e.length)?this.over(e,i=>Q(t.params,i)==="value",i=>t.call(i,o)):h.VALUE;const n=t.errors===!0;return this.over(e,()=>!0,i=>{const r=i.map(a=>a instanceof L?void 0:a);if(!n){for(const a of r)if(a instanceof b)return a}return t.call(...r)})}over(t,e,s){const o=[],n=new Map;for(const[f,u]of t.entries()){if(!e(f)||u instanceof L){o.push(u);continue}const w=this.flat(u);w instanceof m&&n.set(f,w),o.push(w)}if(n.size===0)return s(o);const i=[...n.values()],r=Math.max(...i.map(f=>f.rows)),a=Math.max(...i.map(f=>f.cols));if(r===0||a===0)return h.CALC;const c=new Array(r*a);for(let f=0;f<r;f++)for(let u=0;u<a;u++){const w=o.map((E,A)=>{const y=n.get(A);if(!y)return E;const p=y.rows===1?0:f,g=y.cols===1?0:u;return p<y.rows&&g<y.cols?y.at(p,g):h.NA});c[f*a+u]=this.element(s(w))}return new m(r,a,c)}element(t){const e=this.flat(t);return e instanceof m?e.rows>0&&e.cols>0?e.at(0,0):h.CALC:e}scalar(t,e){if(t instanceof L)return null;if(t instanceof d){const s=this.intersect(t,e);return s instanceof d?this.read(s.sheet,s.top,s.left):h.VALUE}return t instanceof m?t.at(0,0):t instanceof x||t instanceof S?h.VALUE:t}recalculate(){if(this.replica)return this;const t=this.allCells().filter(e=>this.analysisOf(e.shared).volatile);return this.mark(t),this}build(){if(this.graphMade)return;this.graphMade=!0;const t=this.allCells();for(const e of t)this.analysisOf(e.shared).volatile&&this.volatiles.add(e);this.graph.build(t)}addEdges(t){this.analysisOf(t.shared).volatile&&this.volatiles.add(t),this.graph.add(t)}removeEdges(t){this.volatiles.delete(t),this.graph.remove(t)}readersIn(t,e){return this.graph.readersIn(t,e)}readers(t,e,s){return this.build(),this.graph.readers(t,e,s)}precedents(t){return this.readsOf(t)}mark(t){this.build();const e=new Set(t),s=[...t];for(;;){const o=s.pop();if(!o)break;for(const n of this.readers(o.sheet,o.row,o.col))e.has(n)||(e.add(n),s.push(n))}this.settle(e)}settle(t){for(const e of t)e.state=1,this.inCycle.delete(e);this.cycles=this.cycles.filter(e=>!e.some(s=>t.has(s))),this.run([...t])}set(t,e,s,o){const n=typeof o=="string"&&o.length>1&&o.startsWith("=")?new k(new P(o,e,s,{file:""}),t,e,s):void 0;this.change([{sheet:t,row:e,col:s,slot:n??o??void 0}])}prepare(){this.build()}change(t){if(this.replica){for(const{sheet:n,row:i,col:r,slot:a}of t){const c=n.at(i,r);c instanceof k&&n.clearSpill(c),n.replace(i,r,a),a instanceof k&&(a.state=0)}return}this.build();const e=new Set,s=[...this.unsettled];this.unsettled.clear();const o=new Set;for(const{sheet:n,row:i,col:r,slot:a}of t){const c=n.at(i,r);(c instanceof k&&R(c.shared.node)||a instanceof k&&R(a.shared.node))&&(this.tablesOf=void 0),c instanceof k&&(o.add(c),this.inCycle.delete(c),this.removeEdges(c),e.delete(c),c.spill&&this.moved.push({sheet:n,area:c.spill}),n.clearSpill(c));const f=n.spillAt(i,r);f&&s.push(f),s.push(...n.blockedBy({top:i,left:r,bottom:i,right:r})),n.replace(i,r,a),this.forget(n,{top:i,left:r,bottom:i,right:r}),a instanceof k&&(this.addEdges(a),e.add(a))}o.size>0&&(this.cycles=this.cycles.filter(n=>!n.some(i=>o.has(i))));for(const{sheet:n,row:i,col:r}of t)M(s,this.readers(n,i,r));for(M(s,this.volatiles);;){const n=s.pop();if(!n)break;e.has(n)||(e.add(n),M(s,this.readers(n.sheet,n.row,n.col)))}this.settle(e)}setName(t,e){const s=this.wb.name(t);if(!s)throw new Error(`There is no name ${t}.`);s.value=e,delete s.formula;const o=t.toLowerCase();this.mark(this.allCells().filter(n=>this.analysisOf(n.shared).names.has(o)))}takeComputed(){const t=[];for(const e of this.settled)t.push({sheet:e.sheet.index,row:e.row,col:e.col,value:X(e.value),...e.array?{array:K(e.array)}:{},...e.spill?{spill:e.spill}:{},...e.blocked?{blocked:e.blocked}:{}});return this.settled.clear(),{cells:t,cycles:this.cycles.map(e=>e.map(s=>({sheet:s.sheet.index,row:s.row,col:s.col}))),hidden:[...this.hiddenBy].map(([e,s])=>({sheet:e.index,rows:[...s]}))}}receive(t){for(const s of t.cells){const o=this.wb.sheets[s.sheet],n=o?.at(s.row,s.col);!o||!(n instanceof k)||(o.clearSpill(n),n.value=$(s.value),n.array=s.array?j(s.array):void 0,s.spill&&o.placeSpill(n,s.spill),s.blocked&&o.blockSpill(n,s.blocked),n.state=0)}this.cycles=t.cycles.flatMap(s=>{const o=s.flatMap(n=>{const i=this.wb.sheets[n.sheet]?.at(n.row,n.col);return i instanceof k?[i]:[]});return o.length>0?[o]:[]});const e=new Map(this.hiddenBy);this.hiddenBy.clear();for(const{sheet:s,rows:o}of t.hidden){const n=this.wb.sheets[s];if(!n)continue;const i=e.get(n),r=i?.size===o.length&&o.every(a=>i.has(a));this.hiddenBy.set(n,r?i:new Set(o))}for(const s of this.wb.sheets)s.tighten()}numberOf(t){return Z(t)}}const at=(l,t)=>l?.top===t?.top&&l?.left===t?.left&&l?.bottom===t?.bottom&&l?.right===t?.right;function ct(l){return l.data.some(t=>t===null)?new m(l.rows,l.cols,l.data.map(t=>t??0)):l}function R(l){return l?.kind==="call"&&l.name==="TABLE"}function F(l){return{sheet:l.sheet,row:l.row,col:l.col,dr:l.row-l.shared.row,dc:l.col-l.shared.col}}function lt(l,t,e){const{sheet:s}=l,o=l.area;let n=o.left,i=o.right;const[r,a]=t.columns;if(r!==void 0){const A=l.column(r),y=a===void 0?A:l.column(a);if(!A||!y)return h.REF;n=o.left+Math.min(A.index,y.index),i=o.left+Math.max(A.index,y.index)}if(t.thisRow)return e.sheet!==s||e.row<=o.top||e.row>o.bottom?h.VALUE:new d(s,e.row,n,e.row,i);const c=t.sections;if(c.includes("#Totals"))return h.REF;const f=c.includes("#All")||c.includes("#Headers"),u=c.length===0||c.includes("#All")||c.includes("#Data"),w=f?o.top:o.top+1,E=u?o.bottom:o.top;return new d(s,w,n,E,i)}function ht(l,t,e){return new S([],{kind:"call",name:l.name,written:t,args:[]},e,l.name)}
|
|
2
|
+
import{MAX_COLUMNS as C,MAX_ROWS as L,overlaps as B}from"../address.js";import{CellError as b,ERR as h,Matrix as w,serialOfDate as H}from"../values.js";import{filteredRows as $}from"../filter.js";import{FormulaCell as k,SharedFormula as j}from"../workbook.js";import{analyse as K}from"./analyse.js";import{Graph as X}from"./graph.js";import{fromWire as Y,matrixFromWire as Z,matrixToWire as J,toWire as Q}from"./remote.js";import{RangeCache as z}from"./indexes.js";import{binary as F,mapBoth as tt,toNumber as et}from"./coerce.js";import{FUNCTIONS as U}from"./functions/index.js";import{fillsGroup as st,paramAt as nt}from"./functions/define.js";import{Lambda as E,MISSING as D,Missing as O,Reference as p,Union as A,pushAll as M}from"./types.js";export function seededRandom(l){let t=2166136261;for(let e=0;e<l.length;e++)t=Math.imul(t^l.charCodeAt(e),16777619);return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const ot=new Set,it=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,G=2e3,rt=50,at=1024;class W extends Error{cell;constructor(t){super("computed first"),this.cell=t}}const P=12;export const ITERATION_DEFAULTS={max_iterations:100,max_change:.001};function ct(l,t){return typeof l=="number"&&typeof t=="number"?Math.abs(t-l):l===t||l instanceof b&&t instanceof b&&l.code===t.code?0:1/0}const lt=(l,t)=>!l||!t?l===t:l.rows!==t.rows||l.cols!==t.cols?!1:l.data.every((e,s)=>e===t.data[s]),ht=(l,t)=>l.row<t.row||l.row===t.row&&l.col<t.col;export class Calc{cycles=[];computed=0;iterated;nowSerial;random;pass=0;lambdaSlots=0;stack=[];inCycle=new Set;waiting=new Set;work=[];unwound=new Map;counted=new Set;looping=new Set;tablesOf;inTables=!1;loop=new Set;around=new Set;namesAt=new Set;graph=new X({readsOf:t=>this.readsOf(t),readsAt:(t,e,s,o)=>this.readsAt(t,e,s,o)});graphMade=!1;reports;replica;settled=new Set;moved=[];unsettled=new Set;hiddenBy=new Map;volatiles=new Set;indexes=new z;results=new z;wb;constructor(t,e={}){this.wb=t;const{now:s,random:o}=e;this.reports=e.report===!0,this.replica=e.replica===!0,this.nowSerial=()=>H(s??new Date),this.random=o??Math.random;for(const n of t.sheets)n.expand()}allCells(){return this.wb.sheets.flatMap(t=>[...t.formulas])}filtered(t){return this.hiddenBy.get(t)??ot}calculate(){if(this.replica){for(const e of this.wb.sheets)e.clearSpills();for(const e of this.allCells())e.state=0;return this}this.hiddenBy.clear(),this.indexes.clear(),this.results.clear();for(const e of this.wb.sheets)e.clearSpills();this.moved=[],this.unsettled.clear();const t=this.allCells();for(const e of t)e.state=1;return this.cycles=[],this.inCycle.clear(),this.looping.clear(),this.loop=new Set,this.around.clear(),this.tablesOf=void 0,this.iterated=void 0,this.run(t),this.applyFilters(t),this}iteration(){const t=this.wb.file.iteration;if(t!==void 0)return{max_iterations:(t===!0?void 0:t.max_iterations)??ITERATION_DEFAULTS.max_iterations,max_change:(t===!0?void 0:t.max_change)??ITERATION_DEFAULTS.max_change}}applyFilters(t){for(const e of this.wb.sheets){if(e.filters.every(o=>o.columns.size===0))continue;const s=$(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>it.test(e.shared.text))){for(const e of t)e.state=1;this.cycles=[],this.inCycle.clear(),this.run([...t])}}value(t,e,s){return this.read(t,e,s)}read(t,e,s){const o=t.at(e,s);return o===void 0?this.ghost(t,e,s):typeof o!="object"||o instanceof b?o:(o.state===1&&!this.waiting.has(o)?this.computeNow(o):o.state!==0&&this.circular(o),o.value)}ghost(t,e,s){const o=t.spillAt(e,s);if(!o)return null;if(o.state===1&&!this.waiting.has(o))this.computeNow(o);else if(o.state!==0)return this.circular(o),null;const{array:n}=o;return n&&t.spillAt(e,s)===o?n.at(e-o.row,s-o.col):null}circular(t){const e=this.stack.indexOf(t);let s=e>=0?this.stack.slice(e):[t],o=!1;if(e<0&&this.waiting.has(t)){s=[];for(let n=this.work.lastIndexOf(t);n<this.work.length-1;n++){const i=this.work[n];if(i!==void 0){if(this.counted.has(i)){o=!0;continue}this.counted.add(i),s.push(...this.unwound.get(i)??[i])}}s.push(...this.stack)}if(this.iteration()){for(const n of s)this.looping.add(n);return}!o&&!s.some(n=>this.inCycle.has(n))&&this.cycles.push(s);for(const n of s)this.inCycle.add(n)}forget(t,e){this.indexes.changed(t,e),this.results.changed(t,e)}run(t){for(const e of t){const{sheet:s,row:o,col:n}=e;this.forget(s,{top:o,left:n,bottom:o,right:n}),e.spill&&this.forget(s,e.spill)}t.some(e=>this.loop.has(e))&&(this.iterated=void 0),this.computed=0;for(const e of this.order(t))e.state===1&&this.computeNow(e);this.spread(),this.iterate(),this.tables()}tableCells(){if(this.tablesOf)return this.tablesOf;const t=new Map;for(const e of this.allCells())if(R(e.shared.node)){const s=t.get(e.shared);s?s.push(e):t.set(e.shared,[e])}return this.tablesOf=t,t}tables(){if(this.replica||this.inTables)return;const t=this.tableCells();if(t.size!==0){this.inTables=!0;try{const e=[];for(const[n,i]of t)this.table(n,i),e.push(...i);const s=new Set(e),o=new Set;for(const n of e)for(const i of this.readers(n.sheet,n.row,n.col))s.has(i)||o.add(i);o.size>0&&this.mark([...o])}finally{this.inTables=!1}}}operations(t){const e=new Map,s=(n,i)=>{const r=n===void 0?void 0:this.eval(n,i);return r instanceof p&&r.single?r:void 0},o=t.map(n=>{const i=n.shared.node;if(i?.kind!=="call")return h.REF;const r=T(n),a=s(i.args[0],r);if(!a)return h.REF;const c=[];for(let u=1;u+1<i.args.length;u+=2){const f=s(i.args[u],r),g=s(i.args[u+1],r);if(!f||!g)return h.REF;const S=`${String(f.sheet.index)}:${String(f.top)}:${String(f.left)}`;e.has(S)||e.set(S,{sheet:f.sheet,row:f.top,col:f.left,slot:f.sheet.at(f.top,f.left)});const x=this.read(g.sheet,g.top,g.left);c.push({sheet:f.sheet,row:f.top,col:f.left,slot:x??void 0})}return this.change(c),this.read(a.sheet,a.top,a.left)});e.size>0&&this.change([...e.values()]),t.forEach((n,i)=>{n.value=o[i]??null,this.reports&&this.settled.add(n)})}table(t,e){const[s]=e,o=t.node;if(!s||o?.kind!=="call")return;if(o.args.length>2){this.operations(e);return}const{sheet:n}=s,i=t.row,r=t.col,a={sheet:n,row:i,col:r,dr:0,dc:0},c=d=>{if(d===void 0||d.kind==="missing")return null;const m=this.eval(d,a);return m instanceof p&&m.single?m:void 0},u=c(o.args[0]),f=c(o.args[1]),g=d=>{for(const m of e)m.value=d,this.reports&&this.settled.add(m)};if(u===void 0||f===void 0||u===null&&f===null||i===0||r===0){g(h.REF);return}const x=[u,f].filter(d=>d!==null).map(d=>({sheet:d.sheet,row:d.top,col:d.left,slot:d.sheet.at(d.top,d.left)})),y=d=>{const m=[];u&&m.push({sheet:u.sheet,row:u.top,col:u.left,slot:this.read(n,i-1,d.col)??void 0}),f&&m.push({sheet:f.sheet,row:f.top,col:f.left,slot:this.read(n,d.row,r-1)??void 0}),this.change(m)},N=d=>{const[m,v]=u&&f?[i-1,r-1]:f?[i-1,d.col]:[d.row,r-1];return this.read(n,m,v)},_=d=>u&&f?void 0:f?d.row:d.col,q=e.map((d,m)=>({cell:d,i:m})).sort((d,m)=>f&&!u?d.cell.row-m.cell.row||d.cell.col-m.cell.col:d.cell.col-m.cell.col||d.cell.row-m.cell.row),I=[];let V;for(const{cell:d,i:m}of q){const v=_(d);(v===void 0||v!==V)&&(y(d),V=v),I[m]=N(d)}this.change(x),e.forEach((d,m)=>{d.value=I[m]??null,this.reports&&this.settled.add(d)})}iterate(){const t=this.iteration();if(!t||this.looping.size===0){this.looping.clear();return}for(const i of this.looping)this.around.add(i);const e=this.downstream([...this.looping]),s=[...e];let o=1,n=1/0;for(;o<t.max_iterations;){const i=s.map(r=>r.value);this.looping.clear(),this.indexes.clear(),this.results.clear();for(const r of s)r.state=1;for(const r of this.order(s))r.state===1&&this.computeNow(r);if(o+=1,n=0,s.forEach((r,a)=>{n=Math.max(n,ct(i[a]??null,r.value))}),n<t.max_change)break}this.looping.clear(),this.spread(),this.looping.clear(),this.loop=e,this.iterated={cells:s.length,iterations:o,change:n}}iterates(t){return this.loop.has(t)}circularCells(){return[...this.around]}downstream(t){const e=new Set(t),s=[...t];for(;;){const o=s.pop();if(!o)break;for(const n of this.readers(o.sheet,o.row,o.col))e.has(n)||(e.add(n),s.push(n))}return e}spread(){for(let t=0;this.moved.length>0;t++){const e=this.moved;if(this.moved=[],t>=P*2)break;if(t>=P)for(const{sheet:n,area:i}of e)for(const r of n.spillsIn(i))this.unsettled.add(r);this.build();const s=new Set,o=[...this.unsettled];for(const{sheet:n,area:i}of e)M(o,this.readersIn(n,i)),M(o,n.blockedBy(i));for(;;){const n=o.pop();if(!n)break;s.has(n)||(s.add(n),M(o,this.readers(n.sheet,n.row,n.col)))}if(s.size!==0){for(const n of s)n.state=1,this.inCycle.delete(n);this.cycles=this.cycles.filter(n=>!n.some(i=>s.has(i))),this.indexes.clear(),this.results.clear();for(const n of this.order([...s]))n.state===1&&this.computeNow(n)}}for(const t of this.wb.sheets)t.tighten()}spillOut(t,e){const{sheet:s,row:o,col:n}=t,i={top:o,left:n,bottom:o+e.rows-1,right:n+e.cols-1},r=s.spillsIn(i).filter(c=>c!==t);if(i.bottom>=L||i.right>=C||s.holds(i,t)||s.tables.some(c=>B(c.area,i))||s.merges.some(c=>B(c,i))||r.some(c=>ht(c,t)))return s.blockSpill(t,i),!1;for(const c of r)this.evict(c,i);return s.placeSpill(t,i),!0}evict(t,e){const{sheet:s,spill:o}=t;o&&(s.clearSpill(t),s.blockSpill(t,e),t.array=void 0,t.value=h.SPILL,this.moved.push({sheet:s,area:o}),this.reports&&this.settled.add(t))}analysisOf(t){return t.analysis??=K(this.wb,t)}readsOf(t){const e=[],s=T(t);for(const o of this.analysisOf(t.shared).reads){const n=o.kind==="ref"?this.resolveRef(o,s):this.structured(o,s);n instanceof p?e.push(n):n instanceof A&&e.push(...n.parts)}return e}readsAt(t,e,s,o){const n={sheet:e,row:s,col:o,dr:s-t.row,dc:o-t.col},i=[];for(const r of this.analysisOf(t).reads){const a=r.kind==="ref"?this.resolveRef(r,n):this.structured(r,n);a instanceof p?i.push(a):a instanceof A?i.push(...a.parts):i.push(void 0)}return i}readsOfFormula(t,e){const s=[],o={sheet:e,row:t.row,col:t.col,dr:0,dc:0};for(const n of this.analysisOf(t).reads){const i=n.kind==="ref"?this.resolveRef(n,o):this.structured(n,o);i instanceof p?s.push(i):i instanceof A&&s.push(...i.parts)}return s}order(t){this.pass+=1;const{pass:e}=this,s=[],o=new Set,n=[],i=r=>{r.mark=e,n.push({cell:r,reads:this.pending(r,o),next:0})};for(const r of t)if(r.mark!==e)for(i(r);;){const a=n[n.length-1];if(!a)break;const c=a.reads[a.next];a.next+=1,c===void 0?(s.push(a.cell),n.pop()):c.mark!==e&&c.state===1&&i(c)}return s}pending(t,e){const s=[];for(const o of this.readsOf(t)){const{sheet:n}=o;if(o.single){const r=n.at(o.top,o.left);r instanceof k&&s.push(r);continue}if(e.has(o.key))continue;e.add(o.key);const i=Math.min(o.right,n.columnsUsed-1);for(let r=o.left;r<=i;r++){if(!n.hasFormulas(r))continue;const[a,c]=n.span(r,o.top,o.bottom);for(let u=a;u<=c;u++){const f=n.at(u,r);f instanceof k&&s.push(f)}}}return s}computeNow(t){if(this.stack.length>0){if(this.stack.length>=rt&&this.stack.length<G)throw new W(t);this.compute(t);return}const e=[t],s=new Set([t]);this.work=e;try{for(;e.length>0;){const o=e[e.length-1];if(o===void 0)break;if(this.waiting.delete(o),o.state!==1){e.pop();continue}try{this.compute(o),e.pop()}catch(n){if(!(n instanceof W))throw n;this.unwound.set(o,[...this.stack]),this.counted.delete(o);for(const i of this.stack)i.state=1;if(this.stack.length=0,this.waiting.add(o),s.has(n.cell)){n.cell.value=h.CALC,n.cell.state=0;continue}s.add(n.cell),e.push(n.cell)}}}finally{this.waiting.clear(),this.unwound.clear(),this.counted.clear(),this.work=[]}}compute(t){if(this.stack.length>=G){t.value=h.CALC,t.state=0;return}t.state=2,this.stack.push(t),this.computed+=1;const{node:e}=t.shared,s=R(e)?t.value:e?this.top(this.eval(e,T(t))):h.NAME;this.stack.pop(),this.unsettled.has(t)&&!this.inCycle.has(t)&&(this.inCycle.add(t),this.cycles.push([t])),this.settleResult(t,s),t.state=0,this.reports&&this.settled.add(t)}settleResult(t,e){const{sheet:s}=t,o=t.spill,n=t.array;if(s.clearSpill(t),t.array=void 0,e instanceof w){const r=ut(e);r.rows*r.cols<=1||this.spillOut(t,r)?(t.array=r,t.value=r.at(0,0)??0):t.value=h.SPILL}else t.value=e;this.inCycle.has(t)&&(s.clearSpill(t),t.value=h.CYCLE,t.array=void 0);const i=t.spill;(!lt(n,t.array)||!ft(o,i))&&(o&&this.moved.push({sheet:s,area:o}),i&&this.moved.push({sheet:s,area:i}))}top(t){const e=this.flat(t);return e===null?0:e}flat(t){return t instanceof p?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof A?h.VALUE:t instanceof E?h.CALC:t instanceof O?null:t}matrix(t){const e=t.bottom===L-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===C-1?Math.min(t.right,t.sheet.columnsUsed-1):t.right,o=Math.max(0,e-t.top+1),n=Math.max(0,s-t.left+1),i=new Array(o*n);for(let r=0;r<o;r++)for(let a=0;a<n;a++)i[r*n+a]=this.read(t.sheet,t.top+r,t.left+a);return new w(o,n,i)}each(t,e){const{sheet:s}=t,o=Math.min(t.right,s.columnsUsed-1);for(let n=t.left;n<=o;n++){const[i,r]=s.span(n,t.top,t.bottom);for(let a=i;a<=r;a++){const c=s.at(a,n);if(c===void 0){s.spillAt(a,n)&&e(this.read(s,a,n),a,n);continue}e(typeof c=="object"&&!(c instanceof b)?this.read(s,a,n):c,a,n)}}}evaluate(t,e){const{node:s}=t;return s?this.top(this.eval(s,{sheet:e,row:t.row,col:t.col,dr:0,dc:0})):h.NAME}evaluateAt(t,e,s,o){const{node:n}=t;return n?this.top(this.eval(n,{sheet:e,row:s,col:o,dr:s-t.row,dc:o-t.col})):h.NAME}bound(t,e,s){const o=t.abs?t.index:t.index+e;return o<0||o>=s?void 0:o}resolveRef(t,e){const s=t.sheet===void 0?e.sheet:this.wb.sheet(t.sheet);if(!s)return h.REF;let o=0,n=C-1,i=0,r=L-1;if(t.col){const a=this.bound(t.col,e.dc,C),c=this.bound(t.col2??t.col,e.dc,C);if(a===void 0||c===void 0)return h.REF;o=Math.min(a,c),n=Math.max(a,c)}if(t.row){const a=this.bound(t.row,e.dr,L),c=this.bound(t.row2??t.row,e.dr,L);if(a===void 0||c===void 0)return h.REF;i=Math.min(a,c),r=Math.max(a,c)}if(t.lastSheet!==void 0){const a=this.wb.sheet(t.lastSheet);if(!a)return h.REF;const[c,u]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new A(this.wb.sheets.slice(c,u+1).map(f=>new p(f,i,o,r,n)))}return new p(s,i,o,r,n)}structured(t,e){const s=t.table===void 0?e.sheet.tableAt(e.row,e.col):this.wb.table(t.table);return s?dt(s,t,e):t.table===void 0?h.REF:h.NAME}nameValue(t,e,s){const o=t.toLowerCase();if(s===void 0)for(let c=e.scope;c;c=c.parent){const u=c.names.get(o);if(u!==void 0)return u}const n=s===void 0?e.sheet:this.wb.sheet(s);if(!n)return h.REF;const i=this.wb.name(t,n);if(i){if(!i.formula)return i.value??null;const{node:c}=i.formula;if(!c)return h.NAME;if(this.namesAt.has(o))return h.CYCLE;this.namesAt.add(o);try{return this.eval(c,{sheet:e.sheet,row:0,col:0,dr:0,dc:0})}finally{this.namesAt.delete(o)}}const r=this.wb.table(t);if(r){const{top:c,left:u,bottom:f,right:g}=r.data;return new p(r.sheet,c,u,f,g)}const a=U.get(t.toUpperCase());return a&&a.kind!=="lazy"?pt(a,t,e):h.NAME}intersect(t,e){return!(t instanceof p)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new p(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new p(t.sheet,t.top,e.col,t.top,e.col):h.VALUE}unary(t,e){const s=this.flat(t);return s instanceof w?new w(s.rows,s.cols,s.data.map(e)):e(s)}operate(t,e,s){const o=this.flat(e),n=this.flat(s);if(o instanceof w||n instanceof w){const i=(r,a)=>F(t,r,a);return tt(o instanceof w?o:new w(1,1,[o]),n instanceof w?n:new w(1,1,[n]),i)}return F(t,o,n)}rangeOf(t,e){return t instanceof b?t:e instanceof b?e:!(t instanceof p)||!(e instanceof p)||t.sheet!==e.sheet?h.REF:new p(t.sheet,Math.min(t.top,e.top),Math.min(t.left,e.left),Math.max(t.bottom,e.bottom),Math.max(t.right,e.right))}intersection(t,e){if(t instanceof b)return t;if(e instanceof b)return e;if(!(t instanceof p)||!(e instanceof p))return h.REF;const s=Math.max(t.top,e.top),o=Math.max(t.left,e.left),n=Math.min(t.bottom,e.bottom),i=Math.min(t.right,e.right);return t.sheet!==e.sheet||s>n||o>i?h.NULL:new p(t.sheet,s,o,n,i)}union(t,e){if(t instanceof b)return t;if(e instanceof b)return e;const s=n=>n instanceof p?[n]:n instanceof A?n.parts:[],o=[...s(t),...s(e)];return o.length===0?h.VALUE:new A(o)}eval(t,e){switch(t.kind){case"number":case"string":case"boolean":return t.value;case"error":return b.of(t.code);case"missing":return D;case"ref":return this.resolveRef(t,e);case"structured":return this.structured(t,e);case"name":return this.nameValue(t.name,e,t.sheet);case"paren":return this.eval(t.arg,e);case"unary":{const s=this.eval(t.arg,e);return t.op==="@"?this.intersect(s,e):t.op==="+"?s:this.unary(s,o=>F("*",o,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,o=>F("/",o,100));if(s instanceof p&&s.single){const o=s.sheet.at(s.top,s.left);if(o instanceof k){this.read(s.sheet,s.top,s.left);const{spill:n}=o;if(n)return new p(s.sheet,n.top,n.left,n.bottom,n.right);if(o.array)return s}}return h.REF}case"binary":{const s=this.eval(t.left,e),o=this.eval(t.right,e);return t.op===":"?this.rangeOf(s,o):t.op===" "?this.intersection(s,o):t.op===","?this.union(s,o):this.operate(t.op,s,o)}case"array":return new w(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(o=>{const n=this.eval(o,e);return n instanceof w||n instanceof p||n instanceof A||n instanceof E||n instanceof O?h.VALUE:n})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof E?this.invoke(s,t.args,e):s instanceof b?s:h.VALUE}case"call":return this.call(t.name,t.args,e)}}invoke(t,e,s){return this.apply(t,e.map(o=>this.eval(o,s)))}apply(t,e){if(t.named!==void 0){const i=U.get(t.named);return!i||i.kind==="lazy"?h.NAME:e.length<i.min||e.length>i.max?h.VALUE:this.applyFunction(i,e,t.frame)}if(e.length>t.params.length)return h.VALUE;const s=t.params.length+1;if(this.lambdaSlots+s>at)return h.NUM;const o=new Map;t.params.forEach((i,r)=>{o.set(i,e[r]??D)}),this.lambdaSlots+=s;const n=this.stack.length;try{return this.eval(t.body,{...t.frame,scope:{names:o,...t.frame.scope?{parent:t.frame.scope}:{}}})}catch(i){if(!(i instanceof RangeError)||!i.message.includes("call stack"))throw i;for(const r of this.stack.splice(n))r.state=1;return h.NUM}finally{this.lambdaSlots-=s}}call(t,e,s){const o=U.get(t);if(!o){const n=this.nameValue(t,s);return n instanceof E?this.invoke(n,e,s):h.NAME}return e.length<o.min||e.length>o.max?h.VALUE:o.kind==="lazy"?o.call(e,{calc:this,frame:s}):this.applyFunction(o,e.map(n=>this.eval(n,s)),s)}applyFunction(t,e,s){const o={calc:this,frame:s};if(t.kind==="lazy")return h.VALUE;if(t.kind==="raw")return st(t.params,e.length)?this.over(e,i=>nt(t.params,i)==="value",i=>t.call(i,o)):h.VALUE;const n=t.errors===!0;return this.over(e,()=>!0,i=>{const r=i.map(a=>a instanceof O?void 0:a);if(!n){for(const a of r)if(a instanceof b)return a}return t.call(...r)})}over(t,e,s){const o=[],n=new Map;for(const[u,f]of t.entries()){if(!e(u)||f instanceof O){o.push(f);continue}const g=this.flat(f);g instanceof w&&n.set(u,g),o.push(g)}if(n.size===0)return s(o);const i=[...n.values()],r=Math.max(...i.map(u=>u.rows)),a=Math.max(...i.map(u=>u.cols));if(r===0||a===0)return h.CALC;const c=new Array(r*a);for(let u=0;u<r;u++)for(let f=0;f<a;f++){const g=o.map((S,x)=>{const y=n.get(x);if(!y)return S;const N=y.rows===1?0:u,_=y.cols===1?0:f;return N<y.rows&&_<y.cols?y.at(N,_):h.NA});c[u*a+f]=this.element(s(g))}return new w(r,a,c)}element(t){const e=this.flat(t);return e instanceof w?e.rows>0&&e.cols>0?e.at(0,0):h.CALC:e}scalar(t,e){if(t instanceof O)return null;if(t instanceof p){const s=this.intersect(t,e);return s instanceof p?this.read(s.sheet,s.top,s.left):h.VALUE}return t instanceof w?t.at(0,0):t instanceof A||t instanceof E?h.VALUE:t}recalculate(){if(this.replica)return this;const t=this.allCells().filter(e=>this.analysisOf(e.shared).volatile);return this.mark(t),this}build(){if(this.graphMade)return;this.graphMade=!0;const t=this.allCells();for(const e of t)this.analysisOf(e.shared).volatile&&this.volatiles.add(e);this.graph.build(t)}addEdges(t){this.analysisOf(t.shared).volatile&&this.volatiles.add(t),this.graph.add(t)}removeEdges(t){this.volatiles.delete(t),this.graph.remove(t)}readersIn(t,e){return this.graph.readersIn(t,e)}readers(t,e,s){return this.build(),this.graph.readers(t,e,s)}precedents(t){return this.readsOf(t)}mark(t){this.build();const e=new Set(t),s=[...t];for(;;){const o=s.pop();if(!o)break;for(const n of this.readers(o.sheet,o.row,o.col))e.has(n)||(e.add(n),s.push(n))}this.settle(e)}settle(t){for(const e of t)e.state=1,this.inCycle.delete(e);this.cycles=this.cycles.filter(e=>!e.some(s=>t.has(s))),this.run([...t])}set(t,e,s,o){const n=typeof o=="string"&&o.length>1&&o.startsWith("=")?new k(new j(o,e,s,{file:""}),t,e,s):void 0;this.change([{sheet:t,row:e,col:s,slot:n??o??void 0}])}prepare(){this.build()}change(t){if(this.replica){for(const{sheet:n,row:i,col:r,slot:a}of t){const c=n.at(i,r);c instanceof k&&n.clearSpill(c),n.replace(i,r,a),a instanceof k&&(a.state=0)}return}this.build();const e=new Set,s=[...this.unsettled];this.unsettled.clear();const o=new Set;for(const{sheet:n,row:i,col:r,slot:a}of t){const c=n.at(i,r);(c instanceof k&&R(c.shared.node)||a instanceof k&&R(a.shared.node))&&(this.tablesOf=void 0),c instanceof k&&(o.add(c),this.inCycle.delete(c),this.removeEdges(c),e.delete(c),c.spill&&this.moved.push({sheet:n,area:c.spill}),n.clearSpill(c));const u=n.spillAt(i,r);u&&s.push(u),s.push(...n.blockedBy({top:i,left:r,bottom:i,right:r})),n.replace(i,r,a),this.forget(n,{top:i,left:r,bottom:i,right:r}),a instanceof k&&(this.addEdges(a),e.add(a))}o.size>0&&(this.cycles=this.cycles.filter(n=>!n.some(i=>o.has(i))));for(const{sheet:n,row:i,col:r}of t)M(s,this.readers(n,i,r));for(M(s,this.volatiles);;){const n=s.pop();if(!n)break;e.has(n)||this.inTables&&R(n.shared.node)||(e.add(n),M(s,this.readers(n.sheet,n.row,n.col)))}this.settle(e)}setName(t,e){const s=this.wb.name(t);if(!s)throw new Error(`There is no name ${t}.`);s.value=e,delete s.formula;const o=t.toLowerCase();this.mark(this.allCells().filter(n=>this.analysisOf(n.shared).names.has(o)))}takeComputed(){const t=[];for(const e of this.settled)t.push({sheet:e.sheet.index,row:e.row,col:e.col,value:Q(e.value),...e.array?{array:J(e.array)}:{},...e.spill?{spill:e.spill}:{},...e.blocked?{blocked:e.blocked}:{}});return this.settled.clear(),{cells:t,cycles:this.cycles.map(e=>e.map(s=>({sheet:s.sheet.index,row:s.row,col:s.col}))),hidden:[...this.hiddenBy].map(([e,s])=>({sheet:e.index,rows:[...s]}))}}receive(t){for(const s of t.cells){const o=this.wb.sheets[s.sheet],n=o?.at(s.row,s.col);!o||!(n instanceof k)||(o.clearSpill(n),n.value=Y(s.value),n.array=s.array?Z(s.array):void 0,s.spill&&o.placeSpill(n,s.spill),s.blocked&&o.blockSpill(n,s.blocked),n.state=0)}this.cycles=t.cycles.flatMap(s=>{const o=s.flatMap(n=>{const i=this.wb.sheets[n.sheet]?.at(n.row,n.col);return i instanceof k?[i]:[]});return o.length>0?[o]:[]});const e=new Map(this.hiddenBy);this.hiddenBy.clear();for(const{sheet:s,rows:o}of t.hidden){const n=this.wb.sheets[s];if(!n)continue;const i=e.get(n),r=i?.size===o.length&&o.every(a=>i.has(a));this.hiddenBy.set(n,r?i:new Set(o))}for(const s of this.wb.sheets)s.tighten()}numberOf(t){return et(t)}}const ft=(l,t)=>l?.top===t?.top&&l?.left===t?.left&&l?.bottom===t?.bottom&&l?.right===t?.right;function ut(l){return l.data.some(t=>t===null)?new w(l.rows,l.cols,l.data.map(t=>t??0)):l}function R(l){return l?.kind==="call"&&l.name==="TABLE"}function T(l){return{sheet:l.sheet,row:l.row,col:l.col,dr:l.row-l.shared.row,dc:l.col-l.shared.col}}function dt(l,t,e){const{sheet:s}=l,o=l.area;let n=o.left,i=o.right;const[r,a]=t.columns;if(r!==void 0){const x=l.column(r),y=a===void 0?x:l.column(a);if(!x||!y)return h.REF;n=o.left+Math.min(x.index,y.index),i=o.left+Math.max(x.index,y.index)}if(t.thisRow)return e.sheet!==s||e.row<=o.top||e.row>o.bottom?h.VALUE:new p(s,e.row,n,e.row,i);const c=t.sections;if(c.includes("#Totals"))return h.REF;const u=c.includes("#All")||c.includes("#Headers"),f=c.length===0||c.includes("#All")||c.includes("#Data"),g=u?o.top:o.top+1,S=f?o.bottom:o.top;return new p(s,g,n,S,i)}function pt(l,t,e){return new E([],{kind:"call",name:l.name,written:t,args:[]},e,l.name)}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{CellError as l,ERR as g}from"../../values.js";import{compare as
|
|
2
|
+
import{CellError as l,ERR as g,errorOf as O}from"../../values.js";import{compare as C,numberOfText as A,sameNumber as b}from"../coerce.js";import{Missing as w,Reference as T}from"../types.js";import{indexKeyOf as k}from"../indexes.js";import{cellsOf as h,raw as p}from"./define.js";export function indexKey(e){if(typeof e=="number")return`n${String(Number(e.toPrecision(15)))}`;if(typeof e=="string")return`t${e.toUpperCase()}`;if(typeof e=="boolean")return e?"bT":"bF"}const y=new Map,V=1e4;export function wildcard(e){const n=y.get(e);if(n)return n;y.size>=V&&y.clear();const t=G(e);return y.set(e,t),t}function G(e){let n="";for(let r=0;r<e.length;r++){const o=e.charAt(r);o==="~"&&/[*?~]/u.test(e.charAt(r+1))?(r+=1,n+=e.charAt(r).replace(/[*?]/u,"\\$&")):o==="*"?n+="[\\s\\S]*":o==="?"?n+="[\\s\\S]":n+=o.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}const t=new RegExp(`^${n}$`,"iu");return r=>t.test(r)}export const hasWildcard=e=>/[*?~]/u.test(e);export function literalOf(e){let n="";for(let t=0;t<e.length;t++){const r=e.charAt(t);if(r==="~"&&/[*?~]/u.test(e.charAt(t+1)))t+=1,n+=e.charAt(t);else{if(r==="*"||r==="?")return;n+=r}}return n}function I(e){return n=>{if(typeof n=="number")return b(n,e);if(typeof n!="string")return!1;const t=A(n);return t!==void 0&&b(t,e)}}function P(e,n){const t=r=>e===">"?r>0:e===">="?r>=0:e==="<"?r<0:r<=0;return r=>typeof r==typeof n&&t(C(r,n))}export function criterion(e){if(e===null)return{test:I(0),key:"n0"};if(typeof e=="number")return{test:I(e),key:`n${String(Number(e.toPrecision(15)))}`};if(typeof e=="boolean")return{test:s=>s===e,key:e?"bT":"bF"};if(e instanceof l)return{test:s=>s===e};const n=/^(<>|>=|<=|=|>|<)/u.exec(e),t=n?.[1]??"=",r=n?e.slice(t.length):e,o=s=>s===null;if(r==="")return t==="="?{test:n?o:s=>s===null||s===""}:t==="<>"?{test:s=>s!==null}:{test:()=>!1};const i=A(r),c=/^(?:TRUE|FALSE)$/iu.test(r)?r.toUpperCase()==="TRUE":void 0,f=i??c;if(t==="="||t==="<>"){let s,u;if(i!==void 0)s=I(i),u=indexKey(i);else if(c!==void 0)s=d=>d===c,u=indexKey(c);else if(O(r)!==void 0)s=d=>d instanceof l&&d.code===r.toUpperCase();else if(hasWildcard(r)){const d=wildcard(r);s=m=>typeof m=="string"&&d(m)}else{const d=r.toUpperCase();s=m=>typeof m=="string"&&m.toUpperCase()===d,u=indexKey(r)}return t==="="?u===void 0?{test:s}:{test:s,key:u}:{test:d=>!s(d)}}return{test:P(t,f??r)}}function j(e,n){const t=e.reference;if(!(t?.cols!==1||e.rows<64))return n.calc.indexes.ofRepeated(k("crit",t),[t],()=>{const r=new Map,o=(i,c)=>{if(i===void 0)return;const f=r.get(i);f?f.push(c):r.set(i,[c])};for(let i=0;i<e.rows;i++){const c=e.at(i,0);if(o(indexKey(c),i),typeof c=="string"){const f=A(c);f!==void 0&&o(indexKey(f),i)}}return r})}function E(e,n){const t=n.calc.scalar(e,n.frame);return criterion(t)}function x(e,n){const t=[];for(let o=0;o+1<e.length;o+=2){const i=h(e[o],n);if(i instanceof l)return i;const c=E(e[o+1],n);if(c instanceof l)return c;t.push({cells:i,crit:c})}const r=t[0]?.cells;return r&&t.some(o=>!M(o.cells,r))?g.VALUE:t}const M=(e,n)=>e.cols===n.cols&&(e.reference?.rows??e.rows)===(n.reference?.rows??n.rows);function F(e,n,t){const r=e[0];if(!r)return;let o,i;for(const f of e){if(f.crit.key===void 0)continue;const s=j(f.cells,n);if(!s)continue;const u=s.get(f.crit.key)??[];(o===void 0||u.length<o.length)&&(o=u,i=f)}if(o!==void 0){const f=e.filter(s=>s!==i);for(const s of o)f.every(u=>u.crit.test(u.cells.at(s,0)))&&t(s,0);return}const c=Math.max(...e.map(f=>f.cells.rows));for(let f=0;f<c;f++)for(let s=0;s<r.cells.cols;s++)e.every(u=>u.crit.test(u.cells.at(f,s)))&&t(f,s)}function v(e,n){return e instanceof T?(e.rows-n.rows)*e.cols:0}function R(e,n,t,r){let o;return F(n,t,(i,c)=>{if(o)return;const f=e.at(i,c);f instanceof l?o=f:typeof f=="number"&&r(f)}),o}function L(e,n){const t=h(e[0],n);if(t instanceof l)return t;const r=e[2];return r===void 0?t:h(r,n)}const $=e=>(n,t)=>{const r=h(n[0],t);if(r instanceof l)return r;const o=E(n[1],t);if(o instanceof l)return o;const i=L(n,t);if(i instanceof l)return i;const c=[];return R(i,[{cells:r,crit:o}],t,s=>{c.push(s)})??e(c)},S=e=>(n,t)=>{const r=h(n[0],t);if(r instanceof l)return r;const o=x(n.slice(1),t);if(o instanceof l)return o;const i=o[0]?.cells;if(i&&!M(r,i))return g.VALUE;const c=[];return R(r,o,t,s=>{c.push(s)})??e(c)},a=(e,n)=>(t,r)=>{const o=[],i=[e];for(const u of t)if(u instanceof T)o.push(u),i.push(`r${u.key}`);else if(u instanceof w)i.push("-");else if(u instanceof l)i.push(`e${u.code}`);else if(u===null||typeof u!="object")i.push(`${typeof u}:${String(u)}`);else return n(t,r);if(o.length===0)return n(t,r);const c=i.join("\0"),f=r.calc.results.get(c);if(f!==void 0)return f;const s=n(t,r);return(s===null||typeof s!="object"||s instanceof l)&&r.calc.results.set(c,o,s),s},U=e=>e.reduce((n,t)=>n+t,0),N=e=>e.length===0?g.DIV0:U(e)/e.length,z=e=>e.length===0?0:e.reduce((n,t)=>t<n?t:n),K=e=>e.length===0?0:e.reduce((n,t)=>t>n?t:n);p("SUMIF","rv?r",a("SUMIF",$(U))),p("AVERAGEIF","rv?r",a("AVERAGEIF",$(N))),p("SUMIFS","r(rv)",a("SUMIFS",S(U))),p("AVERAGEIFS","r(rv)",a("AVERAGEIFS",S(N))),p("MINIFS","r(rv)",a("MINIFS",S(z))),p("MAXIFS","r(rv)",a("MAXIFS",S(K))),p("COUNTIF","rv",a("COUNTIF",(e,n)=>{const t=h(e[0],n);if(t instanceof l)return t;const r=E(e[1],n);if(r instanceof l)return r;let o=0;return F([{cells:t,crit:r}],n,()=>{o+=1}),o+(r.test(null)?v(e[0],t):0)})),p("COUNTIFS","(rv)",a("COUNTIFS",(e,n)=>{const t=x(e,n);if(t instanceof l)return t;let r=0;return F(t,n,()=>{r+=1}),r}));
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{parseDate as
|
|
2
|
+
import{parseDate as H,parseTime as X,parseValue as L}from"numfmt";import{CellError as c,ERR as u,dateOf as h,serialOf as p}from"../../values.js";import{toBool as g,toNumber as m,toText as T}from"../coerce.js";import{Missing as v}from"../types.js";import{eachRow as j,number as S,numbersOf as P,raw as U,scalar as l}from"./define.js";const I=2958465,y=e=>{const n=m(e);return n instanceof c?n:n<0||n>=I+1?u.NUM:Math.floor(n)},b=e=>e%4===0&&e%100!==0||e%400===0,A=(e,n)=>n===2?b(e)?29:28:[4,6,9,11].includes(n)?30:31,V=e=>e.month===2&&e.day===A(e.year,2);l("DATE",3,3,S((e=0,n=1,r=1)=>{const t=Math.trunc(e);if(t<0||t>9999)return u.NUM;const o=p(t<1900?t+1900:t,Math.trunc(n),Math.trunc(r));return Number.isFinite(o)&&o>=0&&o<=I?o:u.NUM})),l("TIME",3,3,S((e=0,n=0,r=0)=>{const t=Math.trunc(e)*3600+Math.trunc(n)*60+Math.trunc(r);return t<0?u.NUM:t%86400/86400})),l("YEAR",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).year}),l("MONTH",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).month}),l("DAY",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).day});const Y=e=>{const n=m(e);return n instanceof c?n:n<0?u.NUM:Math.round((n-Math.floor(n))*86400)%86400};l("HOUR",1,1,e=>{const n=Y(e);return n instanceof c?n:Math.floor(n/3600)}),l("MINUTE",1,1,e=>{const n=Y(e);return n instanceof c?n:Math.floor(n/60)%60}),l("SECOND",1,1,e=>{const n=Y(e);return n instanceof c?n:n%60}),l("DATEVALUE",1,1,e=>{const n=T(e);if(n instanceof c)return n;const r=H(n)??L(n);return typeof r?.value=="number"?Math.floor(r.value):u.VALUE}),l("TIMEVALUE",1,1,e=>{const n=T(e);if(n instanceof c)return n;const r=X(n)??L(n);return typeof r?.value!="number"?u.VALUE:r.value-Math.floor(r.value)});const D=e=>(e%7+6)%7;l("WEEKDAY",1,2,(e,n)=>{const r=y(e);if(r instanceof c)return r;const t=n==null?1:m(n);if(t instanceof c)return t;const o=D(r);return t===1?o+1:t===2?(o+6)%7+1:t===3?(o+6)%7:t>=11&&t<=17?(o+7-(t-10)%7)%7+1:u.NUM});const W=e=>{const n=(D(e)+6)%7,r=e-n+3,{year:t}=h(r);return Math.floor((r-p(t,1,1))/7)+1};l("ISOWEEKNUM",1,1,e=>{const n=y(e);return n instanceof c?n:W(n)}),l("WEEKNUM",1,2,(e,n)=>{const r=y(e);if(r instanceof c)return r;const t=n==null?1:m(n);if(t instanceof c)return t;if(t===21)return W(r);const o=t===1?0:t===2?1:t>=11&&t<=17?(t-10)%7:-1;if(o<0)return u.NUM;const a=p(h(r).year,1,1),s=(D(a)+7-o)%7;return Math.floor((r-a+s)/7)+1});const R=(e,n)=>{const{year:r,month:t,day:o}=h(e),a=r*12+t-1+n,s=Math.floor(a/12),f=a-s*12+1;return p(s,f,Math.min(o,A(s,f)))};l("EDATE",2,2,(e,n)=>{const r=y(e),t=m(n);if(r instanceof c)return r;if(t instanceof c)return t;const o=R(r,Math.trunc(t));return o<0?u.NUM:o}),l("EOMONTH",2,2,(e,n)=>{const r=y(e),t=m(n);if(r instanceof c)return r;if(t instanceof c)return t;const o=h(R(r,Math.trunc(t))),a=p(o.year,o.month,A(o.year,o.month));return a<0?u.NUM:a}),l("DAYS",2,2,(e,n)=>{const r=y(e),t=y(n);return r instanceof c?r:t instanceof c?t:r-t}),l("DATEDIF",3,3,(e,n,r)=>{const t=y(e),o=y(n),a=T(r);if(t instanceof c)return t;if(o instanceof c)return o;if(a instanceof c)return a;if(t>o)return u.NUM;const s=h(t),f=h(o),i=(f.year-s.year)*12+f.month-s.month-(f.day<s.day?1:0);switch(a.toUpperCase()){case"D":return o-t;case"M":return i;case"Y":return Math.floor(i/12);case"YM":return i%12;case"MD":{if(f.day>=s.day)return f.day-s.day;const d=f.month===1?12:f.month-1,N=f.month===1?f.year-1:f.year;return A(N,d)-s.day+f.day}case"YD":{const d=f.year-s.year-(i%12<0?1:0),N=Math.floor(i/12),M=R(t,N*12);return d>=0?o-M:u.NUM}default:return u.NUM}});function _(e,n,r){const t=h(e),o=h(n);let a=t.day,s=o.day,f=o.month,i=o.year;return r?(a=Math.min(a,30),s=Math.min(s,30)):(a===A(t.year,t.month)&&(a=30),s===31&&(a<30?(s=1,f+=1,f>12&&(f=1,i+=1)):s=30)),(i-t.year)*360+(f-t.month)*30+s-a}l("DAYS360",2,3,(e,n,r)=>{const t=y(e),o=y(n),a=r===void 0?!1:g(r);return t instanceof c?t:o instanceof c?o:a instanceof c?a:_(t,o,a)});export function yearFrac(e,n,r){const[t,o]=e<=n?[e,n]:[n,e],a=h(t),s=h(o),f=o-t;switch(r){case 0:{let i=a.day,d=s.day;return V(a)&&(V(s)&&(d=30),i=30),d===31&&i>=30&&(d=30),i===31&&(i=30),((s.year-a.year)*360+(s.month-a.month)*30+d-i)/360}case 2:return f/360;case 3:return f/365;case 4:return _(t,o,!0)/360;default:{if(a.year===s.year||s.year===a.year+1&&(a.month>s.month||a.month===s.month&&a.day>=s.day)){let M;if(a.year===s.year)M=b(a.year);else{const E=w=>b(w)?p(w,2,29):1/0;M=E(a.year)>=t&&E(a.year)<=o||E(s.year)>=t&&E(s.year)<=o}return f/(M?366:365)}const d=s.year-a.year+1,N=p(s.year+1,1,1)-p(a.year,1,1);return f/(N/d)}}}l("YEARFRAC",2,3,(e,n,r)=>{const t=y(e),o=y(n),a=r==null?0:m(r);if(t instanceof c)return t;if(o instanceof c)return o;if(a instanceof c)return a;const s=Math.trunc(a);return s<0||s>4?u.NUM:yearFrac(t,o,s)});function k(e){const n=(...t)=>[0,1,2,3,4,5,6].map(o=>t.includes(o));if(e==null)return n(0,6);if(typeof e=="string"){if(!/^[01]{7}$/u.test(e)||e==="1111111")return u.VALUE;const t=e.split("").map(o=>o==="1");return[t[6]??!1,...t.slice(0,6)]}const r=m(e);return r instanceof c?r:r>=1&&r<=7?n((r+5)%7,(r+6)%7):r>=11&&r<=17?n(r-11):u.NUM}function K(e,n){if(e===void 0||e instanceof v)return new Set;const r=P([e],n);return r instanceof c?r:new Set(r.map(t=>Math.floor(t)))}function $(e,n,r,t){const o=e<=n?1:-1;let a=0;for(let s=e;o>0?s<=n:s>=n;s+=o)r[D(s)]!==!0&&!t.has(s)&&(a+=o);return a}function q(e,n,r,t){if(r.every(Boolean))return u.VALUE;const o=n<0?-1:1;let a=e;for(let s=Math.abs(n);s>0;)a+=o,r[D(a)]!==!0&&!t.has(a)&&(s-=1);return a<0?u.NUM:a}const x=e=>(n,r)=>{const t=y(r.calc.scalar(n[0],r.frame)),o=y(r.calc.scalar(n[1],r.frame));if(t instanceof c)return t;if(o instanceof c)return o;const a=e?n[2]:void 0,s=k(a===void 0||a instanceof v?void 0:r.calc.scalar(a,r.frame));if(s instanceof c)return s;const f=K(n[e?3:2],r);return f instanceof c?f:$(t,o,s,f)},F=e=>(n,r)=>{const t=y(r.calc.scalar(n[0],r.frame)),o=m(r.calc.scalar(n[1],r.frame));if(t instanceof c)return t;if(o instanceof c)return o;const a=e?n[2]:void 0,s=k(a===void 0||a instanceof v?void 0:r.calc.scalar(a,r.frame));if(s instanceof c)return s;const f=K(n[e?3:2],r);return f instanceof c?f:q(t,Math.trunc(o),s,f)};U("NETWORKDAYS","vv?r",x(!1)),U("NETWORKDAYS.INTL","vv?vr",x(!0)),U("WORKDAY","vv?r",F(!1)),U("WORKDAY.INTL","vv?vr",F(!0)),U("NOW","",(e,n)=>n.calc.nowSerial(),{volatile:!0}),U("TODAY","",(e,n)=>Math.floor(n.calc.nowSerial()),{volatile:!0});function z(e,n,r){const t=r[0]??0;let o=0;return n.forEach((a,s)=>{o+=a/(1+e)**(((r[s]??t)-t)/365)}),o}const C=(e,n)=>{const r=[];return j([e],n,t=>r.push(t)),r},B=(e,n)=>{const r=C(e[0],n),t=C(e[1],n);if(r.length!==t.length)return u.NUM;const o=[],a=[];for(const[s,f]of r.entries()){const i=t[s]??null;if(f instanceof c)return f;if(i instanceof c)return i;if(typeof f=="number"){if(typeof i!="number")return u.VALUE;o.push(f),a.push(Math.floor(i))}}return o.length===0?u.NUM:{values:o,dates:a}};U("XNPV","vrr",(e,n)=>{const r=m(n.calc.scalar(e[0],n.frame));if(r instanceof c)return r;const t=B(e.slice(1),n);return t instanceof c?t:r<=0?u.NUM:z(r,t.values,t.dates)}),U("XIRR","rr?v",(e,n)=>{const r=B(e,n);if(r instanceof c)return r;const{values:t,dates:o}=r;if(!t.some(i=>i>0)||!t.some(i=>i<0))return u.NUM;const a=e[2]===void 0||e[2]instanceof v?.1:m(n.calc.scalar(e[2],n.frame));if(a instanceof c)return a;const s=o[0]??0;let f=a;for(let i=0;i<100;i++){let d=0,N=0;t.forEach((E,w)=>{const O=((o[w]??s)-s)/365;d+=E/(1+f)**O,N-=O*E/(1+f)**(O+1)});const M=f-d/N;if(!Number.isFinite(M))return u.NUM;if(Math.abs(M-f)<1e-10)return M;f=M<=-1?(f-1)/2:M}return u.NUM});
|