Skip to content

Commit 9ff607d

Browse files
committed
refactor: Move cleanEmptyProperties to schema_generation and update references
1 parent f6e2084 commit 9ff607d

3 files changed

Lines changed: 36 additions & 35 deletions

File tree

src/tools/common/get_actor_output.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { InternalToolArgs, ToolEntry, ToolInputSchema } from '../../types.j
66
import { compileSchema } from '../../utils/ajv.js';
77
import { getValuesByDotKeys, parseCommaSeparatedList } from '../../utils/generic.js';
88
import { buildMCPResponse } from '../../utils/mcp.js';
9+
import { cleanEmptyProperties } from '../../utils/schema_generation.js';
910
import { datasetItemsOutputSchema } from '../structured_output_schemas.js';
1011

1112
/**
@@ -28,39 +29,6 @@ const getActorOutputArgs = z.object({
2829
.describe('Maximum number of items to return (default: 100).'),
2930
});
3031

31-
/**
32-
* Cleans empty properties (null, undefined, empty strings, empty arrays, empty objects) from an object.
33-
* Looser sibling: `cleanEmptyArrays` in `src/utils/schema_generation.ts` strips only empty arrays.
34-
* @param obj - The object to clean
35-
* @returns The cleaned object or undefined if the result is empty
36-
*/
37-
export function cleanEmptyProperties(obj: unknown): unknown {
38-
if (obj === null || obj === undefined || obj === '') {
39-
return undefined;
40-
}
41-
42-
if (typeof obj !== 'object') {
43-
return obj;
44-
}
45-
46-
if (Array.isArray(obj)) {
47-
const cleaned = obj
48-
.map((item) => cleanEmptyProperties(item))
49-
.filter((item) => item !== undefined);
50-
return cleaned.length > 0 ? cleaned : undefined;
51-
}
52-
53-
const cleaned: Record<string, unknown> = {};
54-
for (const [key, value] of Object.entries(obj)) {
55-
const cleanedValue = cleanEmptyProperties(value);
56-
if (cleanedValue !== undefined) {
57-
cleaned[key] = cleanedValue;
58-
}
59-
}
60-
61-
return Object.keys(cleaned).length > 0 ? cleaned : undefined;
62-
}
63-
6432
/**
6533
* This tool is used specifically for retrieving Actor output.
6634
* It is a simplified version of the get-dataset-items tool.

src/tools/core/actor_run_response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getWidgetConfig, WIDGET_URIS } from '../../resources/widgets.js';
88
import { logHttpError } from '../../utils/logging.js';
99
import { buildMCPResponse } from '../../utils/mcp.js';
1010
import { formatRunStatusMessage, type ProgressTracker, TERMINAL_RUN_STATUSES } from '../../utils/progress.js';
11-
import { cleanEmptyProperties } from '../common/get_actor_output.js';
11+
import { cleanEmptyProperties } from '../../utils/schema_generation.js';
1212

1313
/** Cap on `storages.keyValueStores.default.keys` array length. */
1414
const KV_KEYS_LIMIT = 50;

src/utils/schema_generation.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type SchemaGenerationOptions = {
2424
/**
2525
* Local counterpart to the dataset API's `clean=true` — empty arrays carry no schema info.
2626
* Strips only empty arrays; keeps null / '' / empty objects so schema inference still sees those fields.
27-
* Stricter sibling: `cleanEmptyProperties` in `src/tools/common/get_actor_output.ts` also strips nullish and empty strings.
27+
* Stricter sibling: {@link cleanEmptyProperties} also strips nullish and empty strings.
2828
*/
2929
export function cleanEmptyArrays(obj: unknown): unknown {
3030
if (Array.isArray(obj)) {
@@ -43,6 +43,39 @@ export function cleanEmptyArrays(obj: unknown): unknown {
4343
}, {} as Record<string, unknown>);
4444
}
4545

46+
/**
47+
* Cleans empty properties (null, undefined, empty strings, empty arrays, empty objects) from an object.
48+
* Looser sibling: {@link cleanEmptyArrays} strips only empty arrays.
49+
* @param obj - The object to clean
50+
* @returns The cleaned object or undefined if the result is empty
51+
*/
52+
export function cleanEmptyProperties(obj: unknown): unknown {
53+
if (obj === null || obj === undefined || obj === '') {
54+
return undefined;
55+
}
56+
57+
if (typeof obj !== 'object') {
58+
return obj;
59+
}
60+
61+
if (Array.isArray(obj)) {
62+
const cleaned = obj
63+
.map((item) => cleanEmptyProperties(item))
64+
.filter((item) => item !== undefined);
65+
return cleaned.length > 0 ? cleaned : undefined;
66+
}
67+
68+
const cleaned: Record<string, unknown> = {};
69+
for (const [key, value] of Object.entries(obj)) {
70+
const cleanedValue = cleanEmptyProperties(value);
71+
if (cleanedValue !== undefined) {
72+
cleaned[key] = cleanedValue;
73+
}
74+
}
75+
76+
return Object.keys(cleaned).length > 0 ? cleaned : undefined;
77+
}
78+
4679
const FORMAT_DETECTORS: [string, (s: string) => boolean][] = [
4780
['date-time', (s) => /^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(\.\d+)?([Zz]|[+-]\d{2}:\d{2})?$/.test(s)],
4881
['date', (s) => /^\d{4}-\d{2}-\d{2}$/.test(s)],

0 commit comments

Comments
 (0)