Skip to content

Commit 0fd399d

Browse files
authored
Automatically generate roo-code.d.ts (including type generation via zod-to-ts) (#2024)
1 parent e464c41 commit 0fd399d

33 files changed

+1782
-1271
lines changed

.changeset/sweet-bugs-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"roo-cline": patch
3+
---
4+
5+
Automatically generate .d.ts from zod schemas

.husky/pre-commit

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ else
1212
npx_cmd="npx"
1313
fi
1414

15-
"$npx_cmd" lint-staged
15+
npm run generate-types
16+
17+
if [ -n "$(git diff --name-only src/exports/roo-code.d.ts)" ]; then
18+
echo "Error: There are unstaged changes to roo-code.d.ts after running 'npm run generate-types'."
19+
echo "Please review and stage the changes before committing."
20+
exit 1
21+
fi
22+
23+
"$npx_cmd" lint-staged

e2e/src/suite/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type WaitForOptions = {
99

1010
export const waitFor = (
1111
condition: (() => Promise<boolean>) | (() => boolean),
12-
{ timeout = 30_000, interval = 250 }: WaitForOptions = {},
12+
{ timeout = 60_000, interval = 250 }: WaitForOptions = {},
1313
) => {
1414
let timeoutId: NodeJS.Timeout | undefined = undefined
1515

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@
351351
"clean:webview": "cd webview-ui && npm run clean",
352352
"clean:e2e": "cd e2e && npm run clean",
353353
"clean:benchmark": "cd benchmark && npm run clean",
354-
"update-contributors": "node scripts/update-contributors.js"
354+
"update-contributors": "node scripts/update-contributors.js",
355+
"generate-types": "tsx scripts/generate-types.mts"
355356
},
356357
"dependencies": {
357358
"@anthropic-ai/bedrock-sdk": "^0.10.2",

scripts/generate-types.mts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fs from "fs/promises"
2+
3+
import { zodToTs, createTypeAlias, printNode } from "zod-to-ts"
4+
import { $ } from "execa"
5+
6+
import { typeDefinitions } from "../src/schemas"
7+
8+
async function main() {
9+
const types: string[] = [
10+
"// This file is automatically generated by running `npm run generate-types`\n// Do not edit it directly.",
11+
]
12+
13+
for (const { schema, identifier } of typeDefinitions) {
14+
types.push(printNode(createTypeAlias(zodToTs(schema, identifier).node, identifier)))
15+
types.push(`export type { ${identifier} }`)
16+
}
17+
18+
await fs.writeFile("src/exports/types.ts", types.join("\n\n"))
19+
20+
await $`npx tsup src/exports/interface.ts --dts-only -d out`
21+
await fs.copyFile('out/interface.d.ts', 'src/exports/roo-code.d.ts')
22+
23+
await $`npx prettier --write src/exports/types.ts src/exports/roo-code.d.ts`
24+
}
25+
26+
main()

src/core/Cline.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import pWaitFor from "p-wait-for"
1111
import getFolderSize from "get-folder-size"
1212
import { serializeError } from "serialize-error"
1313
import * as vscode from "vscode"
14-
import { isPathOutsideWorkspace } from "../utils/pathUtils"
1514

16-
import { TokenUsage } from "../exports/roo-code"
15+
import { TokenUsage } from "../schemas"
1716
import { ApiHandler, buildApiHandler } from "../api"
1817
import { ApiStream } from "../api/transform/stream"
1918
import { DIFF_VIEW_URI_SCHEME, DiffViewProvider } from "../integrations/editor/DiffViewProvider"
@@ -65,6 +64,7 @@ import { defaultModeSlug, getModeBySlug, getFullModeDetails } from "../shared/mo
6564
import { EXPERIMENT_IDS, experiments as Experiments, ExperimentId } from "../shared/experiments"
6665
import { calculateApiCostAnthropic } from "../utils/cost"
6766
import { fileExistsAtPath } from "../utils/fs"
67+
import { isPathOutsideWorkspace } from "../utils/pathUtils"
6868
import { arePathsEqual, getReadablePath } from "../utils/path"
6969
import { parseMentions } from "./mentions"
7070
import { RooIgnoreController } from "./ignore/RooIgnoreController"

src/core/__tests__/mode-validator.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Mode, isToolAllowedForMode, getModeConfig, modes } from "../../shared/modes"
2-
import { validateToolUse } from "../mode-validator"
1+
import { isToolAllowedForMode, getModeConfig, modes, ModeConfig } from "../../shared/modes"
32
import { TOOL_GROUPS } from "../../shared/tool-groups"
3+
import { validateToolUse } from "../mode-validator"
4+
45
const [codeMode, architectMode, askMode] = modes.map((mode) => mode.slug)
56

67
describe("mode-validator", () => {
@@ -49,7 +50,7 @@ describe("mode-validator", () => {
4950

5051
describe("custom modes", () => {
5152
it("allows tools from custom mode configuration", () => {
52-
const customModes = [
53+
const customModes: ModeConfig[] = [
5354
{
5455
slug: "custom-mode",
5556
name: "Custom Mode",
@@ -65,7 +66,7 @@ describe("mode-validator", () => {
6566
})
6667

6768
it("allows custom mode to override built-in mode", () => {
68-
const customModes = [
69+
const customModes: ModeConfig[] = [
6970
{
7071
slug: codeMode,
7172
name: "Custom Code Mode",
@@ -80,7 +81,7 @@ describe("mode-validator", () => {
8081
})
8182

8283
it("respects tool requirements in custom modes", () => {
83-
const customModes = [
84+
const customModes: ModeConfig[] = [
8485
{
8586
slug: "custom-mode",
8687
name: "Custom Mode",

src/core/config/ContextProxy.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import * as vscode from "vscode"
22

3-
import { logger } from "../../utils/logging"
4-
import type {
5-
ProviderSettings,
6-
RooCodeSettings,
7-
RooCodeSettingsKey,
8-
GlobalStateKey,
9-
GlobalState,
10-
SecretStateKey,
11-
SecretState,
12-
GlobalSettings,
13-
} from "../../exports/roo-code"
143
import {
154
PROVIDER_SETTINGS_KEYS,
16-
GLOBAL_STATE_KEYS,
5+
ProviderSettings,
6+
providerSettingsSchema,
7+
GlobalSettings,
8+
globalSettingsSchema,
9+
RooCodeSettings,
1710
SECRET_STATE_KEYS,
11+
SecretState,
1812
isSecretStateKey,
19-
isPassThroughStateKey,
20-
globalSettingsSchema,
21-
providerSettingsSchema,
22-
} from "../../shared/globalState"
13+
GLOBAL_STATE_KEYS,
14+
GlobalState,
15+
} from "../../schemas"
16+
import { logger } from "../../utils/logging"
17+
18+
type GlobalStateKey = keyof GlobalState
19+
type SecretStateKey = keyof SecretState
20+
type RooCodeSettingsKey = keyof RooCodeSettings
21+
22+
const PASS_THROUGH_STATE_KEYS = ["taskHistory"]
23+
24+
export const isPassThroughStateKey = (key: string) => PASS_THROUGH_STATE_KEYS.includes(key)
2325

2426
const globalSettingsExportSchema = globalSettingsSchema.omit({
2527
taskHistory: true,

src/core/config/ProviderSettingsManager.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { ExtensionContext } from "vscode"
22
import { z } from "zod"
33

4-
import { providerSettingsSchema } from "../../shared/globalState"
4+
import { providerSettingsSchema, ApiConfigMeta } from "../../schemas"
55
import { Mode } from "../../shared/modes"
6-
import { ApiConfigMeta } from "../../shared/ExtensionMessage"
76

87
const providerSettingsWithIdSchema = providerSettingsSchema.extend({ id: z.string().optional() })
98

src/core/config/__tests__/ContextProxy.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
// npx jest src/core/config/__tests__/ContextProxy.test.ts
22

3-
import fs from "fs/promises"
4-
53
import * as vscode from "vscode"
64
import { ContextProxy } from "../ContextProxy"
75

8-
import { logger } from "../../../utils/logging"
9-
import { GLOBAL_STATE_KEYS, SECRET_STATE_KEYS } from "../../../shared/globalState"
6+
import { GLOBAL_STATE_KEYS, SECRET_STATE_KEYS } from "../../../schemas"
107

118
jest.mock("vscode", () => ({
129
Uri: {

0 commit comments

Comments
 (0)