Skip to content

Commit 11be622

Browse files
committed
more fixes
1 parent 4107ced commit 11be622

File tree

20 files changed

+122
-208
lines changed

20 files changed

+122
-208
lines changed

packages/lexical/src/tools/core/interfaces.ts

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,11 @@ import type {
1616
LexicalMetadata,
1717
RegisteredNodeInfo,
1818
} from './types';
19+
import type { ToolExecutionContext } from '@datalayer/jupyter-react';
1920

2021
// Re-export types for convenience
2122
export type { LexicalBlock, LexicalMetadata, RegisteredNodeInfo };
22-
23-
/**
24-
* Base tool execution context - common to all operations
25-
*/
26-
export interface BaseExecutionContext {
27-
/**
28-
* Datalayer SDK for API operations
29-
* (Required for notebook/runtime creation tools)
30-
*/
31-
sdk?: unknown; // DatalayerClient (avoid circular import)
32-
33-
/**
34-
* Authentication provider
35-
* (Required for authenticated operations)
36-
*/
37-
auth?: unknown; // AuthProvider (avoid circular import)
38-
39-
/**
40-
* Response format for tool results
41-
* - "json": Standard JSON format (default) - structured data
42-
* - "toon": TOON format - human/LLM-readable with compact syntax
43-
*/
44-
format?: 'json' | 'toon';
45-
46-
/**
47-
* Platform-agnostic command execution callback.
48-
* Allows operations to invoke platform-specific commands without direct dependencies.
49-
*
50-
* Operations call this with namespaced command names (e.g., "notebook.insertCell", "lexical.insertBlock").
51-
* The platform adapter is responsible for mapping these to the appropriate implementation.
52-
*
53-
* @param command - Command name with namespace prefix (e.g., "notebook.insertCell")
54-
* @param args - Command arguments (platform adapter handles any necessary conversions)
55-
* @returns Command result
56-
*/
57-
executeCommand?: <T = void>(command: string, args: unknown) => Promise<T>;
58-
59-
/**
60-
* Platform-specific extras (escape hatch for special cases)
61-
* Use this to pass additional context that doesn't fit in the standard interface.
62-
*/
63-
extras?: Record<string, unknown>;
64-
}
65-
66-
/**
67-
* Lexical-specific execution context
68-
* Used by all Lexical block operations (insertBlock, deleteBlock, readBlocks)
69-
*/
70-
export interface LexicalExecutionContext extends BaseExecutionContext {
71-
/**
72-
* Lexical document ID - universal identifier for both local and remote documents
73-
* - Local documents: Same as file URI (e.g., "file:///path/to/document.lexical")
74-
* - Remote documents: Datalayer document UID (e.g., "01KAJ42KE2XKM7NBNZV568KXQX")
75-
*/
76-
lexicalId: string;
77-
78-
/**
79-
* Executor for calling lexical store operations
80-
* Used in ag-ui environments to directly call store methods
81-
*/
82-
executor?: {
83-
execute<T = unknown>(operationName: string, args?: unknown): Promise<T>;
84-
};
85-
}
23+
export type { ToolExecutionContext };
8624

8725
/**
8826
* Core tool operation interface - platform agnostic.
@@ -107,11 +45,11 @@ export interface ToolOperation<TParams, TResult> {
10745
* ToolDefinition (schema), not here. Operations are pure implementation.
10846
*
10947
* @param params - Tool-specific parameters
110-
* @param context - Execution context (lexicalId, SDK, auth)
48+
* @param context - Execution context (documentId, executor, SDK, auth)
11149
* @returns Operation result
11250
* @throws Error if operation fails
11351
*/
114-
execute(params: TParams, context: LexicalExecutionContext): Promise<TResult>;
52+
execute(params: TParams, context: ToolExecutionContext): Promise<TResult>;
11553
}
11654

11755
/**

packages/lexical/src/tools/operations/deleteBlock.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
* @module tools/core/operations/deleteBlock
1111
*/
1212

13-
import type {
14-
ToolOperation,
15-
LexicalExecutionContext,
16-
} from '../core/interfaces';
13+
import type { ToolOperation, ToolExecutionContext } from '../core/interfaces';
1714
import type { LexicalBlock } from '../core/types';
1815
import { readAllBlocksOperation } from './readAllBlocks';
1916
import { validateWithZod } from '../core/zodUtils';
@@ -53,20 +50,20 @@ export interface DeleteBlockResult {
5350
*
5451
* IDs are stable identifiers so deletion order does not matter (unlike index-based deletion).
5552
*
56-
* Uses lexicalId as the universal identifier (matches Lexical component).
53+
* Uses documentId as the universal identifier (matches Lexical component).
5754
*
5855
* Architecture:
59-
* - Core layer: Works with lexicalId (platform-agnostic)
60-
* - VS Code adapter: Converts lexicalId → documentUri via registry
56+
* - Core layer: Works with documentId (platform-agnostic)
57+
* - VS Code adapter: Converts documentId → documentUri via registry
6158
* - Internal command: Uses documentUri to send message to webview
62-
* - Webview: Lexical component uses same lexicalId
59+
* - Webview: Lexical component uses same documentId
6360
*
6461
* @example
6562
* ```typescript
6663
* // Delete blocks
6764
* await deleteBlockOperation.execute(
6865
* { ids: ["block-123", "block-456", "block-789"] },
69-
* { lexicalId: "doc-1", executeCommand }
66+
* { documentId: "doc-1", executeCommand }
7067
* );
7168
* ```
7269
*/
@@ -78,7 +75,7 @@ export const deleteBlockOperation: ToolOperation<
7875

7976
async execute(
8077
params: unknown,
81-
context: LexicalExecutionContext,
78+
context: ToolExecutionContext,
8279
): Promise<DeleteBlockResult> {
8380
// Validate params using Zod
8481
const validatedParams = validateWithZod(
@@ -88,13 +85,13 @@ export const deleteBlockOperation: ToolOperation<
8885
);
8986

9087
const { ids } = validatedParams;
91-
const { lexicalId } = context;
88+
const { documentId } = context;
9289

9390
// Validate context
94-
if (!lexicalId) {
91+
if (!documentId) {
9592
throw new Error(
96-
'Lexical ID is required for deleteBlock operation. ' +
97-
'Ensure the tool execution context includes a valid lexicalId.',
93+
'Document ID is required for deleteBlock operation. ' +
94+
'Ensure the tool execution context includes a valid documentId.',
9895
);
9996
}
10097

packages/lexical/src/tools/operations/executeCode.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
* @module tools/operations/executeCode
1212
*/
1313

14-
import type {
15-
ToolOperation,
16-
LexicalExecutionContext,
17-
} from '../core/interfaces';
14+
import type { ToolOperation, ToolExecutionContext } from '../core/interfaces';
1815
import { validateWithZod } from '../core/zodUtils';
1916
import {
2017
executeCodeParamsSchema,
@@ -54,11 +51,11 @@ export const executeCodeOperation: ToolOperation<
5451

5552
async execute(
5653
params: unknown,
57-
context: LexicalExecutionContext,
54+
context: ToolExecutionContext,
5855
): Promise<ExecuteCodeResult> {
5956
console.log('[executeCodeOperation] Called with params:', params);
6057
console.log('[executeCodeOperation] Context:', {
61-
lexicalId: context.lexicalId,
58+
documentId: context.documentId,
6259
hasExecutor: !!context.executor,
6360
});
6461

@@ -71,12 +68,12 @@ export const executeCodeOperation: ToolOperation<
7168

7269
console.log('[executeCodeOperation] Validated params:', validatedParams);
7370

74-
const { lexicalId } = context;
71+
const { documentId } = context;
7572

76-
if (!lexicalId) {
73+
if (!documentId) {
7774
return {
7875
success: false,
79-
error: 'Lexical ID is required for this operation.',
76+
error: 'Document ID is required for this operation.',
8077
};
8178
}
8279

@@ -95,10 +92,10 @@ export const executeCodeOperation: ToolOperation<
9592
});
9693

9794
// Call executor (uses this.name for DRY principle)
98-
const result = await context.executor.execute<ExecuteCodeResult>(
95+
const result = (await context.executor.execute(
9996
this.name,
10097
validatedParams,
101-
);
98+
)) as ExecuteCodeResult;
10299

103100
console.log('[executeCodeOperation] Executor result:', result);
104101
return result;

packages/lexical/src/tools/operations/insertBlock.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
* @module tools/core/operations/insertBlock
1111
*/
1212

13-
import type {
14-
ToolOperation,
15-
LexicalExecutionContext,
16-
} from '../core/interfaces';
13+
import type { ToolOperation, ToolExecutionContext } from '../core/interfaces';
1714
import { validateWithZod } from '../core/zodUtils';
1815
import {
1916
insertBlockParamsSchema,
@@ -52,7 +49,7 @@ export const insertBlockOperation: ToolOperation<
5249

5350
async execute(
5451
params: unknown,
55-
context: LexicalExecutionContext,
52+
context: ToolExecutionContext,
5653
): Promise<InsertBlockResult> {
5754
// Validate params using Zod
5855
const validatedParams = validateWithZod(
@@ -62,13 +59,13 @@ export const insertBlockOperation: ToolOperation<
6259
);
6360

6461
const { afterId, type, source, properties } = validatedParams;
65-
const { lexicalId } = context;
62+
const { documentId } = context;
6663

6764
// Validate context
68-
if (!lexicalId) {
65+
if (!documentId) {
6966
throw new Error(
70-
'Lexical ID is required for insertBlock operation. ' +
71-
'Ensure the tool execution context includes a valid lexicalId.',
67+
'Document ID is required for insertBlock operation. ' +
68+
'Ensure the tool execution context includes a valid documentId.',
7269
);
7370
}
7471

packages/lexical/src/tools/operations/listAvailableBlocks.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
* @module tools/core/operations/listAvailableBlocks
1414
*/
1515

16-
import type {
17-
ToolOperation,
18-
LexicalExecutionContext,
19-
} from '../core/interfaces';
16+
import type { ToolOperation, ToolExecutionContext } from '../core/interfaces';
2017
import type { RegisteredNodeInfo } from '../core/types';
2118
import { validateWithZod } from '../core/zodUtils';
2219
import {
@@ -307,7 +304,7 @@ export const listAvailableBlocksOperation: ToolOperation<
307304

308305
async execute(
309306
params: unknown,
310-
context: LexicalExecutionContext,
307+
context: ToolExecutionContext,
311308
): Promise<ListAvailableBlocksResult> {
312309
// Validate params using Zod
313310
const validatedParams = validateWithZod(
@@ -317,25 +314,28 @@ export const listAvailableBlocksOperation: ToolOperation<
317314
);
318315

319316
const { category } = validatedParams;
320-
const { lexicalId } = context;
317+
const { documentId } = context;
321318

322319
try {
323320
let types = DEFAULT_BLOCK_TYPES;
324321

325322
// Try to get dynamically registered types from the document
326-
// This is optional - if no lexicalId or executor, we fall back to DEFAULT_BLOCK_TYPES
327-
if (lexicalId && context.executor) {
323+
// This is optional - if no documentId or executor, we fall back to DEFAULT_BLOCK_TYPES
324+
if (documentId && context.executor) {
328325
try {
329326
// Call executor to get registered nodes
330-
const registeredNodes = await context.executor.execute<
331-
RegisteredNodeInfo[]
332-
>('listAvailableBlocks', {});
327+
const registeredNodes = (await context.executor.execute(
328+
'listAvailableBlocks',
329+
{},
330+
)) as RegisteredNodeInfo[];
333331

334332
if (registeredNodes && registeredNodes.length > 0) {
335333
// Lexical nodes have types like: "paragraph", "heading", "code", "quote",
336334
// "list", "listitem", "table", "equation", "image", "youtube", "jupyter-cell"
337335
const registeredTypes = new Set(
338-
registeredNodes.map(node => node.type.toLowerCase()),
336+
registeredNodes.map((node: RegisteredNodeInfo) =>
337+
node.type.toLowerCase(),
338+
),
339339
);
340340

341341
// Filter to only schemas for registered node types

packages/lexical/src/tools/operations/readAllBlocks.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
* @module tools/core/operations/readAllBlocks
1111
*/
1212

13-
import type {
14-
ToolOperation,
15-
LexicalExecutionContext,
16-
} from '../core/interfaces';
13+
import type { ToolOperation, ToolExecutionContext } from '../core/interfaces';
1714
import type { LexicalBlock, BriefBlock } from '../core/types';
1815
import { validateWithZod } from '../core/zodUtils';
1916
import {
@@ -54,7 +51,7 @@ export const readAllBlocksOperation: ToolOperation<
5451

5552
async execute(
5653
params: unknown,
57-
context: LexicalExecutionContext,
54+
context: ToolExecutionContext,
5855
): Promise<ReadAllBlocksResult> {
5956
// Validate params using Zod
6057
const validatedParams = validateWithZod(
@@ -64,13 +61,13 @@ export const readAllBlocksOperation: ToolOperation<
6461
);
6562

6663
const { format = 'brief' } = validatedParams;
67-
const { lexicalId } = context;
64+
const { documentId } = context;
6865

6966
// Validate context
70-
if (!lexicalId) {
67+
if (!documentId) {
7168
throw new Error(
72-
'Lexical ID is required for readAllBlocks operation. ' +
73-
'Ensure the tool execution context includes a valid lexicalId.',
69+
'Document ID is required for readAllBlocks operation. ' +
70+
'Ensure the tool execution context includes a valid documentId.',
7471
);
7572
}
7673

@@ -84,9 +81,9 @@ export const readAllBlocksOperation: ToolOperation<
8481

8582
try {
8683
// Call executor with format parameter
87-
const blocks = await context.executor.execute<
88-
LexicalBlock[] | BriefBlock[]
89-
>(this.name, { format });
84+
const blocks = (await context.executor.execute(this.name, {
85+
format,
86+
})) as LexicalBlock[] | BriefBlock[];
9087

9188
return {
9289
success: true,

0 commit comments

Comments
 (0)