@carbonstopper/agent-mcp 0.1.37 → 0.1.39

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.
@@ -0,0 +1,200 @@
1
+ import { Ajv } from "ajv";
2
+ export class ToolInputValidationError extends Error {
3
+ code;
4
+ path;
5
+ executionState = "not_dispatched";
6
+ constructor(code, path, message) {
7
+ super(message);
8
+ this.name = "ToolInputValidationError";
9
+ this.code = code;
10
+ this.path = path;
11
+ }
12
+ toPayload() {
13
+ return {
14
+ code: this.code,
15
+ path: this.path,
16
+ message: this.message,
17
+ executionState: this.executionState,
18
+ };
19
+ }
20
+ }
21
+ const libraryModes = [1, "1"];
22
+ const customModes = [2, "2"];
23
+ const factorMetadataFields = [
24
+ "factorName",
25
+ "factorType",
26
+ "factorSource",
27
+ "factorPublic",
28
+ ];
29
+ const ruleIndexes = {
30
+ factorModeConflict: 0,
31
+ libraryFactorRequired: 1,
32
+ customGasRequired: 2,
33
+ metadataRequiresFactorAction: 3,
34
+ autoMatchConflict: 4,
35
+ };
36
+ function requiredField(field) {
37
+ return { required: [field] };
38
+ }
39
+ function buildEnterpriseFactorContract(gasListField, allowAutoMatch) {
40
+ const allOf = [
41
+ {
42
+ not: {
43
+ required: ["factorId", "fdType"],
44
+ properties: {
45
+ fdType: { enum: customModes },
46
+ },
47
+ },
48
+ },
49
+ {
50
+ if: {
51
+ required: ["fdType"],
52
+ properties: {
53
+ fdType: { enum: libraryModes },
54
+ },
55
+ },
56
+ then: {
57
+ required: ["factorId"],
58
+ },
59
+ },
60
+ {
61
+ if: {
62
+ required: ["fdType"],
63
+ properties: {
64
+ fdType: { enum: customModes },
65
+ },
66
+ },
67
+ then: {
68
+ required: [gasListField],
69
+ },
70
+ },
71
+ {
72
+ if: {
73
+ anyOf: factorMetadataFields.map(requiredField),
74
+ },
75
+ then: {
76
+ anyOf: [
77
+ requiredField("factorId"),
78
+ requiredField(gasListField),
79
+ ],
80
+ },
81
+ },
82
+ ];
83
+ if (allowAutoMatch) {
84
+ allOf.push({
85
+ if: {
86
+ required: ["autoMatchFactor"],
87
+ properties: {
88
+ autoMatchFactor: { const: true },
89
+ },
90
+ },
91
+ then: {
92
+ not: {
93
+ anyOf: [
94
+ "factorId",
95
+ "fdType",
96
+ gasListField,
97
+ ...factorMetadataFields,
98
+ ].map(requiredField),
99
+ },
100
+ },
101
+ });
102
+ }
103
+ else {
104
+ allOf.push({
105
+ not: {
106
+ required: ["sourceId"],
107
+ },
108
+ });
109
+ }
110
+ allOf.push({
111
+ not: {
112
+ required: ["emissionQuantity"],
113
+ },
114
+ });
115
+ return { allOf };
116
+ }
117
+ export const enterpriseEmissionSaveFactorContract = buildEnterpriseFactorContract("factorGasList", false);
118
+ export const enterpriseEmissionUpdateFactorContract = buildEnterpriseFactorContract("gasList", true);
119
+ const ajv = new Ajv({
120
+ allErrors: true,
121
+ strict: false,
122
+ });
123
+ const saveFactorValidator = ajv.compile({
124
+ type: "object",
125
+ ...enterpriseEmissionSaveFactorContract,
126
+ });
127
+ const updateFactorValidator = ajv.compile({
128
+ type: "object",
129
+ ...enterpriseEmissionUpdateFactorContract,
130
+ });
131
+ export function validateEnterpriseFactorContract(source, sourcePath, gasListField, allowAutoMatch) {
132
+ const validator = allowAutoMatch
133
+ ? updateFactorValidator
134
+ : saveFactorValidator;
135
+ if (validator(source))
136
+ return;
137
+ throw contractValidationError(validator, source, sourcePath, gasListField, allowAutoMatch);
138
+ }
139
+ function contractValidationError(validator, source, sourcePath, gasListField, allowAutoMatch) {
140
+ const errors = validator.errors || [];
141
+ if (allowAutoMatch
142
+ && hasRuleError(errors, ruleIndexes.autoMatchConflict)) {
143
+ const conflictingFields = [
144
+ "factorId",
145
+ "fdType",
146
+ gasListField,
147
+ ...factorMetadataFields,
148
+ ];
149
+ const field = firstProvidedField(source, conflictingFields)
150
+ || "autoMatchFactor";
151
+ const isMetadataField = factorMetadataFields.includes(field);
152
+ const message = isMetadataField
153
+ ? `${field} must not be sent for automatic factor re-matching; omit factor metadata when autoMatchFactor=true`
154
+ : `autoMatchFactor cannot be combined with factorId, fdType, ${gasListField}, or factor metadata`;
155
+ return new ToolInputValidationError("FACTOR_AUTO_MATCH_CONFLICT", `${sourcePath}.${field}`, message);
156
+ }
157
+ if (hasRuleError(errors, ruleIndexes.factorModeConflict)) {
158
+ return new ToolInputValidationError("FACTOR_MODE_CONFLICT", `${sourcePath}.factorId`, "factorId cannot be combined with fdType=2; omit fdType or use fdType=1 for a library factor");
159
+ }
160
+ if (hasRuleError(errors, ruleIndexes.libraryFactorRequired)) {
161
+ return new ToolInputValidationError("FACTOR_ID_REQUIRED", `${sourcePath}.factorId`, "factorId is required when fdType=1");
162
+ }
163
+ if (hasRuleError(errors, ruleIndexes.customGasRequired)) {
164
+ return new ToolInputValidationError("CUSTOM_FACTOR_GAS_REQUIRED", `${sourcePath}.${gasListField}`, `${gasListField} must contain at least one gas item for a custom factor`);
165
+ }
166
+ if (hasRuleError(errors, ruleIndexes.metadataRequiresFactorAction)) {
167
+ const field = firstProvidedField(source, factorMetadataFields)
168
+ || factorMetadataFields[0];
169
+ const mode = allowAutoMatch
170
+ ? "factor preservation"
171
+ : "automatic factor matching";
172
+ return new ToolInputValidationError("FACTOR_METADATA_WITHOUT_ACTION", `${sourcePath}.${field}`, `${field} must not be sent for ${mode}; provide factorId or custom gas details for an explicit factor change`);
173
+ }
174
+ const firstError = errors[0];
175
+ return new ToolInputValidationError("TOOL_INPUT_SCHEMA_VIOLATION", appendJsonPointer(sourcePath, firstError?.instancePath || ""), firstError?.message || "Tool input violates the published factor contract");
176
+ }
177
+ function hasRuleError(errors, index) {
178
+ const prefix = `#/allOf/${index}/`;
179
+ return errors.some((error) => error.schemaPath.startsWith(prefix));
180
+ }
181
+ function firstProvidedField(source, fields) {
182
+ return fields.find((field) => Object.prototype.hasOwnProperty.call(source, field)
183
+ && source[field] !== undefined
184
+ && source[field] !== null);
185
+ }
186
+ function appendJsonPointer(basePath, pointer) {
187
+ if (!pointer)
188
+ return basePath;
189
+ let result = basePath;
190
+ for (const rawSegment of pointer.split("/").slice(1)) {
191
+ const segment = rawSegment
192
+ .replaceAll("~1", "/")
193
+ .replaceAll("~0", "~");
194
+ result += /^\d+$/.test(segment)
195
+ ? `[${segment}]`
196
+ : `.${segment}`;
197
+ }
198
+ return result;
199
+ }
200
+ //# sourceMappingURL=tool-validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-validation.js","sourceRoot":"","sources":["../src/tool-validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAA2C,MAAM,KAAK,CAAC;AAWnE,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACxC,IAAI,CAAS;IACb,IAAI,CAAS;IACb,cAAc,GAAG,gBAAyB,CAAC;IAEpD,YAAY,IAAY,EAAE,IAAY,EAAE,OAAe;QACrD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,SAAS;QACP,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,cAAc,EAAE,IAAI,CAAC,cAAc;SACpC,CAAC;IACJ,CAAC;CACF;AAED,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,GAAG,CAAU,CAAC;AACvC,MAAM,WAAW,GAAG,CAAC,CAAC,EAAE,GAAG,CAAU,CAAC;AACtC,MAAM,oBAAoB,GAAG;IAC3B,YAAY;IACZ,YAAY;IACZ,cAAc;IACd,cAAc;CACN,CAAC;AAEX,MAAM,WAAW,GAAG;IAClB,kBAAkB,EAAE,CAAC;IACrB,qBAAqB,EAAE,CAAC;IACxB,iBAAiB,EAAE,CAAC;IACpB,4BAA4B,EAAE,CA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C;AACJ,CAAC;AAED,SAAS,iBAAiB,CAAC,QAAgB,EAAE,OAAe;IAC1D,IAAI,CAAC,OAAO;QAAE,OAAO,QAAQ,CAAC;IAC9B,IAAI,MAAM,GAAG,QAAQ,CAAC;IACtB,KAAK,MAAM,UAAU,IAAI,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;QACrD,MAAM,OAAO,GAAG,UAAU;aACvB,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC;aACrB,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACzB,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;YAC7B,CAAC,CAAC,IAAI,OAAO,GAAG;YAChB,CAAC,CAAC,IAAI,OAAO,EAAE,CAAC;IACpB,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/dist/tools.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { z } from "zod";
2
2
  export declare const schemas: Record<string, z.ZodObject<any> | undefined>;
3
- export declare const exposedToolNames: readonly ["session_carbon_objects", "agent_user_memory_recall", "agent_user_memory_get", "agent_user_memory_upsert", "agent_user_memory_forget", "modeling_re_modeling", "modeling_detail", "modeling_account_detail", "modeling_account_update_structured", "modeling_emission_detail", "modeling_emission_add_structured", "modeling_emission_batch_add_structured", "modeling_emission_update_structured", "modeling_emission_batch_update_structured", "modeling_emission_delete_structured", "modeling_execute_structured", "report_generate", "report_regenerate", "report_detail", "factor_search", "unit_list", "company_org_subject_query", "company_org_subject_batch_add", "company_org_subject_batch_update", "enterprise_computation_exists", "enterprise_computation_create_or_update", "enterprise_computation_batch_add", "enterprise_computation_batch_delete", "enterprise_computation_update_base_info", "enterprise_computation_detail", "enterprise_emission_category_list", "enterprise_report_org_tree", "enterprise_report_generate", "enterprise_report_regenerate", "enterprise_report_file_record_retry", "enterprise_emission_batch_save", "enterprise_emission_batch_update", "enterprise_emission_batch_delete"];
3
+ export declare const exposedToolNames: readonly ["session_carbon_objects", "agent_user_memory_recall", "agent_user_memory_get", "agent_user_memory_upsert", "agent_user_memory_forget", "agent_project_memory_recall", "agent_project_memory_get", "agent_project_memory_upsert", "agent_project_memory_forget", "modeling_re_modeling", "modeling_detail", "modeling_account_detail", "modeling_account_update_structured", "modeling_emission_detail", "modeling_emission_add_structured", "modeling_emission_batch_add_structured", "modeling_emission_update_structured", "modeling_emission_batch_update_structured", "modeling_emission_delete_structured", "modeling_execute_structured", "report_generate", "report_regenerate", "report_detail", "factor_search", "unit_list", "company_org_subject_query", "company_org_subject_batch_add", "company_org_subject_batch_update", "enterprise_computation_exists", "enterprise_computation_create_or_update", "enterprise_computation_batch_add", "enterprise_computation_batch_delete", "enterprise_computation_update_base_info", "enterprise_computation_detail", "enterprise_computation_period_list", "enterprise_emission_category_list", "enterprise_report_org_tree", "enterprise_report_generate", "enterprise_report_regenerate", "enterprise_report_file_record_retry", "enterprise_emission_batch_save", "enterprise_emission_batch_update", "enterprise_emission_batch_delete"];
4
4
  export declare const descriptions: Record<string, string>;
5
- export type BodyKind = "agentSkill" | "sessionContext" | "userMemoryRecall" | "userMemoryGet" | "userMemoryUpsert" | "userMemoryForget" | "workspaceFile" | "multipartArtifact" | "factorSearch" | "singleEmissionAdd" | "enterpriseReport" | "enterpriseEmissionSave" | "enterpriseEmissionUpdate";
5
+ export type BodyKind = "agentSkill" | "sessionContext" | "userMemoryRecall" | "userMemoryGet" | "userMemoryUpsert" | "projectMemoryUpsert" | "userMemoryForget" | "workspaceFile" | "multipartArtifact" | "factorSearch" | "singleEmissionAdd" | "enterpriseReport" | "enterpriseEmissionSave" | "enterpriseEmissionUpdate" | "enterprisePeriodList";
6
6
  export type RoutePath = string | ((args: Record<string, unknown>) => string);
7
7
  export interface ToolRoute {
8
- method: "POST";
8
+ method: "GET" | "POST";
9
9
  path: RoutePath;
10
10
  bodyKind: BodyKind;
11
11
  timeoutSeconds?: number;