Skip to content

Commit ad4b646

Browse files
authored
chore: Update Writable->Mutable and apply it recursively for queries. (#1102)
1 parent 30eecf6 commit ad4b646

File tree

19 files changed

+107
-49
lines changed

19 files changed

+107
-49
lines changed

builder/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ export {
4444
isStatic,
4545
isStreamAlias,
4646
type JSONSchema,
47-
type JSONSchemaWritable,
47+
type JSONSchemaMutable,
4848
type JSONValue,
4949
markAsStatic,
5050
type Module,
5151
type ModuleFactory,
52+
type Mutable,
5253
NAME,
5354
type Node,
5455
type NodeFactory,
@@ -67,7 +68,6 @@ export {
6768
unsafe_originalRecipe,
6869
unsafe_parentRecipe,
6970
type UnsafeBinding,
70-
type Writable,
7171
} from "./types.ts";
7272
export { type Schema, schema } from "./schema-to-ts.ts";
7373

builder/src/recipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
isOpaqueRef,
55
isShadowRef,
66
type JSONSchema,
7-
type JSONSchemaWritable,
7+
type JSONSchemaMutable,
88
makeOpaqueRef,
99
type Module,
1010
type Node,
@@ -262,7 +262,7 @@ function factoryFromRecipe<T, R>(
262262

263263
if (typeof argumentSchemaArg === "string") {
264264
// Create a writable schema
265-
const writableSchema: JSONSchemaWritable = createJsonSchema(defaults, true);
265+
const writableSchema: JSONSchemaMutable = createJsonSchema(defaults, true);
266266
writableSchema.description = argumentSchemaArg;
267267

268268
delete (writableSchema.properties as any)?.[UI]; // TODO(seefeld): This should be a schema for views

builder/src/types.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isObj } from "@commontools/utils";
2+
import { Mutable } from "@commontools/utils/types";
23

34
export const ID: unique symbol = Symbol("ID, unique to the context");
45
export const ID_FIELD: unique symbol = Symbol(
@@ -150,13 +151,8 @@ export type JSONSchema = {
150151
readonly additionalProperties?: Readonly<JSONSchema> | boolean;
151152
};
152153

153-
export type Writable<T> = {
154-
-readonly [P in keyof T]: T[P] extends ReadonlyArray<infer U> ? Writable<U>[]
155-
: T[P] extends Readonly<infer U> ? Writable<U>
156-
: T[P];
157-
};
158-
159-
export type JSONSchemaWritable = Writable<JSONSchema>;
154+
export { type Mutable };
155+
export type JSONSchemaMutable = Mutable<JSONSchema>;
160156

161157
export type Alias = {
162158
$alias: {

builder/src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isShadowRef,
99
isStatic,
1010
type JSONSchema,
11-
type JSONSchemaWritable,
11+
type JSONSchemaMutable,
1212
type JSONValue,
1313
makeOpaqueRef,
1414
markAsStatic,
@@ -232,7 +232,7 @@ export function toJSONWithAliases(
232232
export function createJsonSchema(
233233
example: any,
234234
addDefaults = false,
235-
): JSONSchemaWritable {
235+
): JSONSchemaMutable {
236236
function analyzeType(value: any): JSONSchema {
237237
if (isCell(value)) {
238238
if (value.schema) {
@@ -259,7 +259,7 @@ export function createJsonSchema(
259259
}
260260

261261
const type = typeof value;
262-
const schema: JSONSchemaWritable = {};
262+
const schema: JSONSchemaMutable = {};
263263

264264
switch (type) {
265265
case "object":
@@ -314,7 +314,7 @@ export function createJsonSchema(
314314
return schema;
315315
}
316316

317-
return analyzeType(example);
317+
return analyzeType(example) as JSONSchemaMutable;
318318
}
319319

320320
export function moduleToJSON(module: Module) {

charm/src/iterate.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isObj } from "@commontools/utils";
99
import {
1010
createJsonSchema,
1111
JSONSchema,
12-
type Writable,
12+
JSONSchemaMutable,
1313
} from "@commontools/builder";
1414
import { Charm, CharmManager, charmSourceCellSchema } from "./manager.ts";
1515
import {
@@ -19,12 +19,12 @@ import {
1919
} from "./iframe/recipe.ts";
2020
import { buildPrompt, RESPONSE_PREFILL } from "./iframe/prompt.ts";
2121
import {
22+
applyDefaults,
2223
formatForm,
2324
generateCodeAndSchema,
2425
generateSpecAndSchema,
26+
type GenerationOptions,
2527
LLMClient,
26-
applyDefaults,
27-
type GenerationOptions
2828
} from "@commontools/llm";
2929
import { injectUserCode } from "./iframe/static.ts";
3030
import { IFrameRecipe, WorkflowForm } from "./index.ts";
@@ -236,7 +236,7 @@ export function scrub(data: any): any {
236236
(key) => [key, {}],
237237
),
238238
),
239-
} as JSONSchema;
239+
} satisfies JSONSchema;
240240
console.log("scrubbed generated schema", scrubbed);
241241
// Only if we found any properties, return the scrubbed schema
242242
return Object.keys(scrubbed).length > 0
@@ -302,7 +302,7 @@ async function singlePhaseCodeGeneration(
302302
...existingSchema,
303303
title: title || "missing",
304304
description,
305-
} as Writable<JSONSchema>;
305+
} as JSONSchemaMutable;
306306

307307
if (!schema.type) {
308308
schema.type = "object";
@@ -392,7 +392,7 @@ async function twoPhaseCodeGeneration(
392392
...existingSchema,
393393
title: title || "missing",
394394
description,
395-
} as Writable<JSONSchema>;
395+
} as JSONSchemaMutable;
396396

397397
if (!schema.type) {
398398
schema.type = "object";

jumble/integration/basic-flow.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import {
1212
addCharm,
1313
inspectCharm,
1414
login,
15-
Mutable,
1615
snapshot,
1716
waitForSelectorClick,
1817
waitForSelectorWithText,
1918
} from "@commontools/utils/integration";
2019
import * as path from "@std/path";
2120
import { ensureDirSync } from "@std/fs";
2221
import { join } from "@std/path";
22+
import { Mutable } from "@commontools/utils/types";
2323

2424
const TOOLSHED_API_URL = Deno.env.get("TOOLSHED_API_URL") ??
2525
"http://localhost:8000/";

jumble/src/recipes/smolIframe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const argumentSchema = {
1212
},
1313
},
1414
description: "SMOL Counter demo",
15-
} as JSONSchema;
15+
} satisfies JSONSchema;
1616

1717
export default recipe(argumentSchema, (data) => ({
1818
[NAME]: "smol iframe",

llm/src/prompts/code-and-schema-gen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { hydratePrompt, parseTagFromResponse } from "./prompting.ts";
22
import { LLMClient } from "../client.ts";
3-
import type { JSONSchema, JSONSchemaWritable } from "@commontools/builder";
3+
import type { JSONSchema, JSONSchemaMutable } from "@commontools/builder";
44
import { WorkflowForm } from "@commontools/charm";
55
import { systemMdConcise } from "../../../charm/src/iframe/static.ts";
66
import { formatForm } from "./spec-and-schema-gen.ts";
@@ -273,8 +273,8 @@ Based on this goal and the existing schema, please provide a title, description,
273273
const sourceCode = parseTagFromResponse(response.content, "source_code");
274274

275275
// If we have an existing schema, use it; otherwise parse the generated schema
276-
let resultSchema: JSONSchemaWritable;
277-
let argumentSchema: JSONSchemaWritable;
276+
let resultSchema: JSONSchemaMutable;
277+
let argumentSchema: JSONSchemaMutable;
278278

279279
try {
280280
const resultSchemaJson = parseTagFromResponse(

llm/src/prompts/spec-and-schema-gen.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { hydratePrompt, llmPrompt, parseTagFromResponse } from "./prompting.ts";
22
import { LLMClient } from "../client.ts";
33
import { DEFAULT_MODEL_NAME } from "../types.ts";
4-
import type { JSONSchema, JSONSchemaWritable } from "@commontools/builder";
4+
import type { JSONSchema, JSONSchemaMutable } from "@commontools/builder";
55
import { WorkflowForm } from "@commontools/charm";
66

77
// Prompt for generating schema and specification from a goal
@@ -271,8 +271,8 @@ Based on this goal and the existing schema, please provide a title, description,
271271
const plan = parseTagFromResponse(response.content, "plan");
272272

273273
// If we have an existing schema, use it; otherwise parse the generated schema
274-
let resultSchema: JSONSchemaWritable;
275-
let argumentSchema: JSONSchemaWritable;
274+
let resultSchema: JSONSchemaMutable;
275+
let argumentSchema: JSONSchemaMutable;
276276

277277
try {
278278
const resultSchemaJson = parseTagFromResponse(
@@ -393,8 +393,8 @@ Based on this goal and the existing schema, please provide a title, description,
393393
const plan = parseTagFromResponse(response.content, "plan");
394394

395395
// If we have an existing schema, use it; otherwise parse the generated schema
396-
let resultSchema: JSONSchemaWritable;
397-
let argumentSchema: JSONSchemaWritable;
396+
let resultSchema: JSONSchemaMutable;
397+
let argumentSchema: JSONSchemaMutable;
398398

399399
try {
400400
const resultSchemaJson = parseTagFromResponse(

recipes/bgAdmin.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
handler,
55
JSONSchema,
66
lift,
7+
Mutable,
78
NAME,
89
recipe,
910
Schema,
@@ -34,8 +35,8 @@ const BGCharmEntrySchema = {
3435
"lastRun",
3536
"status",
3637
],
37-
} as const as JSONSchema;
38-
type BGCharmEntry = Schema<typeof BGCharmEntrySchema>;
38+
} as const satisfies JSONSchema;
39+
type BGCharmEntry = Mutable<Schema<typeof BGCharmEntrySchema>>;
3940

4041
const BGCharmEntriesSchema = {
4142
type: "array",
@@ -49,7 +50,7 @@ const InputSchema = {
4950
properties: {
5051
charms: BGCharmEntriesSchema,
5152
},
52-
} as const as JSONSchema;
53+
} as const satisfies JSONSchema;
5354

5455
const ResultSchema = {
5556
type: "object",

0 commit comments

Comments
 (0)