Skip to content

Commit ec8aede

Browse files
authored
chore: runner cleanups, mostly renaming "cell" to "doc" when the code is about docs (#499)
* remove duplicate code from query-result-proxy * moved scheduler's utilities to scheduler * rename `cell` to `doc` where appropriate * renamed cell-map to doc-map * rename cell-map to doc-map * more renaming of `cell` to `doc`
1 parent 75d934a commit ec8aede

File tree

20 files changed

+482
-320
lines changed

20 files changed

+482
-320
lines changed

typescript/packages/common-cli/charm_test.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
/**
2-
* @file This file is Ellyse's exploration into the interactions between
3-
* charms, cells, and documents, and how they relate to common memory.
2+
* @file This file is Ellyse's exploration into the interactions between
3+
* charms, cells, and documents, and how they relate to common memory.
44
*
55
* I'm starting from the bottom (common memory) up and purposely calling
66
* APIs that would normally call into common memory.
7-
*
87
*/
9-
import { CharmManager, Charm } from "../common-charm/src/charm.ts";
8+
import { Charm, CharmManager } from "../common-charm/src/charm.ts";
109
import { Cell } from "../common-runner/src/cell.ts";
1110
import { DocImpl, getDoc } from "../common-runner/src/doc.ts";
12-
import { EntityId } from "../common-runner/src/cell-map.ts";
11+
import { EntityId } from "../common-runner/src/doc-map.ts";
1312
import { storage } from "../common-charm/src/storage.ts";
1413
import { getSpace, Space } from "../common-runner/src/space.ts";
1514

1615
const replica = "ellyse7";
1716
const TOOLSHED_API_URL = "https://toolshed.saga-castor.ts.net/";
1817

1918
// simple log function
20-
const log: <T>(s: T, prefix?: string) => void = (s, prefix?) =>
21-
console.log("-------------\n" + (prefix ? prefix : "") + ":\n" + JSON.stringify(s, null, 2));
19+
const log: <T>(s: T, prefix?: string) => void = (s, prefix?) =>
20+
console.log(
21+
"-------------\n" + (prefix ? prefix : "") + ":\n" +
22+
JSON.stringify(s, null, 2),
23+
);
2224

23-
function createCell(space: Space): Cell<Charm> {
25+
function createCell(space: Space): Cell<Charm> {
2426
const myCharm: Charm = {
2527
NAME: "mycharm",
2628
UI: "someui",
2729
"somekey": "some value",
2830
};
2931

30-
// make this a DocImpl<Charm> because we need to return a Cell<Charm> since
32+
// make this a DocImpl<Charm> because we need to return a Cell<Charm> since
3133
// that's what CharmManger.add() needs later on
32-
const myDoc: DocImpl<Charm> = getDoc<Charm>(myCharm, crypto.randomUUID(), space);
34+
const myDoc: DocImpl<Charm> = getDoc<Charm>(
35+
myCharm,
36+
crypto.randomUUID(),
37+
space,
38+
);
3339
return myDoc.asCell();
3440
}
3541

@@ -38,15 +44,15 @@ async function main() {
3844
const charmManager = new CharmManager(replica);
3945
log(charmManager, "charmManager");
4046

41-
// let's try to create a cell
47+
// let's try to create a cell
4248
const space: Space = getSpace(replica);
4349
const cell: Cell<Charm> = createCell(space);
4450
log(cell.get(), "cell value from Cell.get()");
45-
46-
// this feels like magic and wrong,
47-
// but we crash in the next CharmManager.add() if this isn't set
51+
52+
// this feels like magic and wrong,
53+
// but we crash in the next CharmManager.add() if this isn't set
4854
storage.setRemoteStorage(
49-
new URL(TOOLSHED_API_URL)
55+
new URL(TOOLSHED_API_URL),
5056
);
5157

5258
// let's add the cell to the charmManager

typescript/packages/common-cli/memory_test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MemorySpace } from "@commontools/memory";
22
import { RemoteStorageProvider } from "../common-charm/src/storage/remote.ts";
33
import { StorageProvider } from "../common-charm/src/storage/base.ts";
4-
import { EntityId } from "../common-runner/src/cell-map.ts";
4+
import { EntityId } from "../common-runner/src/doc-map.ts";
55

66
// some config stuff, hardcoded, ofcourse
77
const replica = "ellyse5";
@@ -79,10 +79,11 @@ async function main() {
7979
// now lets try to store a batch of values
8080
console.log("storing all entities");
8181
const result = await storageProvider.send(people_batch);
82-
if (result.ok)
82+
if (result.ok) {
8383
console.log("sent entities successfully");
84-
else
84+
} else {
8585
console.log("got error: " + JSON.stringify(result.error, null, 2));
86+
}
8687
}
8788

8889
main();

typescript/packages/common-runner/src/builtins/fetch-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { refer } from "merkle-reference";
99
*
1010
* Returns the fetched result as `result`. `pending` is true while a request is pending.
1111
*
12-
* @param url - A cell containing the URL to fetch data from.
12+
* @param url - A doc containing the URL to fetch data from.
1313
* @param mode - The mode to use for fetching data. Either `text` or `json`
1414
* default to `json` results.
15-
* @returns { pending: boolean, result: any, error: any } - As individual cells, representing `pending` state, final `result`, and any `error`.
15+
* @returns { pending: boolean, result: any, error: any } - As individual docs, representing `pending` state, final `result`, and any `error`.
1616
*/
1717
export function fetchData(
1818
inputsCell: DocImpl<{

typescript/packages/common-runner/src/builtins/llm.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ import { type ReactivityLog } from "../scheduler.ts";
1212
* Returns the complete result as `result` and the incremental result as
1313
* `partial`. `pending` is true while a request is pending.
1414
*
15-
* @param prompt - A cell to store the prompt message - if you only have a single message
15+
* @param prompt - A doc to store the prompt message - if you only have a single message
1616
* @param messages - list of messages to send to the LLM. - alternating user and assistant messages.
1717
* - if you end with an assistant message, the LLM will continue from there.
1818
* - if both prompt and messages are empty, no LLM call will be made,
1919
* result and partial will be undefined.
20-
* @param model - A cell to store the model to use.
21-
* @param system - A cell to store the system message.
22-
* @param stop - A cell to store (optional) stop sequence.
23-
* @param max_tokens - A cell to store the maximum number of tokens to generate.
20+
* @param model - A doc to store the model to use.
21+
* @param system - A doc to store the system message.
22+
* @param stop - A doc to store (optional) stop sequence.
23+
* @param max_tokens - A doc to store the maximum number of tokens to generate.
2424
*
2525
* @returns { pending: boolean, result?: string, partial?: string } - As individual
26-
* cells, representing `pending` state, final `result` and incrementally
26+
* docs, representing `pending` state, final `result` and incrementally
2727
* updating `partial` result.
2828
*/
2929
export function llm(
@@ -114,7 +114,7 @@ export function llm(
114114

115115
// Return if the same request is being made again, either concurrently (same
116116
// as previousCallHash) or when rehydrated from storage (same as the
117-
// contents of the requestHash cell).
117+
// contents of the requestHash doc).
118118
if (hash === previousCallHash || hash === requestHash.get()) return;
119119
previousCallHash = hash;
120120

typescript/packages/common-runner/src/builtins/map.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import { type AddCancel } from "../cancel.ts";
1313
* The goal is to keep the output array current without recomputing too much.
1414
*
1515
* Approach:
16-
* 1. Create a cell to store the result.
17-
* 2. Create a handler to update the result cell when the input cell changes.
18-
* 3. Create a handler to update the result cell when the op cell changes.
19-
* 4. For each value in the input cell, create a handler to update the result
20-
* cell when the value changes.
16+
* 1. Create a doc to store the result.
17+
* 2. Create a handler to update the result doc when the input doc changes.
18+
* 3. Create a handler to update the result doc when the op doc changes.
19+
* 4. For each value in the input doc, create a handler to update the result
20+
* doc when the value changes.
2121
*
2222
* TODO: Optimization depends on javascript objects and not lookslike objects.
2323
* We should make sure updates to arrays don't unnecessarily re-ify objects
2424
* and/or change the comparision here.
2525
*
26-
* @param list - A cell containing an array of values to map over.
26+
* @param list - A doc containing an array of values to map over.
2727
* @param op - A recipe to apply to each value.
28-
* @returns A cell containing the mapped values.
28+
* @returns A doc containing the mapped values.
2929
*/
3030
export function map(
3131
inputsCell: DocImpl<{
@@ -72,7 +72,7 @@ export function map(
7272
throw new Error("map currently only supports arrays");
7373
}
7474

75-
// // Hack to get to underlying array that lists cell references, etc.
75+
// Hack to get to underlying array that lists doc links, etc.
7676
const listRef = getDocLinkOrThrow(list);
7777

7878
// Same for op, but here it's so that the proxy doesn't follow the aliases
@@ -106,7 +106,7 @@ export function map(
106106
// TODO(seefeld): Have `run` return cancel, once we make resultCell required
107107
addCancel(cancels.get(resultCell));
108108

109-
// Send the result value to the result cell
109+
// Send the result value to the result doc
110110
result.setAtPath([initializedUpTo], { cell: resultCell, path: [] }, log);
111111

112112
initializedUpTo++;

typescript/packages/common-runner/src/builtins/stream-data.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { type ReactivityLog } from "../scheduler.ts";
1111
*
1212
* Returns the streamed result as `result`. `pending` is true while a request is pending.
1313
*
14-
* @param url - A cell containing the URL to stream data from.
15-
* @returns { pending: boolean, result: any, error: any } - As individual cells, representing `pending` state, streamed `result`, and any `error`.
14+
* @param url - A doc containing the URL to stream data from.
15+
* @returns { pending: boolean, result: any, error: any } - As individual docs, representing `pending` state, streamed `result`, and any `error`.
1616
*/
1717
export function streamData(
1818
inputsCell: DocImpl<{
@@ -53,7 +53,7 @@ export function streamData(
5353
result.sourceCell = parentDoc;
5454
error.sourceCell = parentDoc;
5555

56-
// Since we'll only write into the cells above, we only have to call this once
56+
// Since we'll only write into the docs above, we only have to call this once
5757
// here, instead of in the action.
5858
sendResult({ pending, result, error });
5959

typescript/packages/common-runner/src/cell.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
getDocLinkOrValue,
1414
type QueryResult,
1515
} from "./query-result-proxy.ts";
16-
import { followLinks, prepareForSaving, resolvePath } from "./utils.ts";
16+
import { prepareForSaving, resolveLinkToValue, resolvePath } from "./utils.ts";
1717
import { queueEvent, type ReactivityLog, subscribe } from "./scheduler.ts";
18-
import { type EntityId, getDocByEntityId, getEntityId } from "./cell-map.ts";
18+
import { type EntityId, getDocByEntityId, getEntityId } from "./doc-map.ts";
1919
import { type Cancel, isCancel, useCancelGroup } from "./cancel.ts";
2020
import { validateAndTransform } from "./schema.ts";
2121
import { type Schema } from "@commontools/builder";
@@ -203,7 +203,7 @@ export function createCell<T>(
203203
// Resolve the path to check whether it's a stream. We're not logging this right now.
204204
// The corner case where during it's lifetime this changes from non-stream to stream
205205
// or vice versa will not be detected.
206-
const ref = followLinks(resolvePath(doc, path));
206+
const ref = resolveLinkToValue(doc, path);
207207
if (isStreamAlias(ref.cell.getAtPath(ref.path))) {
208208
return createStreamCell(ref.cell, ref.path) as unknown as Cell<T>;
209209
} else return createRegularCell(doc, path, log, schema, rootSchema);

0 commit comments

Comments
 (0)