Skip to content

Commit a60df0b

Browse files
committed
refactor InvalidOrOk to Result
1 parent 8042603 commit a60df0b

File tree

15 files changed

+112
-112
lines changed

15 files changed

+112
-112
lines changed

packages/xl-ai/src/api/formats/base-tools/createAddBlocksTool.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "../../../prosemirror/agent.js";
99
import { updateToReplaceSteps } from "../../../prosemirror/changeset.js";
1010
import { RebaseTool } from "../../../prosemirror/rebaseTool.js";
11-
import { InvalidOrOk, streamTool } from "../../../streamTool/streamTool.js";
11+
import { Result, streamTool } from "../../../streamTool/streamTool.js";
1212
import { isEmptyParagraph } from "../../../util/emptyBlock.js";
1313
import { validateBlockArray } from "./util/validateBlockArray.js";
1414

@@ -38,7 +38,7 @@ export function createAddBlocksTool<T>(config: {
3838
validateBlock: (
3939
block: any,
4040
editor: BlockNoteEditor<any, any, any>,
41-
) => InvalidOrOk<T>;
41+
) => Result<T>;
4242
/**
4343
* The rebaseTool is used to get a projection of the document that
4444
* the JSON Tool Calls will be applied to. By using the rebaseTool we can
@@ -108,31 +108,31 @@ export function createAddBlocksTool<T>(config: {
108108
validate: (operation) => {
109109
if (operation.type !== "add") {
110110
return {
111-
result: "invalid",
112-
reason: "invalid operation type",
111+
ok: false,
112+
error: "invalid operation type",
113113
};
114114
}
115115

116116
if (operation.position !== "before" && operation.position !== "after") {
117117
return {
118-
result: "invalid",
119-
reason: "invalid position",
118+
ok: false,
119+
error: "invalid position",
120120
};
121121
}
122122

123123
if (!operation.referenceId || !operation.blocks) {
124124
return {
125-
result: "invalid",
126-
reason: "referenceId and blocks are required",
125+
ok: false,
126+
error: "referenceId and blocks are required",
127127
};
128128
}
129129

130130
let referenceId = operation.referenceId;
131131
if (options.idsSuffixed) {
132132
if (!referenceId?.endsWith("$")) {
133133
return {
134-
result: "invalid",
135-
reason: "referenceId must end with $",
134+
ok: false,
135+
error: "referenceId must end with $",
136136
};
137137
}
138138

@@ -143,8 +143,8 @@ export function createAddBlocksTool<T>(config: {
143143

144144
if (!block) {
145145
return {
146-
result: "invalid",
147-
reason: "referenceId not found",
146+
ok: false,
147+
error: "referenceId not found",
148148
};
149149
}
150150

@@ -153,12 +153,12 @@ export function createAddBlocksTool<T>(config: {
153153
(block) => config.validateBlock(block, editor),
154154
);
155155

156-
if (validatedBlocksResult.result === "invalid") {
156+
if (!validatedBlocksResult.ok) {
157157
return validatedBlocksResult;
158158
}
159159

160160
return {
161-
result: "ok",
161+
ok: true,
162162
value: {
163163
type: operation.type,
164164
referenceId,

packages/xl-ai/src/api/formats/base-tools/createUpdateBlockTool.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { updateToReplaceSteps } from "../../../prosemirror/changeset.js";
99
import { RebaseTool } from "../../../prosemirror/rebaseTool.js";
1010
import {
11-
InvalidOrOk,
11+
Result,
1212
StreamTool,
1313
streamTool,
1414
StreamToolCall,
@@ -47,7 +47,7 @@ export function createUpdateBlockTool<T>(config: {
4747
block: any,
4848
editor: BlockNoteEditor<any, any, any>,
4949
fallbackType?: string,
50-
) => InvalidOrOk<T>;
50+
) => Result<T>;
5151
/**
5252
* The rebaseTool is used to get a projection of the document that
5353
* the JSON Tool Calls will be applied to. By using the rebaseTool we can
@@ -112,24 +112,24 @@ export function createUpdateBlockTool<T>(config: {
112112
validate: (operation) => {
113113
if (operation.type !== "update") {
114114
return {
115-
result: "invalid",
116-
reason: "invalid operation type",
115+
ok: false,
116+
error: "invalid operation type",
117117
};
118118
}
119119

120120
if (!operation.id) {
121121
return {
122-
result: "invalid",
123-
reason: "id is required",
122+
ok: false,
123+
error: "id is required",
124124
};
125125
}
126126

127127
let id = operation.id;
128128
if (options.idsSuffixed) {
129129
if (!id?.endsWith("$")) {
130130
return {
131-
result: "invalid",
132-
reason: "id must end with $",
131+
ok: false,
132+
error: "id must end with $",
133133
};
134134
}
135135

@@ -138,8 +138,8 @@ export function createUpdateBlockTool<T>(config: {
138138

139139
if (!operation.block) {
140140
return {
141-
result: "invalid",
142-
reason: "block is required",
141+
ok: false,
142+
error: "block is required",
143143
};
144144
}
145145

@@ -149,19 +149,19 @@ export function createUpdateBlockTool<T>(config: {
149149
// eslint-disable-next-line no-console
150150
console.error("BLOCK NOT FOUND", id);
151151
return {
152-
result: "invalid",
153-
reason: "block not found",
152+
ok: false,
153+
error: "block not found",
154154
};
155155
}
156156

157157
const ret = config.validateBlock(operation.block, editor, block.type);
158158

159-
if (ret.result === "invalid") {
159+
if (!ret.ok) {
160160
return ret;
161161
}
162162

163163
return {
164-
result: "ok",
164+
ok: true,
165165
value: {
166166
type: operation.type,
167167
id,

packages/xl-ai/src/api/formats/base-tools/delete.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ export const deleteBlockTool = (
3333
validate: (operation) => {
3434
if (operation.type !== "delete") {
3535
return {
36-
result: "invalid",
37-
reason: "invalid operation type",
36+
ok: false,
37+
error: "invalid operation type",
3838
};
3939
}
4040

4141
if (!operation.id) {
4242
return {
43-
result: "invalid",
44-
reason: "id is required",
43+
ok: false,
44+
error: "id is required",
4545
};
4646
}
4747

4848
let id = operation.id;
4949
if (options.idsSuffixed) {
5050
if (!id?.endsWith("$")) {
5151
return {
52-
result: "invalid",
53-
reason: "id must end with $",
52+
ok: false,
53+
error: "id must end with $",
5454
};
5555
}
5656

@@ -61,13 +61,13 @@ export const deleteBlockTool = (
6161

6262
if (!block) {
6363
return {
64-
result: "invalid",
65-
reason: "block not found",
64+
ok: false,
65+
error: "block not found",
6666
};
6767
}
6868

6969
return {
70-
result: "ok",
70+
ok: true,
7171
value: {
7272
type: "delete", // TODO
7373
id,
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
import { DeepPartial } from "ai";
2-
import { InvalidOrOk } from "../../../../streamTool/streamTool.js";
2+
import { Result } from "../../../../streamTool/streamTool.js";
33

44
export function validateBlockArray<U>(
55
inputArray: DeepPartial<Array<U>>,
6-
validateItem: (item: DeepPartial<U>) => InvalidOrOk<U>,
7-
): InvalidOrOk<U[]> {
6+
validateItem: (item: DeepPartial<U>) => Result<U>,
7+
): Result<U[]> {
88
if (!inputArray || !Array.isArray(inputArray) || inputArray.length === 0) {
99
return {
10-
result: "invalid",
11-
reason: "blocks is required",
10+
ok: false,
11+
error: "blocks is required",
1212
};
1313
}
1414

1515
const validatedBlocks: U[] = [];
1616

1717
for (const item of inputArray) {
1818
const validationResult = validateItem(item as DeepPartial<U>);
19-
if (validationResult.result === "invalid") {
19+
if (!validationResult.ok) {
2020
return {
21-
result: "invalid",
22-
reason: `Invalid block: ${validationResult.reason}`,
21+
ok: false,
22+
error: `Invalid block: ${validationResult.error}`,
2323
};
2424
}
2525
validatedBlocks.push(validationResult.value);
2626
}
2727

2828
return {
29-
result: "ok",
29+
ok: true,
3030
value: validatedBlocks,
3131
};
3232
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { InvalidOrOk } from "../../../../streamTool/streamTool.js";
1+
import { Result } from "../../../../streamTool/streamTool.js";
22

3-
export function validateBlockFunction(block: any): InvalidOrOk<string> {
3+
export function validateBlockFunction(block: any): Result<string> {
44
if (typeof block !== "string") {
55
return {
6-
result: "invalid",
7-
reason: "block must be a string",
6+
ok: false,
7+
error: "block must be a string",
88
};
99
}
1010

1111
return {
12-
result: "ok",
12+
ok: true,
1313
value: block,
1414
};
1515
}

packages/xl-ai/src/api/formats/json/tools/validate.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
isLinkInlineContent,
55
isStyledTextInlineContent,
66
} from "@blocknote/core";
7-
import { InvalidOrOk } from "../../../../streamTool/streamTool.js";
7+
import { Result } from "../../../../streamTool/streamTool.js";
88

99
function validateInlineContent(content: any, editor: any): boolean {
1010
const inlineContentConfig =
@@ -38,15 +38,15 @@ export function validateBlockFunction(
3838
block: any,
3939
editor: BlockNoteEditor<any, any, any>,
4040
fallbackType?: string,
41-
): InvalidOrOk<PartialBlock<any, any, any>> {
41+
): Result<PartialBlock<any, any, any>> {
4242
const type = block.type || fallbackType;
4343
const blockConfig =
4444
editor.schema.blockSchema[type as keyof typeof editor.schema.blockSchema];
4545

4646
if (!blockConfig) {
4747
return {
48-
result: "invalid",
49-
reason: "block type not found in editor",
48+
ok: false,
49+
error: "block type not found in editor",
5050
};
5151
}
5252

@@ -59,31 +59,31 @@ export function validateBlockFunction(
5959
if (block.content) {
6060
// no content expected for this block
6161
return {
62-
result: "invalid",
63-
reason: "block content not expected for this block type",
62+
ok: false,
63+
error: "block content not expected for this block type",
6464
};
6565
}
6666
} else {
6767
if (!block.content) {
6868
// return false;
6969
return {
70-
result: "ok",
70+
ok: true,
7171
value: block,
7272
};
7373
}
7474

7575
if (!Array.isArray(block.content)) {
7676
// content expected for this block
7777
return {
78-
result: "invalid",
79-
reason: "block content must be an array",
78+
ok: false,
79+
error: "block content must be an array",
8080
};
8181
}
8282

8383
if (blockConfig.content === "table") {
8484
// no validation for table content (TODO)
8585
return {
86-
result: "ok",
86+
ok: true,
8787
value: block,
8888
};
8989
}
@@ -94,14 +94,14 @@ export function validateBlockFunction(
9494
})
9595
) {
9696
return {
97-
result: "invalid",
98-
reason: "block content must be an array of inline content",
97+
ok: false,
98+
error: "block content must be an array of inline content",
9999
};
100100
}
101101
}
102102
// TODO: validate props
103103
return {
104-
result: "ok",
104+
ok: true,
105105
value: block,
106106
};
107107
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { InvalidOrOk } from "../../../../streamTool/streamTool.js";
1+
import { Result } from "../../../../streamTool/streamTool.js";
22

3-
export function validateBlockFunction(block: any): InvalidOrOk<string> {
3+
export function validateBlockFunction(block: any): Result<string> {
44
if (typeof block !== "string") {
55
return {
6-
result: "invalid",
7-
reason: "block must be a string",
6+
ok: false,
7+
error: "block must be a string",
88
};
99
}
1010

1111
return {
12-
result: "ok",
12+
ok: true,
1313
value: block,
1414
};
1515
}

0 commit comments

Comments
 (0)