fix: service type

This commit is contained in:
linonetwo 2026-02-11 18:51:42 +08:00
parent 021a8af106
commit fa491658f1
6 changed files with 84 additions and 48 deletions

View file

@ -1,14 +1,11 @@
import type { TidgiService } from '@/types/tidgi-tw';
import { contextBridge } from 'electron';
import * as service from './services';
const attachServiceToTw = () => {
if (typeof window === 'undefined') return;
const tw = (window as unknown as { $tw?: { tidgi?: Record<string, unknown> } }).$tw;
if (!tw) return;
if (!tw.tidgi) {
tw.tidgi = {};
}
(tw.tidgi as { service?: typeof service }).service = service;
if (typeof $tw === 'undefined') return false;
$tw.tidgi = { service: service as unknown as TidgiService };
return true;
};
// add window.service for browserView content
@ -17,5 +14,15 @@ contextBridge.exposeInMainWorld('service', service);
window.service = service;
// keep $tw.tidgi.service available once $tw is ready
attachServiceToTw();
window.addEventListener('DOMContentLoaded', attachServiceToTw);
// retry until $tw is available
const tryAttach = () => {
if (attachServiceToTw()) {
return;
}
const interval = setInterval(() => {
if (attachServiceToTw()) {
clearInterval(interval);
}
}, 50);
};
tryAttach();

View file

@ -55,30 +55,3 @@ export const wikiGitWorkspace = createProxy<IWikiGitWorkspaceService>(WikiGitWor
export const window = createProxy<IWindowService>(WindowServiceIPCDescriptor);
export const workspace = createProxy<AsyncifyProxy<IWorkspaceService>>(WorkspaceServiceIPCDescriptor);
export const workspaceView = createProxy<IWorkspaceViewService>(WorkspaceViewServiceIPCDescriptor);
export const descriptors = {
agentBrowser: AgentBrowserServiceIPCDescriptor,
agentDefinition: AgentDefinitionServiceIPCDescriptor,
agentInstance: AgentInstanceServiceIPCDescriptor,
auth: AuthenticationServiceIPCDescriptor,
context: ContextServiceIPCDescriptor,
deepLink: DeepLinkServiceIPCDescriptor,
git: GitServiceIPCDescriptor,
menu: MenuServiceIPCDescriptor,
native: NativeServiceIPCDescriptor,
notification: NotificationServiceIPCDescriptor,
preference: PreferenceServiceIPCDescriptor,
sync: SyncServiceIPCDescriptor,
systemPreference: SystemPreferenceServiceIPCDescriptor,
theme: ThemeServiceIPCDescriptor,
updater: UpdaterServiceIPCDescriptor,
view: ViewServiceIPCDescriptor,
wiki: WikiServiceIPCDescriptor,
wikiEmbedding: WikiEmbeddingServiceIPCDescriptor,
wikiGitWorkspace: WikiGitWorkspaceServiceIPCDescriptor,
window: WindowServiceIPCDescriptor,
workspace: WorkspaceServiceIPCDescriptor,
workspaceView: WorkspaceViewServiceIPCDescriptor,
externalAPI: ExternalAPIServiceIPCDescriptor,
database: DatabaseServiceIPCDescriptor,
};

View file

@ -309,7 +309,22 @@ export class IpcServerRoutes {
}
export const ipcServerRoutes: IpcServerRoutes = new IpcServerRoutes();
export const ipcServerRoutesMethods = {
// Explicit method signature to preserve types
type IpcServerRoutesMethodsType = {
deleteTiddler: IpcServerRoutes['deleteTiddler'];
getFavicon: IpcServerRoutes['getFavicon'];
getIndex: IpcServerRoutes['getIndex'];
getStatus: IpcServerRoutes['getStatus'];
getTiddler: IpcServerRoutes['getTiddler'];
getTiddlerHtml: IpcServerRoutes['getTiddlerHtml'];
getTiddlersJSON: IpcServerRoutes['getTiddlersJSON'];
putTiddler: IpcServerRoutes['putTiddler'];
getFile: IpcServerRoutes['getFile'];
getWikiChangeObserver: IpcServerRoutes['getWikiChangeObserver'];
};
export const ipcServerRoutesMethods: IpcServerRoutesMethodsType = {
deleteTiddler: ipcServerRoutes.deleteTiddler.bind(ipcServerRoutes),
getFavicon: ipcServerRoutes.getFavicon.bind(ipcServerRoutes),
getIndex: ipcServerRoutes.getIndex.bind(ipcServerRoutes),
@ -325,5 +340,5 @@ export const ipcServerRoutesMethods = {
/**
* Available methods for ipcServerRoutes exposed from wiki worker
*/
export type IpcServerRouteMethods = Omit<typeof ipcServerRoutesMethods, 'getWikiChangeObserver'>;
export type IpcServerRouteMethods = Omit<IpcServerRoutesMethodsType, 'getWikiChangeObserver'>;
export type IpcServerRouteNames = keyof IpcServerRouteMethods;

View file

@ -36,7 +36,7 @@ import { type IWorkspaceViewService, WorkspaceViewServiceIPCDescriptor } from '@
export const agentBrowser = createWorkerProxy<WorkerProxy<IAgentBrowserService>>(AgentBrowserServiceIPCDescriptor, Observable);
export const agentDefinition = createWorkerProxy<WorkerProxy<IAgentDefinitionService>>(AgentDefinitionServiceIPCDescriptor, Observable);
export const agentInstance = createWorkerProxy<WorkerProxy<IAgentInstanceService>>(AgentInstanceServiceIPCDescriptor, Observable);
export const authentication = createWorkerProxy<WorkerProxy<IAuthenticationService>>(AuthenticationServiceIPCDescriptor, Observable);
export const auth = createWorkerProxy<WorkerProxy<IAuthenticationService>>(AuthenticationServiceIPCDescriptor, Observable);
export const context = createWorkerProxy<WorkerProxy<IContextService>>(ContextServiceIPCDescriptor, Observable);
export const database = createWorkerProxy<WorkerProxy<IDatabaseService>>(DatabaseServiceIPCDescriptor, Observable);
export const deepLink = createWorkerProxy<WorkerProxy<IDeepLinkService>>(DeepLinkServiceIPCDescriptor, Observable);
@ -67,7 +67,7 @@ export const service = {
agentBrowser,
agentDefinition,
agentInstance,
authentication,
auth,
context,
database,
deepLink,

View file

@ -5,6 +5,7 @@ import { onWorkerServicesReady } from './servicesReady';
import { getTidGiAuthHeaderWithToken } from '@/constants/auth';
import { defaultServerIP } from '@/constants/urls';
import type { TidgiService } from '@/types/tidgi-tw';
import { DARK_LIGHT_CHANGE_ACTIONS_TAG } from '@services/theme/interface';
import intercept from 'intercept-stdout';
import { nanoid } from 'nanoid';
@ -228,10 +229,10 @@ export function startNodeJSWiki(configs: IStartNodeJSWikiConfigs): Observable<IW
* The sandbox injects `$tw` but NOT `globalThis` or `global`,
* so `$tw.tidgi.service` is the only way for plugins to reach IPC service proxies.
*/
type TidgiContainer = { tidgi?: { service?: typeof service } };
const wikiInstanceWithTidgi = wikiInstance as typeof wikiInstance & TidgiContainer;
type TidgiContainer = { tidgi?: { service?: TidgiService } };
const wikiInstanceWithTidgi = wikiInstance as unknown as (typeof wikiInstance & TidgiContainer);
wikiInstanceWithTidgi.tidgi = wikiInstanceWithTidgi.tidgi ?? {};
wikiInstanceWithTidgi.tidgi.service = service;
wikiInstanceWithTidgi.tidgi.service = service as unknown as TidgiService;
wikiInstance.hooks.addHook('th-server-command-post-start', function(_server: unknown, nodeServer: Server) {
nodeServer.on('error', function(error: Error) {

View file

@ -1,16 +1,56 @@
import type { IAgentBrowserService } from '@services/agentBrowser/interface';
import type { IAgentDefinitionService } from '@services/agentDefinition/interface';
import type { IAgentInstanceService } from '@services/agentInstance/interface';
import type { IAuthenticationService } from '@services/auth/interface';
import type { IContextService } from '@services/context/interface';
import type { IDatabaseService } from '@services/database/interface';
import type { IDeepLinkService } from '@services/deepLink/interface';
import type { IExternalAPIService } from '@services/externalAPI/interface';
import type { IGitService } from '@services/git/interface';
import type { IGitServerService } from '@services/gitServer/interface';
import type { IMenuService } from '@services/menu/interface';
import type { INativeService } from '@services/native/interface';
import type { INotificationService } from '@services/notifications/interface';
import type { IPreferenceService } from '@services/preferences/interface';
import type { ISyncService } from '@services/sync/interface';
import type { ISystemPreferenceService } from '@services/systemPreferences/interface';
import type { IThemeService } from '@services/theme/interface';
import type { IUpdaterService } from '@services/updater/interface';
import type { IViewService } from '@services/view/interface';
import type { IWikiService } from '@services/wiki/interface';
import type { IWikiEmbeddingService } from '@services/wikiEmbedding/interface';
import type { IWikiGitWorkspaceService } from '@services/wikiGitWorkspace/interface';
import type { IWindowService } from '@services/windows/interface';
import type { IWorkspaceService } from '@services/workspaces/interface';
import type { descriptors } from '@/preload/common/services';
import type { IWorkspaceViewService } from '@services/workspacesView/interface';
type TidgiService = {
export type TidgiService = {
agentBrowser: IAgentBrowserService;
agentDefinition: IAgentDefinitionService;
agentInstance: IAgentInstanceService;
auth: IAuthenticationService;
context: IContextService;
database: IDatabaseService;
deepLink: IDeepLinkService;
externalAPI: IExternalAPIService;
git: IGitService;
gitServer?: IGitServerService;
menu: IMenuService;
native: INativeService;
workspace: IWorkspaceService;
notification: INotificationService;
preference: IPreferenceService;
sync: ISyncService;
systemPreference: ISystemPreferenceService;
theme: IThemeService;
updater: IUpdaterService;
view: IViewService;
wiki: IWikiService;
descriptors: typeof descriptors;
} & Record<string, unknown>;
wikiEmbedding: IWikiEmbeddingService;
wikiGitWorkspace: IWikiGitWorkspaceService;
window: IWindowService;
workspace: IWorkspaceService;
workspaceView: IWorkspaceViewService;
};
declare module 'tiddlywiki' {
interface ITiddlyWiki {