Repository navigation
Expand file tree
/
Copy pathclient-profile.ts
More file actions
205 lines (192 loc) · 8.04 KB
/
Copy pathclient-profile.ts
File metadata and controls
205 lines (192 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { z } from "zod";
import {
ApprovalWorkflow,
type ApprovalWorkflow as TApprovalWorkflow,
type WorkflowStep,
} from "@/lib/approval-workflow";
/**
* A CLIENT PROFILE, the config that makes the pipeline behave differently per
* customer, without touching code. This is what onboarding a client comes down
* to: not a custom build, but filling in this profile.
*
* The profile holds the matching tolerances and the APPROVAL WORKFLOW, the
* conditional DAG of who approves what under which conditions (lib/approval-
* workflow.ts). The onboarding discovery agent derives the workflow from the
* client's org chart, a human validates it, and the pipeline executes it. One
* profile in, the whole P2P flow adapts. See `.product/` for the architecture.
*
* The legacy two-tier `approvalPolicy` is still here as the SOURCE the default
* workflow is generated from (`workflowFromPolicy`), so a profile without an
* explicit workflow still routes exactly as before, the DAG is a strict
* superset of the old flat tiering.
*
* Defaults mirror the values the demo shipped with, so a profile is optional
* everywhere, pass one to customise, omit it for the standard behaviour.
*/
/** Tolerances below which a variance is rounding noise, not a real exception. */
export const MatchTolerances = z
.object({
/** Relative unit-price tolerance (e.g. 0.01 = 1%). */
pricePct: z.number().min(0),
/** Absolute per-line tolerance for the amount = qty × price check. */
lineAmountAbs: z.number().min(0),
/** Quantity tolerance (0 = exact; you either received the units or you didn't). */
qtyAbs: z.number().min(0),
})
.strict();
export type MatchTolerances = z.infer<typeof MatchTolerances>;
/** A single approval tier: the money/variance at or above which it kicks in. */
const ApprovalTier = z
.object({
amount: z.number().min(0),
variancePct: z.number().min(0),
})
.strict();
/** When an exception needs a human, which tier owns it (by money + variance). */
export const ApprovalPolicy = z
.object({
manager: ApprovalTier,
director: ApprovalTier,
})
.strict();
export type ApprovalPolicy = z.infer<typeof ApprovalPolicy>;
/** The full per-client config the pipeline runs under. */
export const ClientProfile = z
.object({
/** Stable client id (e.g. "severn-manufacturing"). */
id: z.string().trim().min(1),
/** Human label for the queue / UI. */
name: z.string().trim().min(1),
tolerances: MatchTolerances,
/** The legacy tier thresholds, the source the default workflow derives from. */
approvalPolicy: ApprovalPolicy,
/**
* The conditional approval DAG. Optional: when absent, the pipeline derives a
* behaviour-equivalent workflow from `approvalPolicy` via `workflowFromPolicy`,
* so an un-onboarded profile still routes exactly as the old flat tiering did.
* The onboarding agent fills this in for real.
*/
workflow: ApprovalWorkflow.optional(),
})
.strict();
export type ClientProfile = z.infer<typeof ClientProfile>;
/* ── Defaults, the values the demo shipped with ────────────────────────────
Used wherever a profile isn't supplied, so existing call sites (tests, the
sanity check) keep their exact behaviour. */
export const DEFAULT_TOLERANCES: MatchTolerances = {
pricePct: 0.01, // 1%, absorbs FX/rounding without hiding real overcharges
lineAmountAbs: 0.01,
qtyAbs: 0,
};
export const DEFAULT_APPROVAL_POLICY: ApprovalPolicy = {
manager: { amount: 1000, variancePct: 0.05 },
director: { amount: 10_000, variancePct: 0.1 },
};
/* ────────────────────────────────────────────────────────────────────────── *
* The default workflow: the standard two-tier policy as a DAG
* ────────────────────────────────────────────────────────────────────────── *
*
* What a profile without an explicit (agent-derived) workflow runs, the standard
* AP policy expressed as the same conditional DAG the onboarding template produces,
* so a run behaves consistently with or without discovery:
*
* • small clean invoice → both gates' conditions are false → straight to the post.
* • clean over the manager floor → manager gate fires (a human signs a material
* bill even when it matches cleanly).
* • exception → manager gate fires; the director gate additionally fires when
* the amount or variance clears the director threshold.
* • duplicate → handled as a pre-workflow control (blocked), never routed
* here, a duplicate is a control failure, not an approval.
*/
export const workflowFromPolicy = (
policy: ApprovalPolicy,
name = "Default approval workflow"
): TApprovalWorkflow => {
const exceptionCondition: WorkflowStep["when"] = {
kind: "leaf",
field: "verdict",
op: "==",
value: "exception",
};
// Manager sees an invoice when it's NOT trivial: any exception, or any clean bill
// over the manager floor. So a small clean invoice posts straight through, while
// anything material or flagged gets a human. Kept in step with the onboarding
// template (lib/onboarding.ts) so a run behaves the same with or without discovery.
const managerReview: WorkflowStep["when"] = {
kind: "any",
conditions: [
exceptionCondition,
{ kind: "leaf", field: "amount", op: ">", value: policy.manager.amount },
],
};
// Exception AND (amount >= director.amount OR variancePct >= director.variancePct).
const directorEscalation: WorkflowStep["when"] = {
kind: "all",
conditions: [
exceptionCondition,
{
kind: "any",
conditions: [
{
kind: "leaf",
field: "amount",
op: ">=",
value: policy.director.amount,
},
{
kind: "leaf",
field: "variancePct",
op: ">=",
value: policy.director.variancePct,
},
],
},
],
};
// The default (un-onboarded) workflow stands in for "the standard policy", so its
// gates are filled by ROLE, not a real person. We set approverName to the role so
// the canvas reads cleanly ("Manager review, Manager") instead of flagging every
// gate "unresolved", that warning is for an ONBOARDED workflow the agent couldn't
// resolve, not for this generic fallback. (Onboarding resolves these to people.)
const steps: WorkflowStep[] = [
{
id: "manager-review",
kind: "approval",
label: "Manager review",
when: managerReview,
approverTitle: "Manager",
approverName: "Manager",
// Linear escalation: Manager → Director → Post. NO direct Manager → Post edge:
// the graph shows a clean chain, and `resolvePath` short-circuits it per invoice
// (Director's condition false → Director is pruned and the edge rewires straight
// to Post). So a small bill resolves to Manager → Post at RUN time, without the
// base workflow carrying a confusing conditional fork.
next: ["director-review"],
},
{
id: "director-review",
kind: "approval",
label: "Director review",
when: directorEscalation,
approverTitle: "Director",
approverName: "Director",
next: ["post-netsuite"],
},
{
id: "post-netsuite",
kind: "integration",
label: "Post to NetSuite",
when: { kind: "always" },
integration: "netsuite",
next: [],
},
];
return { name, steps, roots: ["manager-review"] };
};
/** The workflow a profile runs: its explicit one, or the policy-derived default. */
export const workflowFor = (profile: {
approvalPolicy: ApprovalPolicy;
workflow?: TApprovalWorkflow;
}): TApprovalWorkflow => {
return profile.workflow ?? workflowFromPolicy(profile.approvalPolicy);
};