Skip to content

Commit 31c0f52

Browse files
authored
fix(default-app): fix deletion in default app and minor cleanups (commontoolsinc#1942)
* chore(runner): minor type cleanup related to wishes * fix(default-app): fix deletion + other cleanups * simplified types, etc. in wish.ts again * allow schema to be inferred
1 parent ab82d86 commit 31c0f52

File tree

3 files changed

+46
-62
lines changed

3 files changed

+46
-62
lines changed

packages/patterns/default-app.tsx

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ import BacklinksIndex, { type MentionableCharm } from "./backlinks-index.tsx";
2323
import ChatList from "./chatbot-list-view.tsx";
2424
import { calculator, readWebpage, searchWeb } from "./common-tools.tsx";
2525

26-
export type Charm = {
26+
type MinimalCharm = {
2727
[NAME]?: string;
28-
[UI]?: unknown;
29-
[key: string]: any;
3028
};
3129

3230
type CharmsListInput = void;
@@ -43,28 +41,27 @@ interface CharmsListOutput {
4341

4442
const visit = handler<
4543
Record<string, never>,
46-
{ charm: any }
44+
{ charm: Cell<MinimalCharm> }
4745
>((_, state) => {
4846
return navigateTo(state.charm);
4947
}, { proxy: true });
5048

5149
const removeCharm = handler<
5250
Record<string, never>,
5351
{
54-
charm: any;
55-
allCharms: Cell<any[]>;
52+
charm: Cell<MinimalCharm>;
53+
allCharms: Cell<MinimalCharm[]>;
5654
}
5755
>((_, state) => {
58-
const charmName = state.charm[NAME];
5956
const allCharmsValue = state.allCharms.get();
60-
const index = allCharmsValue.findIndex((c: any) => c[NAME] === charmName);
57+
const index = allCharmsValue.findIndex((c: any) => state.charm.equals(c));
6158

6259
if (index !== -1) {
6360
const charmListCopy = [...allCharmsValue];
64-
console.log("charmListCopy before", charmListCopy);
61+
console.log("charmListCopy before", charmListCopy.length);
6562
charmListCopy.splice(index, 1);
66-
console.log("charmListCopy after", charmListCopy);
67-
state.allCharms.set(charmListCopy);
63+
console.log("charmListCopy after", charmListCopy.length);
64+
state.allCharms.resolveAsCell().set(charmListCopy);
6865
}
6966
});
7067

@@ -238,37 +235,36 @@ export default recipe<CharmsListInput, CharmsListOutput>(
238235
</tr>
239236
</thead>
240237
<tbody>
241-
{derive(allCharms, (allCharms) =>
242-
allCharms.map((charm: any) => (
243-
<tr>
244-
<td>{charm[NAME] || "Untitled Charm"}</td>
245-
<td>
246-
<ct-hstack gap="2">
247-
<ct-button
248-
size="sm"
249-
onClick={visit({ charm })}
250-
>
251-
Visit
252-
</ct-button>
253-
<ct-button
254-
size="sm"
255-
variant="destructive"
256-
onClick={removeCharm({ charm, allCharms })}
257-
>
258-
Remove
259-
</ct-button>
260-
</ct-hstack>
261-
</td>
262-
</tr>
263-
)))}
238+
{allCharms.map((charm) => (
239+
<tr>
240+
<td>{charm?.[NAME] || "Untitled Charm"}</td>
241+
<td>
242+
<ct-hstack gap="2">
243+
<ct-button
244+
size="sm"
245+
onClick={visit({ charm })}
246+
>
247+
Visit
248+
</ct-button>
249+
<ct-button
250+
size="sm"
251+
variant="destructive"
252+
onClick={removeCharm({ charm, allCharms })}
253+
>
254+
Remove
255+
</ct-button>
256+
</ct-hstack>
257+
</td>
258+
</tr>
259+
))}
264260
</tbody>
265261
</ct-table>
266262
</ct-vstack>
267263
</ct-screen>
268264
),
269265
sidebarUI: (
270266
<div>
271-
{/* TODO(bf): why any? */}
267+
{/* TODO(bf): Remove once we fix types to not require ReactNode */}
272268
{omnibot.ui.attachmentsAndTools as any}
273269
{omnibot.ui.chatLog as any}
274270
</div>

packages/runner/src/builtins/wish.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ function resolveWishTarget(
2424
runtime: IRuntime,
2525
space: MemorySpace,
2626
tx: IExtendedStorageTransaction,
27-
): Cell<unknown> | undefined {
28-
const path = resolution.path ? [...resolution.path] : [];
27+
): Cell<any> | undefined {
2928
return runtime.getCellFromEntityId(
3029
space,
3130
resolution.entityId,
32-
path,
31+
resolution.path,
3332
undefined,
3433
tx,
3534
);
@@ -61,10 +60,6 @@ function parseWishTarget(target: string): ParsedWishTarget | undefined {
6160
return undefined;
6261
}
6362

64-
function segmentToPropertyKey(segment: string): PropertyKey {
65-
return /^\d+$/.test(segment) ? Number(segment) : segment;
66-
}
67-
6863
type WishContext = {
6964
runtime: IRuntime;
7065
tx: IExtendedStorageTransaction;
@@ -90,20 +85,14 @@ function getSpaceCell(ctx: WishContext): Cell<unknown> {
9085
}
9186

9287
function resolvePath(
93-
base: Cell<unknown>,
88+
base: Cell<any>,
9489
path: readonly string[],
95-
ctx: WishContext,
9690
): Cell<unknown> {
97-
let current = base.withTx(ctx.tx);
91+
let current = base;
9892
for (const segment of path) {
99-
const keyed = current.key(segmentToPropertyKey(segment) as never);
100-
try {
101-
current = keyed.resolveAsCell().withTx(ctx.tx);
102-
} catch {
103-
current = keyed.withTx(ctx.tx);
104-
}
93+
current = current.key(segment);
10594
}
106-
return current;
95+
return current.resolveAsCell();
10796
}
10897

10998
function resolveBase(
@@ -210,13 +199,12 @@ export function wish(
210199
const combinedPath = baseResolution.pathPrefix
211200
? [...baseResolution.pathPrefix, ...parsed.path]
212201
: parsed.path;
213-
const resolvedCell = resolvePath(baseResolution.cell, combinedPath, ctx);
214-
const resolvedWithTx = resolvedCell.withTx(tx);
202+
const resolvedCell = resolvePath(baseResolution.cell, combinedPath);
215203

216-
if (hasDefault && resolvedWithTx.get() === undefined) {
217-
sendResult(tx, defaultCell);
204+
if (resolvedCell.get() !== undefined) {
205+
sendResult(tx, resolvedCell);
218206
} else {
219-
sendResult(tx, resolvedWithTx);
207+
sendResult(tx, hasDefault ? defaultCell : undefined);
220208
}
221209
};
222210
}

packages/runner/src/runtime.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ export interface IRuntime {
132132
getCellFromEntityId<S extends JSONSchema = JSONSchema>(
133133
space: MemorySpace,
134134
entityId: EntityId,
135-
path: PropertyKey[],
135+
path: readonly PropertyKey[],
136136
schema: S,
137137
tx?: IExtendedStorageTransaction,
138138
): Cell<Schema<S>>;
139139
getCellFromEntityId<T>(
140140
space: MemorySpace,
141141
entityId: EntityId,
142-
path?: PropertyKey[],
142+
path?: readonly PropertyKey[],
143143
schema?: JSONSchema,
144144
tx?: IExtendedStorageTransaction,
145145
): Cell<T>;
@@ -490,21 +490,21 @@ export class Runtime implements IRuntime {
490490
getCellFromEntityId<T>(
491491
space: MemorySpace,
492492
entityId: EntityId | string,
493-
path?: PropertyKey[],
493+
path?: readonly PropertyKey[],
494494
schema?: JSONSchema,
495495
tx?: IExtendedStorageTransaction,
496496
): Cell<T>;
497497
getCellFromEntityId<S extends JSONSchema = JSONSchema>(
498498
space: MemorySpace,
499499
entityId: EntityId | string,
500-
path: PropertyKey[],
500+
path: readonly PropertyKey[],
501501
schema: S,
502502
tx?: IExtendedStorageTransaction,
503503
): Cell<Schema<S>>;
504504
getCellFromEntityId(
505505
space: MemorySpace,
506506
entityId: EntityId | string,
507-
path: PropertyKey[] = [],
507+
path: readonly PropertyKey[] = [],
508508
schema?: JSONSchema,
509509
tx?: IExtendedStorageTransaction,
510510
): Cell<any> {

0 commit comments

Comments
 (0)