@proteos/component-sdk 0.87.0 → 0.89.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/protocol.cjs CHANGED
@@ -1,24 +1,24 @@
1
1
  'use strict';
2
2
 
3
- var chunk7LS4T4F2_cjs = require('./chunk-7LS4T4F2.cjs');
3
+ var chunkJKEB3AIC_cjs = require('./chunk-JKEB3AIC.cjs');
4
4
 
5
5
 
6
6
 
7
7
  Object.defineProperty(exports, "COMPONENT_OVERLAY_Z_INDEX", {
8
8
  enumerable: true,
9
- get: function () { return chunk7LS4T4F2_cjs.COMPONENT_OVERLAY_Z_INDEX; }
9
+ get: function () { return chunkJKEB3AIC_cjs.COMPONENT_OVERLAY_Z_INDEX; }
10
10
  });
11
11
  Object.defineProperty(exports, "MESSAGE_SOURCE", {
12
12
  enumerable: true,
13
- get: function () { return chunk7LS4T4F2_cjs.MESSAGE_SOURCE; }
13
+ get: function () { return chunkJKEB3AIC_cjs.MESSAGE_SOURCE; }
14
14
  });
15
15
  Object.defineProperty(exports, "isHostMessage", {
16
16
  enumerable: true,
17
- get: function () { return chunk7LS4T4F2_cjs.isHostMessage; }
17
+ get: function () { return chunkJKEB3AIC_cjs.isHostMessage; }
18
18
  });
19
19
  Object.defineProperty(exports, "isRuntimeMessage", {
20
20
  enumerable: true,
21
- get: function () { return chunk7LS4T4F2_cjs.isRuntimeMessage; }
21
+ get: function () { return chunkJKEB3AIC_cjs.isRuntimeMessage; }
22
22
  });
23
23
  //# sourceMappingURL=protocol.cjs.map
24
24
  //# sourceMappingURL=protocol.cjs.map
@@ -214,7 +214,35 @@ interface RecordDraftReviewClosedMessage {
214
214
  requestId: string;
215
215
  outcome?: RecordDraftReviewOutcome;
216
216
  }
217
- type HostMessage = InitMessage | AgentTabsAvailabilityMessage | AuthRefreshMessage | DataInvalidateMessage | OverlayRectMessage | ModalClosedMessage | PageActionSettledMessage | RecordDraftReviewClosedMessage;
217
+ /** What a host knowledge-editor modal reports once it closes, when it saved
218
+ * (or created) the node at least once. */
219
+ interface KnowledgeEditorModalResult {
220
+ /** The node edited — or created, see `created`. */
221
+ nodeId: string;
222
+ /** Its title as last saved. */
223
+ title: string;
224
+ /** True when the modal created the node (no `nodeId` was passed). */
225
+ created: boolean;
226
+ }
227
+ /** Resolution of an OPEN_KNOWLEDGE_EDITOR request: the modal closed. `result`
228
+ * is absent when nothing was saved — dismissed (an unsaved draft stays on
229
+ * the device), or the host could not open it (a public page, a preview, a
230
+ * modal from this frame already open). */
231
+ interface KnowledgeEditorClosedMessage {
232
+ type: 'KNOWLEDGE_EDITOR_CLOSED';
233
+ requestId: string;
234
+ result?: KnowledgeEditorModalResult;
235
+ }
236
+ /** The agent calls one of the tools this frame registered with
237
+ * AGENT_TOOLS. The runtime answers AGENT_TOOL_RESULT with the same
238
+ * `requestId`. */
239
+ interface AgentToolCallMessage {
240
+ type: 'AGENT_TOOL_CALL';
241
+ requestId: string;
242
+ tool: string;
243
+ input: unknown;
244
+ }
245
+ type HostMessage = InitMessage | AgentToolCallMessage | AgentTabsAvailabilityMessage | AuthRefreshMessage | DataInvalidateMessage | OverlayRectMessage | ModalClosedMessage | PageActionSettledMessage | RecordDraftReviewClosedMessage | KnowledgeEditorClosedMessage;
218
246
  interface ReadyMessage {
219
247
  type: 'READY';
220
248
  }
@@ -256,6 +284,40 @@ interface OpenRecordDraftReviewMessage {
256
284
  /** The record draft to review. */
257
285
  recordDraftId: string;
258
286
  }
287
+ /** Fields for a knowledge node the editor modal creates on its first save. */
288
+ interface KnowledgeEditorCreateDefaults {
289
+ /** Prefills the title field. */
290
+ title?: string;
291
+ summary?: string;
292
+ /** File the node into a space. Omitted: unassigned. */
293
+ space_slug?: string;
294
+ status?: 'draft' | 'published';
295
+ label_ids?: string[];
296
+ }
297
+ /** Ask the host to open its knowledge editor — the app's own `KnowledgeEditor`
298
+ * in a dialog — on one knowledge node, or on a new one. Read mode with an
299
+ * outline, Edit for the tools, local drafts, and a save that refuses to
300
+ * overwrite a node changed meanwhile. The host answers
301
+ * KNOWLEDGE_EDITOR_CLOSED with the same `requestId`.
302
+ *
303
+ * One per frame at a time; ignored (answered without a result) on public
304
+ * pages and by hosts without one. */
305
+ interface OpenKnowledgeEditorMessage {
306
+ type: 'OPEN_KNOWLEDGE_EDITOR';
307
+ /** Correlates the eventual KNOWLEDGE_EDITOR_CLOSED. Generated by the runtime. */
308
+ requestId: string;
309
+ /** The node to open. Omit to create one on the first save. */
310
+ nodeId?: string;
311
+ /** Dialog heading. Defaults to the node's title (or "New note"). */
312
+ title?: string;
313
+ /** Where an unsaved draft of a NEW node is kept, so it survives a reload —
314
+ * a key stable for what the node is for, e.g. `note:deal:<record id>`. */
315
+ draftKey?: string;
316
+ /** Used when creating (no `nodeId`). */
317
+ createDefaults?: KnowledgeEditorCreateDefaults;
318
+ /** Open straight into edit mode. Default: read for an existing node. */
319
+ mode?: 'read' | 'edit';
320
+ }
259
321
  /** The runtime has (or no longer has) an overlay open and needs the host to
260
322
  * promote (or restore) the iframe. See {@link COMPONENT_OVERLAY_Z_INDEX}.
261
323
  *
@@ -417,16 +479,44 @@ interface RunPageActionMessage {
417
479
  action: PageAction;
418
480
  context?: PageActionContext;
419
481
  }
482
+ /** A tool a component offers the workspace agent, as it rides the wire. */
483
+ interface AgentToolSpec {
484
+ /** Unique within the component, snake_case, e.g. `preview_clusters`. */
485
+ name: string;
486
+ /** What the tool does and when to use it — the agent reads only this. */
487
+ description: string;
488
+ /** JSON Schema of the input object. */
489
+ inputSchema?: Record<string, unknown>;
490
+ /** Changes nothing (reads what the component shows). */
491
+ readOnly?: boolean;
492
+ }
493
+ /** The tools and state this frame offers the workspace agent — a full
494
+ * replacement of what it posted before. An empty `tools` withdraws the
495
+ * frame. `state` is the snapshot the agent reads in describe_tab; plain,
496
+ * JSON-like data only. Ignored by hosts without an agent workspace. */
497
+ interface AgentToolsMessage {
498
+ type: 'AGENT_TOOLS';
499
+ tools: AgentToolSpec[];
500
+ label?: string;
501
+ state?: unknown;
502
+ }
503
+ /** The answer to AGENT_TOOL_CALL: the handler's output, or its error. */
504
+ interface AgentToolResultMessage {
505
+ type: 'AGENT_TOOL_RESULT';
506
+ requestId: string;
507
+ output?: unknown;
508
+ error?: string;
509
+ }
420
510
  interface ErrorMessage {
421
511
  type: 'ERROR';
422
512
  message: string;
423
513
  stack?: string;
424
514
  }
425
- type RuntimeMessage = ReadyMessage | ResizeMessage | NavigateMessage | OpenPeekMessage | OpenCallMessage | OpenRecordDraftReviewMessage | OverlayStateMessage | OpenModalMessage | ModalResultMessage | EscapePressedMessage | OpenAgentWindowMessage | CloseAgentWindowMessage | RecordWrittenMessage | RunPageActionMessage | ErrorMessage;
515
+ type RuntimeMessage = ReadyMessage | ResizeMessage | NavigateMessage | OpenPeekMessage | OpenCallMessage | OpenRecordDraftReviewMessage | OpenKnowledgeEditorMessage | OverlayStateMessage | OpenModalMessage | ModalResultMessage | EscapePressedMessage | OpenAgentWindowMessage | CloseAgentWindowMessage | RecordWrittenMessage | RunPageActionMessage | AgentToolsMessage | AgentToolResultMessage | ErrorMessage;
426
516
  type Envelope<T> = T & {
427
517
  source: typeof MESSAGE_SOURCE;
428
518
  };
429
519
  declare function isHostMessage(data: unknown): data is Envelope<HostMessage>;
430
520
  declare function isRuntimeMessage(data: unknown): data is Envelope<RuntimeMessage>;
431
521
 
432
- export { type AgentTabsAvailabilityMessage, type AgentWindowTab, type AuthRefreshMessage, COMPONENT_OVERLAY_Z_INDEX, type CloseAgentWindowMessage, type DataInvalidateMessage, type Envelope, type ErrorMessage, type EscapePressedMessage, type HostMessage, type InitMessage, MESSAGE_SOURCE, type ModalClosedMessage, type ModalResultMessage, type ModalWidth, type NavigateMessage, type OpenAgentWindowMessage, type OpenCallMessage, type OpenModalMessage, type OpenPeekMessage, type OpenRecordDraftReviewMessage, type OverlayRect, type OverlayRectMessage, type OverlayStateMessage, type PageActionContext, type PageActionOutcome, type PageActionRecord, type PageActionSettledMessage, type PageContext, type PageMode, type ReadyMessage, type RecordDraftReviewClosedMessage, type RecordDraftReviewOutcome, type RecordWrittenMessage, type ResizeMessage, type RunPageActionMessage, type RuntimeMessage, type TriggerPhase, isHostMessage, isRuntimeMessage };
522
+ export { type AgentTabsAvailabilityMessage, type AgentToolCallMessage, type AgentToolResultMessage, type AgentToolSpec, type AgentToolsMessage, type AgentWindowTab, type AuthRefreshMessage, COMPONENT_OVERLAY_Z_INDEX, type CloseAgentWindowMessage, type DataInvalidateMessage, type Envelope, type ErrorMessage, type EscapePressedMessage, type HostMessage, type InitMessage, type KnowledgeEditorClosedMessage, type KnowledgeEditorCreateDefaults, type KnowledgeEditorModalResult, MESSAGE_SOURCE, type ModalClosedMessage, type ModalResultMessage, type ModalWidth, type NavigateMessage, type OpenAgentWindowMessage, type OpenCallMessage, type OpenKnowledgeEditorMessage, type OpenModalMessage, type OpenPeekMessage, type OpenRecordDraftReviewMessage, type OverlayRect, type OverlayRectMessage, type OverlayStateMessage, type PageActionContext, type PageActionOutcome, type PageActionRecord, type PageActionSettledMessage, type PageContext, type PageMode, type ReadyMessage, type RecordDraftReviewClosedMessage, type RecordDraftReviewOutcome, type RecordWrittenMessage, type ResizeMessage, type RunPageActionMessage, type RuntimeMessage, type TriggerPhase, isHostMessage, isRuntimeMessage };
@@ -214,7 +214,35 @@ interface RecordDraftReviewClosedMessage {
214
214
  requestId: string;
215
215
  outcome?: RecordDraftReviewOutcome;
216
216
  }
217
- type HostMessage = InitMessage | AgentTabsAvailabilityMessage | AuthRefreshMessage | DataInvalidateMessage | OverlayRectMessage | ModalClosedMessage | PageActionSettledMessage | RecordDraftReviewClosedMessage;
217
+ /** What a host knowledge-editor modal reports once it closes, when it saved
218
+ * (or created) the node at least once. */
219
+ interface KnowledgeEditorModalResult {
220
+ /** The node edited — or created, see `created`. */
221
+ nodeId: string;
222
+ /** Its title as last saved. */
223
+ title: string;
224
+ /** True when the modal created the node (no `nodeId` was passed). */
225
+ created: boolean;
226
+ }
227
+ /** Resolution of an OPEN_KNOWLEDGE_EDITOR request: the modal closed. `result`
228
+ * is absent when nothing was saved — dismissed (an unsaved draft stays on
229
+ * the device), or the host could not open it (a public page, a preview, a
230
+ * modal from this frame already open). */
231
+ interface KnowledgeEditorClosedMessage {
232
+ type: 'KNOWLEDGE_EDITOR_CLOSED';
233
+ requestId: string;
234
+ result?: KnowledgeEditorModalResult;
235
+ }
236
+ /** The agent calls one of the tools this frame registered with
237
+ * AGENT_TOOLS. The runtime answers AGENT_TOOL_RESULT with the same
238
+ * `requestId`. */
239
+ interface AgentToolCallMessage {
240
+ type: 'AGENT_TOOL_CALL';
241
+ requestId: string;
242
+ tool: string;
243
+ input: unknown;
244
+ }
245
+ type HostMessage = InitMessage | AgentToolCallMessage | AgentTabsAvailabilityMessage | AuthRefreshMessage | DataInvalidateMessage | OverlayRectMessage | ModalClosedMessage | PageActionSettledMessage | RecordDraftReviewClosedMessage | KnowledgeEditorClosedMessage;
218
246
  interface ReadyMessage {
219
247
  type: 'READY';
220
248
  }
@@ -256,6 +284,40 @@ interface OpenRecordDraftReviewMessage {
256
284
  /** The record draft to review. */
257
285
  recordDraftId: string;
258
286
  }
287
+ /** Fields for a knowledge node the editor modal creates on its first save. */
288
+ interface KnowledgeEditorCreateDefaults {
289
+ /** Prefills the title field. */
290
+ title?: string;
291
+ summary?: string;
292
+ /** File the node into a space. Omitted: unassigned. */
293
+ space_slug?: string;
294
+ status?: 'draft' | 'published';
295
+ label_ids?: string[];
296
+ }
297
+ /** Ask the host to open its knowledge editor — the app's own `KnowledgeEditor`
298
+ * in a dialog — on one knowledge node, or on a new one. Read mode with an
299
+ * outline, Edit for the tools, local drafts, and a save that refuses to
300
+ * overwrite a node changed meanwhile. The host answers
301
+ * KNOWLEDGE_EDITOR_CLOSED with the same `requestId`.
302
+ *
303
+ * One per frame at a time; ignored (answered without a result) on public
304
+ * pages and by hosts without one. */
305
+ interface OpenKnowledgeEditorMessage {
306
+ type: 'OPEN_KNOWLEDGE_EDITOR';
307
+ /** Correlates the eventual KNOWLEDGE_EDITOR_CLOSED. Generated by the runtime. */
308
+ requestId: string;
309
+ /** The node to open. Omit to create one on the first save. */
310
+ nodeId?: string;
311
+ /** Dialog heading. Defaults to the node's title (or "New note"). */
312
+ title?: string;
313
+ /** Where an unsaved draft of a NEW node is kept, so it survives a reload —
314
+ * a key stable for what the node is for, e.g. `note:deal:<record id>`. */
315
+ draftKey?: string;
316
+ /** Used when creating (no `nodeId`). */
317
+ createDefaults?: KnowledgeEditorCreateDefaults;
318
+ /** Open straight into edit mode. Default: read for an existing node. */
319
+ mode?: 'read' | 'edit';
320
+ }
259
321
  /** The runtime has (or no longer has) an overlay open and needs the host to
260
322
  * promote (or restore) the iframe. See {@link COMPONENT_OVERLAY_Z_INDEX}.
261
323
  *
@@ -417,16 +479,44 @@ interface RunPageActionMessage {
417
479
  action: PageAction;
418
480
  context?: PageActionContext;
419
481
  }
482
+ /** A tool a component offers the workspace agent, as it rides the wire. */
483
+ interface AgentToolSpec {
484
+ /** Unique within the component, snake_case, e.g. `preview_clusters`. */
485
+ name: string;
486
+ /** What the tool does and when to use it — the agent reads only this. */
487
+ description: string;
488
+ /** JSON Schema of the input object. */
489
+ inputSchema?: Record<string, unknown>;
490
+ /** Changes nothing (reads what the component shows). */
491
+ readOnly?: boolean;
492
+ }
493
+ /** The tools and state this frame offers the workspace agent — a full
494
+ * replacement of what it posted before. An empty `tools` withdraws the
495
+ * frame. `state` is the snapshot the agent reads in describe_tab; plain,
496
+ * JSON-like data only. Ignored by hosts without an agent workspace. */
497
+ interface AgentToolsMessage {
498
+ type: 'AGENT_TOOLS';
499
+ tools: AgentToolSpec[];
500
+ label?: string;
501
+ state?: unknown;
502
+ }
503
+ /** The answer to AGENT_TOOL_CALL: the handler's output, or its error. */
504
+ interface AgentToolResultMessage {
505
+ type: 'AGENT_TOOL_RESULT';
506
+ requestId: string;
507
+ output?: unknown;
508
+ error?: string;
509
+ }
420
510
  interface ErrorMessage {
421
511
  type: 'ERROR';
422
512
  message: string;
423
513
  stack?: string;
424
514
  }
425
- type RuntimeMessage = ReadyMessage | ResizeMessage | NavigateMessage | OpenPeekMessage | OpenCallMessage | OpenRecordDraftReviewMessage | OverlayStateMessage | OpenModalMessage | ModalResultMessage | EscapePressedMessage | OpenAgentWindowMessage | CloseAgentWindowMessage | RecordWrittenMessage | RunPageActionMessage | ErrorMessage;
515
+ type RuntimeMessage = ReadyMessage | ResizeMessage | NavigateMessage | OpenPeekMessage | OpenCallMessage | OpenRecordDraftReviewMessage | OpenKnowledgeEditorMessage | OverlayStateMessage | OpenModalMessage | ModalResultMessage | EscapePressedMessage | OpenAgentWindowMessage | CloseAgentWindowMessage | RecordWrittenMessage | RunPageActionMessage | AgentToolsMessage | AgentToolResultMessage | ErrorMessage;
426
516
  type Envelope<T> = T & {
427
517
  source: typeof MESSAGE_SOURCE;
428
518
  };
429
519
  declare function isHostMessage(data: unknown): data is Envelope<HostMessage>;
430
520
  declare function isRuntimeMessage(data: unknown): data is Envelope<RuntimeMessage>;
431
521
 
432
- export { type AgentTabsAvailabilityMessage, type AgentWindowTab, type AuthRefreshMessage, COMPONENT_OVERLAY_Z_INDEX, type CloseAgentWindowMessage, type DataInvalidateMessage, type Envelope, type ErrorMessage, type EscapePressedMessage, type HostMessage, type InitMessage, MESSAGE_SOURCE, type ModalClosedMessage, type ModalResultMessage, type ModalWidth, type NavigateMessage, type OpenAgentWindowMessage, type OpenCallMessage, type OpenModalMessage, type OpenPeekMessage, type OpenRecordDraftReviewMessage, type OverlayRect, type OverlayRectMessage, type OverlayStateMessage, type PageActionContext, type PageActionOutcome, type PageActionRecord, type PageActionSettledMessage, type PageContext, type PageMode, type ReadyMessage, type RecordDraftReviewClosedMessage, type RecordDraftReviewOutcome, type RecordWrittenMessage, type ResizeMessage, type RunPageActionMessage, type RuntimeMessage, type TriggerPhase, isHostMessage, isRuntimeMessage };
522
+ export { type AgentTabsAvailabilityMessage, type AgentToolCallMessage, type AgentToolResultMessage, type AgentToolSpec, type AgentToolsMessage, type AgentWindowTab, type AuthRefreshMessage, COMPONENT_OVERLAY_Z_INDEX, type CloseAgentWindowMessage, type DataInvalidateMessage, type Envelope, type ErrorMessage, type EscapePressedMessage, type HostMessage, type InitMessage, type KnowledgeEditorClosedMessage, type KnowledgeEditorCreateDefaults, type KnowledgeEditorModalResult, MESSAGE_SOURCE, type ModalClosedMessage, type ModalResultMessage, type ModalWidth, type NavigateMessage, type OpenAgentWindowMessage, type OpenCallMessage, type OpenKnowledgeEditorMessage, type OpenModalMessage, type OpenPeekMessage, type OpenRecordDraftReviewMessage, type OverlayRect, type OverlayRectMessage, type OverlayStateMessage, type PageActionContext, type PageActionOutcome, type PageActionRecord, type PageActionSettledMessage, type PageContext, type PageMode, type ReadyMessage, type RecordDraftReviewClosedMessage, type RecordDraftReviewOutcome, type RecordWrittenMessage, type ResizeMessage, type RunPageActionMessage, type RuntimeMessage, type TriggerPhase, isHostMessage, isRuntimeMessage };
package/dist/protocol.js CHANGED
@@ -1,3 +1,3 @@
1
- export { COMPONENT_OVERLAY_Z_INDEX, MESSAGE_SOURCE, isHostMessage, isRuntimeMessage } from './chunk-UEPSZRI7.js';
1
+ export { COMPONENT_OVERLAY_Z_INDEX, MESSAGE_SOURCE, isHostMessage, isRuntimeMessage } from './chunk-BJ2JULNO.js';
2
2
  //# sourceMappingURL=protocol.js.map
3
3
  //# sourceMappingURL=protocol.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proteos/component-sdk",
3
- "version": "0.87.0",
3
+ "version": "0.89.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Typed runtime hooks for authoring Proteos custom components — the SDK you import inside a component bundle.",
6
6
  "repository": {
@@ -42,7 +42,7 @@
42
42
  "src"
43
43
  ],
44
44
  "dependencies": {
45
- "@proteos/sdk": "^0.87.0"
45
+ "@proteos/sdk": "^0.89.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "@tanstack/react-query": ">=5.0.0",
@@ -60,8 +60,8 @@
60
60
  "tsup": "^8.3.0",
61
61
  "typescript": "^6.0.3",
62
62
  "vitest": "^3.2.4",
63
- "@proteos/biome-config": "0.0.0",
64
- "@proteos/tsconfig": "0.0.0"
63
+ "@proteos/tsconfig": "0.0.0",
64
+ "@proteos/biome-config": "0.0.0"
65
65
  },
66
66
  "scripts": {
67
67
  "build": "tsup",
package/src/context.tsx CHANGED
@@ -16,12 +16,15 @@ import {
16
16
  type CloseAgentWindowMessage,
17
17
  type Envelope,
18
18
  isHostMessage,
19
+ type KnowledgeEditorCreateDefaults,
20
+ type KnowledgeEditorModalResult,
19
21
  MESSAGE_SOURCE,
20
22
  type ModalResultMessage,
21
23
  type ModalWidth,
22
24
  type NavigateMessage,
23
25
  type OpenAgentWindowMessage,
24
26
  type OpenCallMessage,
27
+ type OpenKnowledgeEditorMessage,
25
28
  type OpenModalMessage,
26
29
  type OpenPeekMessage,
27
30
  type OpenRecordDraftReviewMessage,
@@ -54,6 +57,21 @@ export interface OpenModalOptions {
54
57
  height?: number
55
58
  }
56
59
 
60
+ /** Options for {@link RuntimeContextValue.openKnowledgeEditor}. */
61
+ export interface OpenKnowledgeEditorOptions {
62
+ /** The node to open. Omit to create a new markdown node on the first save. */
63
+ nodeId?: string
64
+ /** Dialog heading. Defaults to the node's title (or "New note"). */
65
+ title?: string
66
+ /** Where an unsaved draft of a NEW node is kept across reloads — a key
67
+ * stable for what the node is for, e.g. `note:deal:<record id>`. */
68
+ draftKey?: string
69
+ /** Fields for the created node (no `nodeId`). */
70
+ createDefaults?: KnowledgeEditorCreateDefaults
71
+ /** Open in edit mode. Default: read for an existing node, edit for a new one. */
72
+ mode?: 'read' | 'edit'
73
+ }
74
+
57
75
  /** What {@link AgentWindowController.open} opens the host's agent window to. */
58
76
  export interface OpenAgentWindowOptions {
59
77
  /** Resume this session (its agent comes with it). */
@@ -167,6 +185,12 @@ export interface RuntimeContextValue extends RuntimeValue {
167
185
  * resolves with the draft's terminal status, or null when it closed
168
186
  * undecided or could not be opened. */
169
187
  openRecordDraftReview: (recordDraftId: string) => Promise<RecordDraftReviewOutcome | null>
188
+ /** Open the host's knowledge editor on a node, or on a new one (posts
189
+ * OPEN_KNOWLEDGE_EDITOR); resolves once it closed — with the node when it
190
+ * was saved or created, null otherwise. */
191
+ openKnowledgeEditor: (
192
+ options?: OpenKnowledgeEditorOptions,
193
+ ) => Promise<KnowledgeEditorModalResult | null>
170
194
  /** Open another component in a host-rendered modal; resolves with the value
171
195
  * that component closed with (`undefined` if dismissed). */
172
196
  openModal: (
@@ -247,6 +271,10 @@ function navigate(to: string, options?: NavigateOptions): void {
247
271
  const pendingModals = new Map<string, (result: unknown) => void>()
248
272
  const pendingPageActions = new Map<string, (outcome: PageActionOutcome) => void>()
249
273
  const pendingDraftReviews = new Map<string, (outcome: RecordDraftReviewOutcome | null) => void>()
274
+ const pendingKnowledgeEditors = new Map<
275
+ string,
276
+ (result: KnowledgeEditorModalResult | null) => void
277
+ >()
250
278
  let replyListenerAttached = false
251
279
 
252
280
  function ensureReplyListener(): void {
@@ -275,6 +303,13 @@ function ensureReplyListener(): void {
275
303
  if (!resolve) return
276
304
  pendingDraftReviews.delete(data.requestId)
277
305
  resolve(data.outcome ?? null)
306
+ return
307
+ }
308
+ if (data.type === 'KNOWLEDGE_EDITOR_CLOSED') {
309
+ const resolve = pendingKnowledgeEditors.get(data.requestId)
310
+ if (!resolve) return
311
+ pendingKnowledgeEditors.delete(data.requestId)
312
+ resolve(data.result ?? null)
278
313
  }
279
314
  })
280
315
  }
@@ -397,6 +432,33 @@ function openRecordDraftReview(recordDraftId: string): Promise<RecordDraftReview
397
432
  })
398
433
  }
399
434
 
435
+ /** Post OPEN_KNOWLEDGE_EDITOR and resolve when the host answers
436
+ * KNOWLEDGE_EDITOR_CLOSED. Focus goes back to the pressed control. */
437
+ function openKnowledgeEditor(
438
+ options: OpenKnowledgeEditorOptions = {},
439
+ ): Promise<KnowledgeEditorModalResult | null> {
440
+ ensureReplyListener()
441
+ const requestId = newRequestId()
442
+ const envelope: Envelope<OpenKnowledgeEditorMessage> = {
443
+ source: MESSAGE_SOURCE,
444
+ type: 'OPEN_KNOWLEDGE_EDITOR',
445
+ requestId,
446
+ ...(options.nodeId !== undefined ? { nodeId: options.nodeId } : {}),
447
+ ...(options.title !== undefined ? { title: options.title } : {}),
448
+ ...(options.draftKey !== undefined ? { draftKey: options.draftKey } : {}),
449
+ ...(options.createDefaults !== undefined ? { createDefaults: options.createDefaults } : {}),
450
+ ...(options.mode !== undefined ? { mode: options.mode } : {}),
451
+ }
452
+ const opener = document.activeElement instanceof HTMLElement ? document.activeElement : null
453
+ return new Promise<KnowledgeEditorModalResult | null>((resolve) => {
454
+ pendingKnowledgeEditors.set(requestId, (result) => {
455
+ restoreFocus(opener)
456
+ resolve(result)
457
+ })
458
+ window.parent?.postMessage(envelope, '*')
459
+ })
460
+ }
461
+
400
462
  /** Post MODAL_RESULT — the component inside a host modal closing with a value. */
401
463
  export function postModalResult(requestId: string, result?: unknown): void {
402
464
  const envelope: Envelope<ModalResultMessage> = {
@@ -425,6 +487,7 @@ export function RuntimeProvider({ value, children }: { value: RuntimeValue; chil
425
487
  navigate,
426
488
  openCall,
427
489
  openRecordDraftReview,
490
+ openKnowledgeEditor,
428
491
  openModal,
429
492
  agentWindow,
430
493
  runPageAction,
@@ -1,9 +1,10 @@
1
- import { renderHook } from '@testing-library/react'
1
+ import { act, renderHook } from '@testing-library/react'
2
2
  import type { ReactNode } from 'react'
3
3
  import { afterEach, describe, expect, it, vi } from 'vitest'
4
4
 
5
5
  import { RuntimeProvider, type RuntimeValue } from './context'
6
6
  import {
7
+ useAgentTools,
7
8
  useAgentWindow,
8
9
  useApiBaseUrl,
9
10
  useComponentModal,
@@ -354,6 +355,110 @@ describe('runtime hooks', () => {
354
355
  )
355
356
  })
356
357
 
358
+ it('useAgentTools posts its tools and state, answers calls with the latest handler, and withdraws on unmount', async () => {
359
+ const post = vi.spyOn(window.parent, 'postMessage').mockImplementation(() => {})
360
+ const posted = () =>
361
+ post.mock.calls.map(([payload]) => payload as Record<string, unknown> & { type: string })
362
+ const host = (data: unknown) =>
363
+ act(async () => {
364
+ window.dispatchEvent(new MessageEvent('message', { data }))
365
+ // Let the handler's promise settle and its reply post.
366
+ await new Promise((resolve) => setTimeout(resolve, 0))
367
+ })
368
+ const { rerender, unmount } = renderHook(
369
+ ({ drafts }: { drafts: number }) =>
370
+ useAgentTools({
371
+ label: 'Cluster canvas',
372
+ state: { drafts },
373
+ tools: [
374
+ {
375
+ name: 'preview_clusters',
376
+ description: 'Draft clusters',
377
+ inputSchema: { type: 'object' },
378
+ run: (input: { count: number }) => ({ drafted: input.count, drafts }),
379
+ },
380
+ {
381
+ name: 'confirm_drafts',
382
+ description: 'Confirm',
383
+ run: () => {
384
+ throw new Error('Nothing to confirm')
385
+ },
386
+ },
387
+ ],
388
+ }),
389
+ { wrapper: wrapper(makeValue()), initialProps: { drafts: 0 } },
390
+ )
391
+ expect(posted()).toEqual([
392
+ {
393
+ source: MESSAGE_SOURCE,
394
+ type: 'AGENT_TOOLS',
395
+ label: 'Cluster canvas',
396
+ state: { drafts: 0 },
397
+ tools: [
398
+ {
399
+ name: 'preview_clusters',
400
+ description: 'Draft clusters',
401
+ inputSchema: { type: 'object' },
402
+ },
403
+ { name: 'confirm_drafts', description: 'Confirm' },
404
+ ],
405
+ },
406
+ ])
407
+ // A re-render with the same state posts nothing; a state change re-posts.
408
+ rerender({ drafts: 0 })
409
+ expect(posted()).toHaveLength(1)
410
+ rerender({ drafts: 2 })
411
+ expect(posted()).toHaveLength(2)
412
+ expect(posted()[1]).toMatchObject({ type: 'AGENT_TOOLS', state: { drafts: 2 } })
413
+
414
+ await host({
415
+ source: MESSAGE_SOURCE,
416
+ type: 'AGENT_TOOL_CALL',
417
+ requestId: 'r1',
418
+ tool: 'preview_clusters',
419
+ input: { count: 3 },
420
+ })
421
+ await host({
422
+ source: MESSAGE_SOURCE,
423
+ type: 'AGENT_TOOL_CALL',
424
+ requestId: 'r2',
425
+ tool: 'confirm_drafts',
426
+ input: {},
427
+ })
428
+ await host({
429
+ source: MESSAGE_SOURCE,
430
+ type: 'AGENT_TOOL_CALL',
431
+ requestId: 'r3',
432
+ tool: 'nope',
433
+ input: {},
434
+ })
435
+ const results = posted().filter((payload) => payload.type === 'AGENT_TOOL_RESULT')
436
+ expect(results).toEqual([
437
+ // The handler of the latest render: it sees drafts = 2.
438
+ {
439
+ source: MESSAGE_SOURCE,
440
+ type: 'AGENT_TOOL_RESULT',
441
+ requestId: 'r1',
442
+ output: { drafted: 3, drafts: 2 },
443
+ },
444
+ {
445
+ source: MESSAGE_SOURCE,
446
+ type: 'AGENT_TOOL_RESULT',
447
+ requestId: 'r2',
448
+ error: 'Nothing to confirm',
449
+ },
450
+ {
451
+ source: MESSAGE_SOURCE,
452
+ type: 'AGENT_TOOL_RESULT',
453
+ requestId: 'r3',
454
+ error: 'This component has no tool nope',
455
+ },
456
+ ])
457
+
458
+ unmount()
459
+ expect(posted().at(-1)).toEqual({ source: MESSAGE_SOURCE, type: 'AGENT_TOOLS', tools: [] })
460
+ })
461
+
357
462
  it('throws a clear error when used outside the runtime', () => {
358
463
  // Silence the expected React error-boundary console noise.
359
464
  vi.spyOn(console, 'error').mockImplementation(() => {})