Skip to content

Commit fa677b9

Browse files
committed
fix(js): format TypeScript files using the faster biomejs formatter and Google style #1987
This speeds up formatting by 4-12x and fixes several formatting related issues whether they have to do with the commit message, source code, or license headers. ``` zsh❯ hyperfine -i "pnpm run format:biome" "pnpm run format:prettier" Benchmark 1: pnpm run format:biome Time (mean ± σ): 453.6 ms ± 5.6 ms [User: 679.3 ms, System: 92.5 ms] Range (min … max): 447.9 ms … 465.3 ms 10 runs Warning: Ignoring non-zero exit code. Benchmark 2: pnpm run format:prettier Time (mean ± σ): 5.715 s ± 0.285 s [User: 9.578 s, System: 0.739 s] Range (min … max): 5.506 s … 6.426 s 10 runs Summary pnpm run format:biome ran 12.60 ± 0.65 times faster than pnpm run format:prettier ``` A sample run follows: ``` zsh❯ pnpm run format > genkit@ format /Users/yesudeep/code/github.com/firebase/genkit > pnpm dlx @biomejs/biome format --write . && (tsx scripts/copyright.ts) Formatted 663 files in 86ms. Fixed 217 files. Checking copyright in sources... Updated copyright headers in 0 files ``` The configuration for the formatting is more or less based on the Google TypeScript formatting guidelines at: - [ ] https://google.github.io/styleguide/tsguide.html#string-literals - [ ] https://google.github.io/styleguide/jsguide.html - [ ] https://google.github.io/styleguide/tsguide.html#arrow-function-bodies - [ ] https://google.github.io/styleguide/tsguide.html#automatic-semicolon-insertion ISSUE: #1987 CHANGELOG: - [ ] Update `pnpm run format` to use biomejs - [ ] Update `pnpm run format:check` to use biomejs - [ ] Use `bin/add_license` to add license headers. - [ ] Exclude the `py/` and `go/` runtimes from the formatter. - [ ] Fix Import attributes cannot be used with a type-only import error - [ ] Update all the TypeScript files to use Google-style formatting. - [ ] Re-enable pre-commit formatting for TypeScript.
1 parent 8a40f5b commit fa677b9

File tree

354 files changed

+2086
-2353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

354 files changed

+2086
-2353
lines changed

bin/fmt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,9 @@ fi
3939
popd
4040

4141
# Format all TypeScript code.
42-
#
43-
# TODO: Re-enable once we have biome configured and enabled because that is
44-
# several times faster and compatible.
45-
#
46-
#pushd ${TOP_DIR}
47-
#pnpm run format
48-
#if [[ $? -ne 0 ]]; then
49-
# exit 1
50-
#fi
51-
#popd
42+
pushd ${TOP_DIR}
43+
pnpm run format
44+
if [[ $? -ne 0 ]]; then
45+
exit 1
46+
fi
47+
popd

biome.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
".nx/**",
3232
".trunk/**",
3333
"bazel-*/**",
34+
"go/**",
3435
"node_modules/**",
36+
"py/**",
3537
"third_party/**"
3638
]
3739
},

genkit-tools/cli/package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
"version": "1.0.4",
44
"description": "CLI for interacting with the Google Genkit AI framework",
55
"license": "Apache-2.0",
6-
"keywords": [
7-
"genkit",
8-
"ai",
9-
"genai",
10-
"generative-ai"
11-
],
6+
"keywords": ["genkit", "ai", "genai", "generative-ai"],
127
"author": "genkit",
138
"bin": {
149
"genkit": "dist/bin/genkit.js"

genkit-tools/cli/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export async function startCLI(): Promise<void> {
6464
// For now only record known command names, to avoid tools plugins causing
6565
// arbitrary text to get recorded. Once we launch tools plugins, we'll have
6666
// to give this more thought
67-
const commandNames = commands.map((c) => c.name());
67+
const commandNames = commands.map(c => c.name());
6868
let commandName: string;
6969
if (commandNames.includes(actionCommand.name())) {
7070
commandName = actionCommand.name();

genkit-tools/cli/src/commands/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const CONFIG_TAGS: Record<
2929
string,
3030
(value: string) => string | boolean | number
3131
> = {
32-
[ANALYTICS_OPT_OUT_CONFIG_TAG]: (value) => {
32+
[ANALYTICS_OPT_OUT_CONFIG_TAG]: value => {
3333
let o: boolean | undefined;
3434
try {
3535
o = JSON.parse(value);
@@ -46,7 +46,7 @@ config
4646
.description('set development environment configuration')
4747
.command('get')
4848
.argument('<tag>', `The config tag to get. One of [${readableTagsHint()}]`)
49-
.action((tag) => {
49+
.action(tag => {
5050
if (!CONFIG_TAGS[tag]) {
5151
logger.error(
5252
`Unknown config tag "${clc.bold(tag)}.\nValid options: ${readableTagsHint()}`

genkit-tools/cli/src/commands/eval-extract-data.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {
17+
import type {
1818
EvalInput,
1919
EvalInputDataset,
2020
TraceData,
@@ -45,23 +45,23 @@ export const evalExtractData = new Command('eval:extractData')
4545
.option('--maxRows <maxRows>', 'maximum number of rows', '100')
4646
.option('--label [label]', 'label flow run in this batch')
4747
.action(async (flowName: string, options: EvalDatasetOptions) => {
48-
await runWithManager(async (manager) => {
48+
await runWithManager(async manager => {
4949
const extractors = await getEvalExtractors(`/flow/${flowName}`);
5050

5151
logger.info(`Extracting trace data '/flow/${flowName}'...`);
5252
let dataset: EvalInputDataset = [];
5353
let continuationToken = undefined;
54-
while (dataset.length < parseInt(options.maxRows)) {
54+
while (dataset.length < Number.parseInt(options.maxRows)) {
5555
const response = await manager.listTraces({
56-
limit: parseInt(options.maxRows),
56+
limit: Number.parseInt(options.maxRows),
5757
continuationToken,
5858
});
5959
continuationToken = response.continuationToken;
6060
const traces = response.traces;
61-
let batch: EvalInput[] = traces
62-
.map((t) => {
61+
const batch: EvalInput[] = traces
62+
.map(t => {
6363
const rootSpan = Object.values(t.spans).find(
64-
(s) =>
64+
s =>
6565
s.attributes['genkit:metadata:subtype'] === 'flow' &&
6666
(!options.label ||
6767
s.attributes['batchRun'] === options.label) &&
@@ -73,7 +73,7 @@ export const evalExtractData = new Command('eval:extractData')
7373
return t;
7474
})
7575
.filter((t): t is TraceData => !!t)
76-
.map((trace) => {
76+
.map(trace => {
7777
return {
7878
testCaseId: generateTestCaseId(),
7979
input: extractors.input(trace),
@@ -82,14 +82,14 @@ export const evalExtractData = new Command('eval:extractData')
8282
// The trace (t) does not contain the traceId, so we have to pull it out of the
8383
// spans, de- dupe, and turn it back into an array.
8484
traceIds: Array.from(
85-
new Set(Object.values(trace.spans).map((span) => span.traceId))
85+
new Set(Object.values(trace.spans).map(span => span.traceId))
8686
),
8787
} as EvalInput;
8888
})
8989
.filter((result): result is EvalInput => !!result);
90-
batch.forEach((d) => dataset.push(d));
91-
if (dataset.length > parseInt(options.maxRows)) {
92-
dataset = dataset.splice(0, parseInt(options.maxRows));
90+
batch.forEach(d => dataset.push(d));
91+
if (dataset.length > Number.parseInt(options.maxRows)) {
92+
dataset = dataset.splice(0, Number.parseInt(options.maxRows));
9393
break;
9494
}
9595
if (!continuationToken) {

genkit-tools/cli/src/commands/eval-flow.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
*/
1616

1717
import {
18-
Action,
19-
Dataset,
20-
DatasetMetadata,
2118
DatasetSchema,
19+
type Action,
20+
type Dataset,
21+
type DatasetMetadata,
2222
} from '@genkit-ai/tools-common';
2323
import {
24-
EvalExporter,
2524
getAllEvaluatorActions,
2625
getDatasetStore,
2726
getExporterForString,
2827
getMatchingEvaluatorActions,
2928
runEvaluation,
3029
runInference,
30+
type EvalExporter,
3131
} from '@genkit-ai/tools-common/eval';
3232
import {
3333
confirmLlmUse,
@@ -83,7 +83,7 @@ export const evalFlow = new Command('eval:flow')
8383
.option('-f, --force', 'Automatically accept all interactive prompts')
8484
.action(
8585
async (flowName: string, data: string, options: EvalFlowRunCliOptions) => {
86-
await runWithManager(async (manager) => {
86+
await runWithManager(async manager => {
8787
const actionRef = `/flow/${flowName}`;
8888
if (!data && !options.input) {
8989
throw new Error(
@@ -102,7 +102,7 @@ export const evalFlow = new Command('eval:flow')
102102
} else {
103103
const evalActionKeys = options.evaluators
104104
.split(',')
105-
.map((k) => `/evaluator/${k}`);
105+
.map(k => `/evaluator/${k}`);
106106
evaluatorActions = await getMatchingEvaluatorActions(
107107
manager,
108108
evalActionKeys
@@ -116,7 +116,7 @@ export const evalFlow = new Command('eval:flow')
116116
);
117117
}
118118
logger.debug(
119-
`Using evaluators: ${evaluatorActions.map((action) => action.name).join(',')}`
119+
`Using evaluators: ${evaluatorActions.map(action => action.name).join(',')}`
120120
);
121121

122122
if (!options.force) {

genkit-tools/cli/src/commands/eval-run.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Action, EvalInputDataset } from '@genkit-ai/tools-common';
17+
import type { Action, EvalInputDataset } from '@genkit-ai/tools-common';
1818
import {
19-
EvalExporter,
2019
getAllEvaluatorActions,
2120
getExporterForString,
2221
getMatchingEvaluatorActions,
2322
runEvaluation,
23+
type EvalExporter,
2424
} from '@genkit-ai/tools-common/eval';
2525
import {
2626
confirmLlmUse,
@@ -59,7 +59,7 @@ export const evalRun = new Command('eval:run')
5959
)
6060
.option('--force', 'Automatically accept all interactive prompts')
6161
.action(async (dataset: string, options: EvalRunCliOptions) => {
62-
await runWithManager(async (manager) => {
62+
await runWithManager(async manager => {
6363
if (!dataset) {
6464
throw new Error(
6565
'No input data passed. Specify input data using [data] argument'
@@ -72,7 +72,7 @@ export const evalRun = new Command('eval:run')
7272
} else {
7373
const evalActionKeys = options.evaluators
7474
.split(',')
75-
.map((k) => `/evaluator/${k}`);
75+
.map(k => `/evaluator/${k}`);
7676
evaluatorActions = await getMatchingEvaluatorActions(
7777
manager,
7878
evalActionKeys
@@ -86,7 +86,7 @@ export const evalRun = new Command('eval:run')
8686
);
8787
}
8888
logger.info(
89-
`Using evaluators: ${evaluatorActions.map((action) => action.name).join(',')}`
89+
`Using evaluators: ${evaluatorActions.map(action => action.name).join(',')}`
9090
);
9191

9292
if (!options.force) {

genkit-tools/cli/src/commands/flow-batch-run.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ export const flowBatchRun = new Command('flow:batchRun')
4343
fileName: string,
4444
options: FlowBatchRunOptions
4545
) => {
46-
await runWithManager(async (manager) => {
46+
await runWithManager(async manager => {
4747
const inputData = JSON.parse(await readFile(fileName, 'utf8')) as any[];
4848
let input = inputData;
4949
if (inputData.length === 0) {
5050
throw new Error('batch input data must be a non-empty array');
5151
}
5252
if (Object.hasOwn(inputData[0], 'input')) {
5353
// If object has "input" field, use that instead.
54-
input = inputData.map((d) => d.input);
54+
input = inputData.map(d => d.input);
5555
}
5656

5757
const outputValues = [] as { input: any; output: any }[];
5858
for (const data of input) {
5959
logger.info(`Running '/flow/${flowName}'...`);
60-
let response = await manager.runAction({
60+
const response = await manager.runAction({
6161
key: `/flow/${flowName}`,
6262
input: data,
6363
context: options.context ? JSON.parse(options.context) : undefined,

genkit-tools/cli/src/commands/flow-run.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ export const flowRun = new Command('flow:run')
3939
'name of the output file to store the extracted data'
4040
)
4141
.action(async (flowName: string, data: string, options: FlowRunOptions) => {
42-
await runWithManager(async (manager) => {
42+
await runWithManager(async manager => {
4343
logger.info(`Running '/flow/${flowName}' (stream=${options.stream})...`);
44-
let result = (
44+
const result = (
4545
await manager.runAction(
4646
{
4747
key: `/flow/${flowName}`,
4848
input: data ? JSON.parse(data) : undefined,
4949
context: options.context ? JSON.parse(options.context) : undefined,
5050
},
5151
options.stream
52-
? (chunk) => console.log(JSON.stringify(chunk, undefined, ' '))
52+
? chunk => console.log(JSON.stringify(chunk, undefined, ' '))
5353
: undefined
5454
)
5555
).result;

0 commit comments

Comments
 (0)