@powerhousedao/reactor-browser 6.2.2-dev.77 → 6.2.2-dev.79
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/{document-model-modules-BB9s6a3m.js → document-model-modules-DMIR-uSD.js} +36 -2
- package/dist/document-model-modules-DMIR-uSD.js.map +1 -0
- package/dist/{document-operations-I1cGIcPg.js → document-operations-BO-SlTDw.js} +3 -37
- package/dist/document-operations-BO-SlTDw.js.map +1 -0
- package/dist/index.js +3 -3
- package/dist/{selected-document-CtRlEZyi.js → selected-document-xFBNPc9Q.js} +2 -2
- package/dist/{selected-document-CtRlEZyi.js.map → selected-document-xFBNPc9Q.js.map} +1 -1
- package/dist/src/ai/index.d.ts +83 -17
- package/dist/src/ai/index.d.ts.map +1 -1
- package/dist/src/ai/index.js +251 -7
- package/dist/src/ai/index.js.map +1 -1
- package/dist/src/graphql-client/entry.js +2 -2
- package/package.json +9 -9
- package/dist/document-model-modules-BB9s6a3m.js.map +0 -1
- package/dist/document-operations-I1cGIcPg.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selected-document-
|
|
1
|
+
{"version":3,"file":"selected-document-xFBNPc9Q.js","names":[],"sources":["../src/hooks/document-of-type.ts","../src/hooks/selected-document.ts"],"sourcesContent":["import { ModuleNotFoundError } from \"@powerhousedao/reactor\";\nimport type { DocumentDispatch } from \"@powerhousedao/reactor-browser\";\nimport type { Action, PHDocument } from \"@powerhousedao/shared/document-model\";\nimport { DocumentTypeMismatchError } from \"../errors.js\";\nimport { useDocumentById } from \"./document-by-id.js\";\nimport { useDocumentModelModuleById } from \"./document-model-modules.js\";\n\n/** Returns a document of a specific type, throws an error if the found document has a different type */\nexport function useDocumentOfType<\n TDocument extends PHDocument,\n TAction extends Action,\n>(\n documentId: string | null | undefined,\n documentType: string | null | undefined,\n) {\n const [document, dispatch] = useDocumentById(documentId);\n const documentModelModule = useDocumentModelModuleById(documentType);\n\n if (!documentId || !documentType) return [];\n\n if (!document) {\n throw new Error(`Document not found: ${documentId}`);\n }\n if (!documentModelModule) {\n throw new ModuleNotFoundError(documentType);\n }\n\n if (document.header.documentType !== documentType) {\n throw new DocumentTypeMismatchError(\n documentId,\n documentType,\n document.header.documentType,\n );\n }\n\n return [document, dispatch] as [TDocument, DocumentDispatch<TAction>];\n}\n","import type { DocumentDispatch } from \"@powerhousedao/reactor-browser\";\nimport { isFileNode } from \"@powerhousedao/shared/document-drive\";\nimport type { Action, PHDocument } from \"@powerhousedao/shared/document-model\";\nimport { NoSelectedDocumentError } from \"../errors.js\";\nimport type { DispatchFn, UseDispatchResult } from \"./dispatch.js\";\nimport { useDocumentById } from \"./document-by-id.js\";\nimport { useDocumentOfType } from \"./document-of-type.js\";\nimport { useSelectedNode } from \"./selected-node.js\";\n\n/** Returns the selected document id */\nexport function useSelectedDocumentId(): string | undefined {\n const selectedNode = useSelectedNode();\n return selectedNode && isFileNode(selectedNode) ? selectedNode.id : undefined;\n}\n\n/** Returns the selected document. */\nexport function useSelectedDocument(): readonly [\n PHDocument,\n DispatchFn<Action>,\n] {\n const selectedDocumentId = useSelectedDocumentId();\n const [document, dispatch] = useDocumentById(selectedDocumentId);\n if (!document) {\n throw new NoSelectedDocumentError();\n }\n return [document, dispatch] as const;\n}\n\n/** Returns the selected document. */\nexport function useSelectedDocumentSafe(): UseDispatchResult<\n PHDocument,\n Action\n> {\n const selectedDocumentId = useSelectedDocumentId();\n return useDocumentById(selectedDocumentId);\n}\n\n/** Returns the selected document of a specific type, throws an error if the found document has a different type */\nexport function useSelectedDocumentOfType(\n documentType: null | undefined,\n): never[];\nexport function useSelectedDocumentOfType<\n TDocument extends PHDocument,\n TAction extends Action,\n>(documentType: string): [TDocument, DocumentDispatch<TAction>];\nexport function useSelectedDocumentOfType<\n TDocument extends PHDocument,\n TAction extends Action,\n>(\n documentType: string | null | undefined,\n): never[] | [TDocument, DocumentDispatch<TAction>] {\n const documentId = useSelectedDocumentId();\n\n if (!documentType) {\n return [];\n }\n if (!documentId) {\n throw new NoSelectedDocumentError();\n }\n return useDocumentOfType<TDocument, TAction>(documentId, documentType);\n}\n"],"mappings":";;;;;;AAQA,SAAgB,kBAId,YACA,cACA;CACA,MAAM,CAAC,UAAU,YAAY,gBAAgB,WAAW;CACxD,MAAM,sBAAsB,2BAA2B,aAAa;AAEpE,KAAI,CAAC,cAAc,CAAC,aAAc,QAAO,EAAE;AAE3C,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,uBAAuB,aAAa;AAEtD,KAAI,CAAC,oBACH,OAAM,IAAI,oBAAoB,aAAa;AAG7C,KAAI,SAAS,OAAO,iBAAiB,aACnC,OAAM,IAAI,0BACR,YACA,cACA,SAAS,OAAO,aACjB;AAGH,QAAO,CAAC,UAAU,SAAS;;;;;ACzB7B,SAAgB,wBAA4C;CAC1D,MAAM,eAAe,iBAAiB;AACtC,QAAO,gBAAgB,WAAW,aAAa,GAAG,aAAa,KAAK,KAAA;;;AAItE,SAAgB,sBAGd;CAEA,MAAM,CAAC,UAAU,YAAY,gBADF,uBAAuB,CACc;AAChE,KAAI,CAAC,SACH,OAAM,IAAI,yBAAyB;AAErC,QAAO,CAAC,UAAU,SAAS;;;AAI7B,SAAgB,0BAGd;AAEA,QAAO,gBADoB,uBAAuB,CACR;;AAW5C,SAAgB,0BAId,cACkD;CAClD,MAAM,aAAa,uBAAuB;AAE1C,KAAI,CAAC,aACH,QAAO,EAAE;AAEX,KAAI,CAAC,WACH,OAAM,IAAI,yBAAyB;AAErC,QAAO,kBAAsC,YAAY,aAAa"}
|
package/dist/src/ai/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { PhAiToolAnnotations, PhAiToolDescriptor } from "@powerhousedao/shared/document-model";
|
|
2
|
+
import { ISyncManager } from "@powerhousedao/reactor";
|
|
1
3
|
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
2
|
-
import { ZodRawShape } from "zod";
|
|
3
4
|
import { LanguageModel, ModelMessage } from "ai";
|
|
4
5
|
|
|
5
6
|
//#region src/ai/types.d.ts
|
|
@@ -28,13 +29,7 @@ interface AiSettings {
|
|
|
28
29
|
autoApproveWrites: boolean;
|
|
29
30
|
}
|
|
30
31
|
/** MCP-compatible annotation hints; structural, no MCP SDK dependency. */
|
|
31
|
-
type AiToolAnnotations =
|
|
32
|
-
title?: string;
|
|
33
|
-
readOnlyHint?: boolean;
|
|
34
|
-
destructiveHint?: boolean;
|
|
35
|
-
idempotentHint?: boolean;
|
|
36
|
-
openWorldHint?: boolean;
|
|
37
|
-
};
|
|
32
|
+
type AiToolAnnotations = PhAiToolAnnotations;
|
|
38
33
|
/**
|
|
39
34
|
* Provider-agnostic tool descriptor accepted by the chat agent.
|
|
40
35
|
*
|
|
@@ -44,18 +39,17 @@ type AiToolAnnotations = {
|
|
|
44
39
|
* because the envelope shape (MCP `CallToolResult`) is unwrapped at the
|
|
45
40
|
* adapter boundary.
|
|
46
41
|
*/
|
|
47
|
-
type AiToolDescriptor =
|
|
48
|
-
name: string;
|
|
49
|
-
description?: string;
|
|
50
|
-
inputSchema: ZodRawShape;
|
|
51
|
-
annotations?: AiToolAnnotations;
|
|
52
|
-
callback: (args: never) => Promise<unknown>;
|
|
53
|
-
};
|
|
42
|
+
type AiToolDescriptor = PhAiToolDescriptor;
|
|
54
43
|
/** Tool descriptors the app resolves when the user sends a message. */
|
|
55
44
|
type AiToolsProvider = () => Promise<AiToolDescriptor[]>;
|
|
56
45
|
/** Tool names whose execution mutates the reactor and requires approval. */
|
|
57
46
|
declare const WRITE_TOOLS: ReadonlySet<string>;
|
|
58
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Whether executing the tool mutates state and requires user approval:
|
|
49
|
+
* the built-in write tools, or any tool flagged destructive in its
|
|
50
|
+
* annotations (covers package-provided tools outside the built-in set).
|
|
51
|
+
*/
|
|
52
|
+
declare function isWriteTool(name: string, annotations?: AiToolAnnotations): boolean;
|
|
59
53
|
type ToolCallState = "awaiting-approval" | "executing" | "done" | "error" | "rejected";
|
|
60
54
|
/** One rendered unit inside a chat message. */
|
|
61
55
|
type ChatPart = {
|
|
@@ -90,6 +84,9 @@ interface ChatContext {
|
|
|
90
84
|
nodeKind?: "file" | "folder";
|
|
91
85
|
documentType?: string;
|
|
92
86
|
documentName?: string;
|
|
87
|
+
documentId?: string;
|
|
88
|
+
switchboardUrl?: string;
|
|
89
|
+
switchboardGraphqlUrl?: string;
|
|
93
90
|
}
|
|
94
91
|
/** Events the agent emits while running a turn. */
|
|
95
92
|
type AgentEvent = {
|
|
@@ -263,5 +260,74 @@ declare function subscribeAiSettings(listener: Listener): () => void;
|
|
|
263
260
|
/** True when the endpoint, key and model are all configured. */
|
|
264
261
|
declare function isAiConfigured(settings: AiSettings): boolean;
|
|
265
262
|
//#endregion
|
|
266
|
-
|
|
263
|
+
//#region src/ai/switchboard.d.ts
|
|
264
|
+
/**
|
|
265
|
+
* The switchboard serving a remote drive: the base URL the browser talks to
|
|
266
|
+
* and its GraphQL supergraph endpoint.
|
|
267
|
+
*/
|
|
268
|
+
interface DriveSwitchboard {
|
|
269
|
+
/** Switchboard base URL (e.g. `http://localhost:4001`). */
|
|
270
|
+
switchboardUrl: string;
|
|
271
|
+
/** GraphQL supergraph endpoint (`<base>/graphql`). */
|
|
272
|
+
graphqlUrl: string;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Reads the tab-side sync manager from the browser global. Returns
|
|
276
|
+
* `undefined` in non-browser environments or before the reactor client
|
|
277
|
+
* module is initialized.
|
|
278
|
+
*/
|
|
279
|
+
declare function getBrowserSyncManager(): ISyncManager | undefined;
|
|
280
|
+
/**
|
|
281
|
+
* Resolves the switchboard that serves a drive, if any.
|
|
282
|
+
*
|
|
283
|
+
* Every remote drive stores its switchboard's exact GraphQL channel
|
|
284
|
+
* endpoint in its sync remote — `channelConfig = { type: "gql",
|
|
285
|
+
* parameters: { url: "<origin>/graphql/r" } }` — and the switchboard base
|
|
286
|
+
* is that URL minus the `/graphql/r` suffix. Deriving it per drive (rather
|
|
287
|
+
* than from a global setting) is what keeps multi-switchboard deployments
|
|
288
|
+
* correct. Drives that are not synced to a remote switchboard (local
|
|
289
|
+
* drives, other channel types, unexpected shapes) return `undefined`.
|
|
290
|
+
*/
|
|
291
|
+
declare function resolveDriveSwitchboard(driveId: string | undefined, syncManager?: ISyncManager): DriveSwitchboard | undefined;
|
|
292
|
+
/**
|
|
293
|
+
* Introspection over the switchboard supergraph's root query and mutation
|
|
294
|
+
* fields. Types are requested with three levels of `ofType` nesting so
|
|
295
|
+
* that common shapes like `[X!]!` (LIST(NON_NULL(LIST(X)))) render fully.
|
|
296
|
+
*/
|
|
297
|
+
declare const SWITCHBOARD_INTROSPECTION_QUERY: string;
|
|
298
|
+
/** One root query or mutation field, reduced to a prompt-friendly line. */
|
|
299
|
+
type SchemaFieldSummary = {
|
|
300
|
+
name: string;
|
|
301
|
+
description?: string;
|
|
302
|
+
args: string;
|
|
303
|
+
returns: string;
|
|
304
|
+
};
|
|
305
|
+
/**
|
|
306
|
+
* Reduces a GraphQL `__schema` introspection payload (the `data.__schema`
|
|
307
|
+
* object) to a compact summary of its root query and mutation fields.
|
|
308
|
+
* Accepts `unknown` because the payload arrives over the wire.
|
|
309
|
+
*/
|
|
310
|
+
declare function summarizeIntrospectionSchema(schema: unknown): {
|
|
311
|
+
queries: SchemaFieldSummary[];
|
|
312
|
+
mutations: SchemaFieldSummary[];
|
|
313
|
+
truncated: boolean;
|
|
314
|
+
};
|
|
315
|
+
declare const SWITCHBOARD_SCHEMA_TOOL_NAME = "getSwitchboardSchema";
|
|
316
|
+
/** Test seams for {@link createSwitchboardSchemaTool}. */
|
|
317
|
+
interface SwitchboardToolDeps {
|
|
318
|
+
getSyncManager?: () => ISyncManager | undefined;
|
|
319
|
+
getSelectedDriveId?: () => string | undefined;
|
|
320
|
+
tokenProvider?: () => Promise<string | undefined>;
|
|
321
|
+
fetchImpl?: typeof fetch;
|
|
322
|
+
timeoutMs?: number;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Creates the read-only `getSwitchboardSchema` tool: it introspects the
|
|
326
|
+
* GraphQL supergraph of the switchboard serving a drive and returns a
|
|
327
|
+
* summary of its root queries and mutations. It reads the schema only and
|
|
328
|
+
* never modifies data.
|
|
329
|
+
*/
|
|
330
|
+
declare function createSwitchboardSchemaTool(deps?: SwitchboardToolDeps): AiToolDescriptor;
|
|
331
|
+
//#endregion
|
|
332
|
+
export { AgentEvent, AiSettings, AiToolAnnotations, AiToolDescriptor, AiToolsProvider, ApprovalCard, ChatContext, ChatMessage, ChatPart, ChatWindow, DEFAULT_AI_SETTINGS, DriveSwitchboard, MAX_AGENT_STEPS, PendingApproval, ReactorChatAgent, ReactorChatAgentOptions, ReactorChatFab, SWITCHBOARD_INTROSPECTION_QUERY, SWITCHBOARD_SCHEMA_TOOL_NAME, SchemaFieldSummary, SwitchboardToolDeps, ToolCallState, UseReactorChatResult, WRITE_TOOLS, buildSystemPrompt, clearAiSettings, createReactorChatModel, createSwitchboardSchemaTool, getAiSettings, getBrowserSyncManager, isAiConfigured, isWriteTool, resolveDriveSwitchboard, subscribeAiSettings, summarizeIntrospectionSchema, unwrapToolResult, updateAiSettings, useChatContext, useReactorChat };
|
|
267
333
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../../src/ai/types.ts","../../../src/ai/agent.ts","../../../src/ai/components/approval-card.tsx","../../../src/ai/use-reactor-chat.ts","../../../src/ai/components/chat-window.tsx","../../../src/ai/components/reactor-chat-fab.tsx","../../../src/ai/context.ts","../../../src/ai/settings-store.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../../src/ai/types.ts","../../../src/ai/agent.ts","../../../src/ai/components/approval-card.tsx","../../../src/ai/use-reactor-chat.ts","../../../src/ai/components/chat-window.tsx","../../../src/ai/components/reactor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,kBAAA,CAAA,GAAA,CAAA,OAAA;;;;;;;;AJ9GD;;iBKDgB,cAAA,CAAA;EAAiB;AAAA;EAAc,QAAA,GAAW,eAAA;AAAA,IAAiB,kBAAA,CAAA,GAAA,CAAA,OAAA;;;;;;;;iBCF3D,cAAA,CAAA,GAAkB,WAAA;;;cCRrB,mBAAA,EAAqB,UAAA;AAAA,KAQ7B,QAAA;;iBA6CW,aAAA,CAAA,GAAiB,UAAA;;iBAKjB,gBAAA,CAAiB,KAAA,EAAO,OAAA,CAAQ,UAAA,IAAc,UAAA;;iBAQ9C,eAAA,CAAA;;iBAWA,mBAAA,CAAoB,QAAA,EAAU,QAAA;;iBAQ9B,cAAA,CAAe,QAAA,EAAU,UAAA;;;;;;;UChFxB,gBAAA;ERMU;EQJzB,cAAA;ERIyB;EQFzB,UAAA;AAAA;;;;;;iBAQc,qBAAA,CAAA,GAAyB,YAAA;;;;;ARsBzC;;;;;AAGA;;iBQJgB,uBAAA,CACd,OAAA,sBACA,WAAA,GAAc,YAAA,GACb,gBAAA;;;ARIH;;;cQgDa,+BAAA;;KA6GD,kBAAA;EACV,IAAA;EACA,WAAA;EACA,IAAA;EACA,OAAA;AAAA;;;;AR5IF;;iBQyKgB,4BAAA,CAA6B,MAAA;EAC3C,OAAA,EAAS,kBAAA;EACT,SAAA,EAAW,kBAAA;EACX,SAAA;AAAA;AAAA,cAYW,4BAAA;;UAGI,mBAAA;EACf,cAAA,SAAuB,YAAA;EACvB,kBAAA;EACA,aAAA,SAAsB,OAAA;EACtB,SAAA,UAAmB,KAAA;EACnB,SAAA;AAAA;;;;;;AR5KF;iBQqLgB,2BAAA,CACd,IAAA,GAAO,mBAAA,GACN,gBAAA"}
|
package/dist/src/ai/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { S as useSelectedDriveSafe, s as useSelectedNode } from "../../document-model-modules-
|
|
2
|
-
import { n as useSelectedDocumentId } from "../../selected-document-
|
|
1
|
+
import { H as ambientRenownTokenProvider, S as useSelectedDriveSafe, s as useSelectedNode } from "../../document-model-modules-DMIR-uSD.js";
|
|
2
|
+
import { n as useSelectedDocumentId } from "../../selected-document-xFBNPc9Q.js";
|
|
3
3
|
import { isFileNode, isFolderNode } from "@powerhousedao/shared/document-drive";
|
|
4
4
|
import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
5
|
+
import { DriveCollectionId } from "@powerhousedao/reactor";
|
|
5
6
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
7
|
import { z } from "zod";
|
|
7
8
|
import { isStepCount, streamText, tool } from "ai";
|
|
@@ -20,8 +21,13 @@ const WRITE_TOOLS = new Set([
|
|
|
20
21
|
"deleteDrive",
|
|
21
22
|
"addRemoteDrive"
|
|
22
23
|
]);
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Whether executing the tool mutates state and requires user approval:
|
|
26
|
+
* the built-in write tools, or any tool flagged destructive in its
|
|
27
|
+
* annotations (covers package-provided tools outside the built-in set).
|
|
28
|
+
*/
|
|
29
|
+
function isWriteTool(name, annotations) {
|
|
30
|
+
return WRITE_TOOLS.has(name) || annotations?.destructiveHint === true;
|
|
25
31
|
}
|
|
26
32
|
//#endregion
|
|
27
33
|
//#region src/ai/agent.ts
|
|
@@ -83,12 +89,15 @@ function buildSystemPrompt(context) {
|
|
|
83
89
|
"Documents are instances of typed document models; drives are collections that group documents and folders.",
|
|
84
90
|
"Prefer read-only tools to discover state (document models, drives, documents, relationships) before making changes.",
|
|
85
91
|
"Never invent document ids, drive ids, folder ids or document model types — discover them with the read tools first.",
|
|
92
|
+
"Never accept secret values (passwords, tokens, API keys) in chat. When a connection or configuration requires a secret, tell the user to enter it in the relevant editor (e.g. the connection editor) and point them to the document. Never ask the user to paste a secret into the chat.",
|
|
86
93
|
"When the user refers to 'this', 'here' or 'it' without naming a target, they mean the current selection below; prefer it for create/modify targets."
|
|
87
94
|
];
|
|
88
95
|
const selection = [];
|
|
89
96
|
if (context.driveName) selection.push(`The current drive is "${context.driveName}"` + (context.driveId ? ` (id: ${context.driveId})` : "") + ".");
|
|
90
97
|
if (context.nodeKind === "folder" && context.nodeName) selection.push(`The current folder is "${context.nodeName}".`);
|
|
91
|
-
if (context.documentType) selection.push(`The current document is "${context.documentName ?? "unnamed"}" of type "${context.documentType}"
|
|
98
|
+
if (context.documentType) selection.push(`The current document is "${context.documentName ?? "unnamed"}" of type "${context.documentType}"` + (context.documentId ? ` (id: ${context.documentId})` : "") + ".");
|
|
99
|
+
if (context.switchboardUrl) selection.push(`The switchboard for this drive is at ${context.switchboardUrl}; its GraphQL endpoint is ${context.switchboardGraphqlUrl ?? ""}. Use the getSwitchboardSchema tool to list the queries and mutations it exposes.`);
|
|
100
|
+
else if (context.driveId) selection.push("This drive is not synced to a switchboard, so no switchboard endpoints are available for it.");
|
|
92
101
|
if (selection.length > 0) lines.push("Current selection:", ...selection.map((s) => `- ${s}`));
|
|
93
102
|
else lines.push("Nothing is currently selected. When the target of an action is ambiguous, ask the user which drive, folder or document it should apply to.");
|
|
94
103
|
return lines.join("\n");
|
|
@@ -165,7 +174,8 @@ var ReactorChatAgent = class {
|
|
|
165
174
|
}));
|
|
166
175
|
},
|
|
167
176
|
toolApproval: async ({ toolCall }) => {
|
|
168
|
-
|
|
177
|
+
const descriptor = this.options.tools.find((t) => t.name === toolCall.toolName);
|
|
178
|
+
if (settings.autoApproveWrites || !isWriteTool(toolCall.toolName, descriptor?.annotations)) return "not-applicable";
|
|
169
179
|
const toolCallId = toolCall.toolCallId;
|
|
170
180
|
onEvent({
|
|
171
181
|
type: "approval-request",
|
|
@@ -699,6 +709,234 @@ function isAiConfigured(settings) {
|
|
|
699
709
|
return settings.baseUrl.trim().length > 0 && settings.apiKey.trim().length > 0 && settings.model.trim().length > 0;
|
|
700
710
|
}
|
|
701
711
|
//#endregion
|
|
712
|
+
//#region src/ai/switchboard.ts
|
|
713
|
+
/**
|
|
714
|
+
* Reads the tab-side sync manager from the browser global. Returns
|
|
715
|
+
* `undefined` in non-browser environments or before the reactor client
|
|
716
|
+
* module is initialized.
|
|
717
|
+
*/
|
|
718
|
+
function getBrowserSyncManager() {
|
|
719
|
+
if (typeof window === "undefined") return;
|
|
720
|
+
return window.ph?.reactorClientModule?.reactorModule?.syncModule?.syncManager;
|
|
721
|
+
}
|
|
722
|
+
/** Suffix of the per-drive GraphQL channel endpoint the switchboard serves. */
|
|
723
|
+
const GQL_CHANNEL_SUFFIX = "/graphql/r";
|
|
724
|
+
/**
|
|
725
|
+
* Resolves the switchboard that serves a drive, if any.
|
|
726
|
+
*
|
|
727
|
+
* Every remote drive stores its switchboard's exact GraphQL channel
|
|
728
|
+
* endpoint in its sync remote — `channelConfig = { type: "gql",
|
|
729
|
+
* parameters: { url: "<origin>/graphql/r" } }` — and the switchboard base
|
|
730
|
+
* is that URL minus the `/graphql/r` suffix. Deriving it per drive (rather
|
|
731
|
+
* than from a global setting) is what keeps multi-switchboard deployments
|
|
732
|
+
* correct. Drives that are not synced to a remote switchboard (local
|
|
733
|
+
* drives, other channel types, unexpected shapes) return `undefined`.
|
|
734
|
+
*/
|
|
735
|
+
function resolveDriveSwitchboard(driveId, syncManager) {
|
|
736
|
+
const manager = syncManager ?? getBrowserSyncManager();
|
|
737
|
+
if (!driveId || !manager) return;
|
|
738
|
+
const collectionId = DriveCollectionId.forDrive(driveId);
|
|
739
|
+
const remote = manager.list().find((r) => r.meta.collectionId.equals(collectionId));
|
|
740
|
+
if (!remote) return;
|
|
741
|
+
const { channelConfig } = remote.meta;
|
|
742
|
+
if (channelConfig.type !== "gql") return;
|
|
743
|
+
const url = channelConfig.parameters.url;
|
|
744
|
+
if (typeof url !== "string" || !url.endsWith(GQL_CHANNEL_SUFFIX)) return;
|
|
745
|
+
const switchboardUrl = url.slice(0, -10);
|
|
746
|
+
return {
|
|
747
|
+
switchboardUrl,
|
|
748
|
+
graphqlUrl: `${switchboardUrl}/graphql`
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Introspection over the switchboard supergraph's root query and mutation
|
|
753
|
+
* fields. Types are requested with three levels of `ofType` nesting so
|
|
754
|
+
* that common shapes like `[X!]!` (LIST(NON_NULL(LIST(X)))) render fully.
|
|
755
|
+
*/
|
|
756
|
+
const SWITCHBOARD_INTROSPECTION_QUERY = `
|
|
757
|
+
query SwitchboardIntrospection {
|
|
758
|
+
__schema {
|
|
759
|
+
queryType {
|
|
760
|
+
fields {
|
|
761
|
+
name
|
|
762
|
+
description
|
|
763
|
+
args {
|
|
764
|
+
name
|
|
765
|
+
type {
|
|
766
|
+
kind
|
|
767
|
+
name
|
|
768
|
+
ofType {
|
|
769
|
+
kind
|
|
770
|
+
name
|
|
771
|
+
ofType {
|
|
772
|
+
kind
|
|
773
|
+
name
|
|
774
|
+
ofType {
|
|
775
|
+
kind
|
|
776
|
+
name
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
type {
|
|
783
|
+
kind
|
|
784
|
+
name
|
|
785
|
+
ofType {
|
|
786
|
+
kind
|
|
787
|
+
name
|
|
788
|
+
ofType {
|
|
789
|
+
kind
|
|
790
|
+
name
|
|
791
|
+
ofType {
|
|
792
|
+
kind
|
|
793
|
+
name
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
mutationType {
|
|
801
|
+
fields {
|
|
802
|
+
name
|
|
803
|
+
description
|
|
804
|
+
args {
|
|
805
|
+
name
|
|
806
|
+
type {
|
|
807
|
+
kind
|
|
808
|
+
name
|
|
809
|
+
ofType {
|
|
810
|
+
kind
|
|
811
|
+
name
|
|
812
|
+
ofType {
|
|
813
|
+
kind
|
|
814
|
+
name
|
|
815
|
+
ofType {
|
|
816
|
+
kind
|
|
817
|
+
name
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
type {
|
|
824
|
+
kind
|
|
825
|
+
name
|
|
826
|
+
ofType {
|
|
827
|
+
kind
|
|
828
|
+
name
|
|
829
|
+
ofType {
|
|
830
|
+
kind
|
|
831
|
+
name
|
|
832
|
+
ofType {
|
|
833
|
+
kind
|
|
834
|
+
name
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
`;
|
|
844
|
+
/**
|
|
845
|
+
* Renders an introspected type reference as a GraphQL type string.
|
|
846
|
+
* Recursion is bounded by the `ofType` nesting of the query, so it always
|
|
847
|
+
* terminates.
|
|
848
|
+
*/
|
|
849
|
+
function renderTypeRef(type) {
|
|
850
|
+
if (!type) return "Unknown";
|
|
851
|
+
switch (type.kind) {
|
|
852
|
+
case "NON_NULL": return `${renderTypeRef(type.ofType)}!`;
|
|
853
|
+
case "LIST": return `[${renderTypeRef(type.ofType)}]`;
|
|
854
|
+
default: return type.name ?? "Unknown";
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
/** Cap on the number of fields reported per root type. */
|
|
858
|
+
const MAX_FIELDS_PER_ROOT = 200;
|
|
859
|
+
function summarizeFields(fields) {
|
|
860
|
+
const list = fields ?? [];
|
|
861
|
+
return {
|
|
862
|
+
fields: list.slice(0, MAX_FIELDS_PER_ROOT).map((field) => ({
|
|
863
|
+
name: field.name,
|
|
864
|
+
...field.description ? { description: field.description } : {},
|
|
865
|
+
args: field.args.map((arg) => `${arg.name}: ${renderTypeRef(arg.type)}`).join(", "),
|
|
866
|
+
returns: renderTypeRef(field.type)
|
|
867
|
+
})),
|
|
868
|
+
truncated: list.length > MAX_FIELDS_PER_ROOT
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Reduces a GraphQL `__schema` introspection payload (the `data.__schema`
|
|
873
|
+
* object) to a compact summary of its root query and mutation fields.
|
|
874
|
+
* Accepts `unknown` because the payload arrives over the wire.
|
|
875
|
+
*/
|
|
876
|
+
function summarizeIntrospectionSchema(schema) {
|
|
877
|
+
const introspection = schema ?? {};
|
|
878
|
+
const queries = summarizeFields(introspection.queryType?.fields);
|
|
879
|
+
const mutations = summarizeFields(introspection.mutationType?.fields);
|
|
880
|
+
return {
|
|
881
|
+
queries: queries.fields,
|
|
882
|
+
mutations: mutations.fields,
|
|
883
|
+
truncated: queries.truncated || mutations.truncated
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
const SWITCHBOARD_SCHEMA_TOOL_NAME = "getSwitchboardSchema";
|
|
887
|
+
/**
|
|
888
|
+
* Creates the read-only `getSwitchboardSchema` tool: it introspects the
|
|
889
|
+
* GraphQL supergraph of the switchboard serving a drive and returns a
|
|
890
|
+
* summary of its root queries and mutations. It reads the schema only and
|
|
891
|
+
* never modifies data.
|
|
892
|
+
*/
|
|
893
|
+
function createSwitchboardSchemaTool(deps) {
|
|
894
|
+
const getSyncManager = deps?.getSyncManager ?? getBrowserSyncManager;
|
|
895
|
+
const getSelectedDriveId = deps?.getSelectedDriveId ?? (() => typeof window === "undefined" ? void 0 : window.ph?.selectedDriveId);
|
|
896
|
+
const tokenProvider = deps?.tokenProvider ?? ambientRenownTokenProvider;
|
|
897
|
+
const fetchImpl = deps?.fetchImpl ?? fetch;
|
|
898
|
+
const timeoutMs = deps?.timeoutMs ?? 1e4;
|
|
899
|
+
return {
|
|
900
|
+
name: SWITCHBOARD_SCHEMA_TOOL_NAME,
|
|
901
|
+
description: "Introspect the GraphQL endpoint of the switchboard serving a drive and list the queries and mutations it exposes (reactor operations, document-model read models, package subgraphs). Read-only: it reads the schema only and never modifies data. Omit driveId to introspect the currently selected drive.",
|
|
902
|
+
inputSchema: { driveId: z.string().describe("Drive to introspect. Omit to use the currently selected drive.").optional() },
|
|
903
|
+
annotations: { readOnlyHint: true },
|
|
904
|
+
callback: async (input) => {
|
|
905
|
+
const driveId = input?.driveId ?? getSelectedDriveId();
|
|
906
|
+
const sb = resolveDriveSwitchboard(driveId, getSyncManager());
|
|
907
|
+
if (!sb) throw new Error(driveId ? `Drive "${driveId}" has no switchboard: it is not synced to a remote switchboard.` : "No drive is currently selected and no driveId was given.");
|
|
908
|
+
const token = await tokenProvider();
|
|
909
|
+
let response;
|
|
910
|
+
try {
|
|
911
|
+
response = await fetchImpl(sb.graphqlUrl, {
|
|
912
|
+
method: "POST",
|
|
913
|
+
headers: {
|
|
914
|
+
"content-type": "application/json",
|
|
915
|
+
...token ? { authorization: `Bearer ${token}` } : {}
|
|
916
|
+
},
|
|
917
|
+
body: JSON.stringify({ query: SWITCHBOARD_INTROSPECTION_QUERY }),
|
|
918
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
919
|
+
});
|
|
920
|
+
} catch (error) {
|
|
921
|
+
throw new Error(`Switchboard at ${sb.graphqlUrl} is not reachable: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
922
|
+
}
|
|
923
|
+
if (response.status === 401 || response.status === 403) throw new Error(`Switchboard at ${sb.graphqlUrl} refused the request (HTTP ${response.status}). Sign in to the switchboard (Renown) and try again.`);
|
|
924
|
+
if (!response.ok) throw new Error(`Switchboard introspection failed: HTTP ${response.status} ${response.statusText} from ${sb.graphqlUrl}`);
|
|
925
|
+
const body = await response.json();
|
|
926
|
+
const schemaErrors = body?.errors;
|
|
927
|
+
if (schemaErrors && schemaErrors.length > 0) throw new Error(schemaErrors.map((e) => e.message ?? "unknown error").join("; "));
|
|
928
|
+
const schema = body?.data?.__schema;
|
|
929
|
+
if (!schema?.queryType) throw new Error(`Switchboard at ${sb.graphqlUrl} did not return a GraphQL schema.`);
|
|
930
|
+
const summary = summarizeIntrospectionSchema(schema);
|
|
931
|
+
return {
|
|
932
|
+
switchboardUrl: sb.switchboardUrl,
|
|
933
|
+
graphqlUrl: sb.graphqlUrl,
|
|
934
|
+
...summary
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
//#endregion
|
|
702
940
|
//#region src/ai/context.ts
|
|
703
941
|
/**
|
|
704
942
|
* Snapshot of the current selection in the drive explorer, used to make the
|
|
@@ -722,6 +960,12 @@ function useChatContext() {
|
|
|
722
960
|
if (documentId && node && isFileNode(node)) {
|
|
723
961
|
context.documentName = node.name;
|
|
724
962
|
context.documentType = node.documentType;
|
|
963
|
+
context.documentId = documentId;
|
|
964
|
+
}
|
|
965
|
+
const sb = resolveDriveSwitchboard(drive?.header.id);
|
|
966
|
+
if (sb) {
|
|
967
|
+
context.switchboardUrl = sb.switchboardUrl;
|
|
968
|
+
context.switchboardGraphqlUrl = sb.graphqlUrl;
|
|
725
969
|
}
|
|
726
970
|
return context;
|
|
727
971
|
}
|
|
@@ -946,6 +1190,6 @@ function ReactorChatFab({ getTools }) {
|
|
|
946
1190
|
})] });
|
|
947
1191
|
}
|
|
948
1192
|
//#endregion
|
|
949
|
-
export { ApprovalCard, ChatWindow, DEFAULT_AI_SETTINGS, MAX_AGENT_STEPS, ReactorChatAgent, ReactorChatFab, WRITE_TOOLS, buildSystemPrompt, clearAiSettings, createReactorChatModel, getAiSettings, isAiConfigured, isWriteTool, subscribeAiSettings, unwrapToolResult, updateAiSettings, useChatContext, useReactorChat };
|
|
1193
|
+
export { ApprovalCard, ChatWindow, DEFAULT_AI_SETTINGS, MAX_AGENT_STEPS, ReactorChatAgent, ReactorChatFab, SWITCHBOARD_INTROSPECTION_QUERY, SWITCHBOARD_SCHEMA_TOOL_NAME, WRITE_TOOLS, buildSystemPrompt, clearAiSettings, createReactorChatModel, createSwitchboardSchemaTool, getAiSettings, getBrowserSyncManager, isAiConfigured, isWriteTool, resolveDriveSwitchboard, subscribeAiSettings, summarizeIntrospectionSchema, unwrapToolResult, updateAiSettings, useChatContext, useReactorChat };
|
|
950
1194
|
|
|
951
1195
|
//# sourceMappingURL=index.js.map
|