|
1 | | -import type { |
2 | | - CurrentValueResult, |
3 | | - HistoricalValueResult, |
4 | | - I3xStack, |
5 | | - ModelNode, |
6 | | - Namespace, |
7 | | - ObjectInstanceResponse, |
8 | | - ObjectType, |
9 | | - RelationshipType, |
10 | | - ServerInfo, |
11 | | -} from '@node-i3x/core'; |
12 | | -import { rankSuggestions } from '../utils/similarity.js'; |
13 | | -import type { I3XClientBinding } from './types.js'; |
14 | | - |
15 | | -export class InProcessConnectorBinding implements I3XClientBinding { |
16 | | - constructor(private readonly stack: I3xStack) {} |
17 | | - |
18 | | - async getInfo(): Promise<ServerInfo> { |
19 | | - return { |
20 | | - specVersion: '1.0.0-beta', |
21 | | - serverVersion: '0.10.0', |
22 | | - serverName: 'node-i3x (In-Process Direct)', |
23 | | - capabilities: { |
24 | | - query: { history: true }, |
25 | | - update: { current: true, history: true }, |
26 | | - subscribe: { stream: true }, |
27 | | - }, |
28 | | - }; |
29 | | - } |
30 | | - |
31 | | - async getNamespaces(): Promise<Namespace[]> { |
32 | | - const model = await this.stack.modelService.getOrBuildModel(); |
33 | | - const uris = new Set<string>(); |
34 | | - for (const node of model.nodesById.values()) { |
35 | | - if (node.namespaceUri) uris.add(node.namespaceUri); |
36 | | - } |
37 | | - return Array.from(uris).map((uri) => ({ |
38 | | - uri, |
39 | | - displayName: uri, |
40 | | - })); |
41 | | - } |
42 | | - |
43 | | - async getObjectTypes(namespaceUri?: string): Promise<ObjectType[]> { |
44 | | - return this.stack.typeService.getObjectTypes(namespaceUri); |
45 | | - } |
46 | | - |
47 | | - async getRelationshipTypes(): Promise<RelationshipType[]> { |
48 | | - return [ |
49 | | - { |
50 | | - elementId: 'HasComponent', |
51 | | - displayName: 'Has Component', |
52 | | - namespaceUri: 'http://opcfoundation.org/UA/', |
53 | | - relationshipId: 'i=47', |
54 | | - reverseOf: 'ComponentOf', |
55 | | - }, |
56 | | - { |
57 | | - elementId: 'HasProperty', |
58 | | - displayName: 'Has Property', |
59 | | - namespaceUri: 'http://opcfoundation.org/UA/', |
60 | | - relationshipId: 'i=46', |
61 | | - reverseOf: 'PropertyOf', |
62 | | - }, |
63 | | - ]; |
64 | | - } |
65 | | - |
66 | | - async getObjects(options?: { |
67 | | - rootOnly?: boolean; |
68 | | - typeElementId?: string; |
69 | | - }): Promise<ObjectInstanceResponse[]> { |
70 | | - const model = await this.stack.modelService.getOrBuildModel(); |
71 | | - let nodes: ModelNode[]; |
72 | | - |
73 | | - if (options?.rootOnly) { |
74 | | - nodes = model.rootIds |
75 | | - .map((id) => model.nodesById.get(id)) |
76 | | - .filter((n): n is ModelNode => n !== undefined && n.kind === 'instance'); |
77 | | - } else { |
78 | | - nodes = Array.from(model.nodesById.values()).filter( |
79 | | - (n): n is ModelNode => n.kind === 'instance' || n.kind === 'property', |
80 | | - ); |
81 | | - } |
82 | | - |
83 | | - if (options?.typeElementId) { |
84 | | - nodes = nodes.filter((n) => n.type === options.typeElementId); |
85 | | - } |
86 | | - |
87 | | - const rootSet = new Set(model.rootIds); |
88 | | - |
89 | | - return nodes.map((n) => ({ |
90 | | - elementId: n.id, |
91 | | - displayName: n.name, |
92 | | - typeElementId: n.type || 'BaseObjectType', |
93 | | - parentId: rootSet.has(n.id) ? null : undefined, |
94 | | - isComposition: n.kind === 'instance', |
95 | | - metadata: { |
96 | | - typeNamespaceUri: n.namespaceUri, |
97 | | - engUnit: n.engUnit || null, |
98 | | - sourceTypeId: n.sourceTypeId || null, |
99 | | - }, |
100 | | - })); |
101 | | - } |
102 | | - |
103 | | - async getObjectById(elementId: string): Promise<ObjectInstanceResponse | null> { |
104 | | - const objects = await this.getObjects(); |
105 | | - return objects.find((o) => o.elementId === elementId) || null; |
106 | | - } |
107 | | - |
108 | | - async getRelatedObjects(elementId: string): Promise<ObjectInstanceResponse[]> { |
109 | | - const model = await this.stack.modelService.getOrBuildModel(); |
110 | | - const node = model.nodesById.get(elementId); |
111 | | - if (!node?.children) return []; |
112 | | - |
113 | | - const children = node.children |
114 | | - .map((childId) => model.nodesById.get(childId)) |
115 | | - .filter((n): n is ModelNode => n !== undefined); |
116 | | - |
117 | | - return children.map((n) => ({ |
118 | | - elementId: n.id, |
119 | | - displayName: n.name, |
120 | | - typeElementId: n.type || 'BaseObjectType', |
121 | | - parentId: elementId, |
122 | | - isComposition: n.kind === 'instance', |
123 | | - metadata: { |
124 | | - typeNamespaceUri: n.namespaceUri, |
125 | | - engUnit: n.engUnit || null, |
126 | | - sourceTypeId: n.sourceTypeId || null, |
127 | | - }, |
128 | | - })); |
129 | | - } |
130 | | - |
131 | | - async getObjectValues( |
132 | | - elementIds: string[], |
133 | | - maxDepth = 1, |
134 | | - ): Promise<CurrentValueResult[]> { |
135 | | - const bulkResults = await this.stack.valueService.readValues(elementIds, maxDepth); |
136 | | - return bulkResults |
137 | | - .map((r) => r.result) |
138 | | - .filter((v): v is CurrentValueResult => v !== null && v !== undefined); |
| 1 | +import type { RestServerDeps } from '@node-i3x/rest-server'; |
| 2 | +import { RestClientBinding } from './rest-binding.js'; |
| 3 | + |
| 4 | +type InjectableApp = Awaited< |
| 5 | + ReturnType<typeof import('@node-i3x/rest-server').createApp> |
| 6 | +>; |
| 7 | + |
| 8 | +/** |
| 9 | + * Binding to an i3X stack running in this process. |
| 10 | + * |
| 11 | + * Requests go through the real rest-server routes (Fastify `inject()`, no socket), |
| 12 | + * so in-process answers are exactly what a remote node-i3x server would send: |
| 13 | + * same parentId, roots, related objects, values and server info. |
| 14 | + */ |
| 15 | +export class InProcessConnectorBinding extends RestClientBinding { |
| 16 | + constructor(deps: RestServerDeps) { |
| 17 | + super('http://in-process', undefined, 15_000, injectFetch(deps)); |
139 | 18 | } |
| 19 | +} |
140 | 20 |
|
141 | | - async getHistoricalValues( |
142 | | - elementIds: string[], |
143 | | - startTime: string, |
144 | | - endTime: string, |
145 | | - maxDepth = 1, |
146 | | - ): Promise<HistoricalValueResult[]> { |
147 | | - const start = startTime ? new Date(startTime) : null; |
148 | | - const end = endTime ? new Date(endTime) : null; |
149 | | - const bulkResults = await this.stack.historyService.readHistory( |
150 | | - elementIds, |
151 | | - start, |
152 | | - end, |
153 | | - maxDepth, |
| 21 | +/** A fetch that answers from an in-process rest-server instead of the network. */ |
| 22 | +export function injectFetch(deps: RestServerDeps): typeof fetch { |
| 23 | + let app: Promise<InjectableApp> | undefined; |
| 24 | + return async (input, init) => { |
| 25 | + // Loaded lazily so the REST-only CLI never pulls in Fastify. No request logging: |
| 26 | + // Fastify logs to stdout, which is the MCP stdio channel. |
| 27 | + app ??= import('@node-i3x/rest-server').then(({ createApp }) => |
| 28 | + createApp({ ...deps, requireAuth: false }, { logger: false }), |
154 | 29 | ); |
155 | | - return bulkResults |
156 | | - .map((r) => r.result) |
157 | | - .filter((v): v is HistoricalValueResult => v !== null && v !== undefined); |
158 | | - } |
159 | | - |
160 | | - async findSuggestions(unknownId: string): Promise<string[]> { |
161 | | - try { |
162 | | - const allObjects = await this.getObjects(); |
163 | | - const candidates = allObjects.map((o) => o.elementId); |
164 | | - return rankSuggestions(unknownId, candidates, 5, 0.4); |
165 | | - } catch { |
166 | | - return []; |
167 | | - } |
168 | | - } |
| 30 | + const url = new URL(String(input)); |
| 31 | + const res = await (await app).inject({ |
| 32 | + method: (init?.method ?? 'GET') as 'GET' | 'POST', |
| 33 | + url: `${url.pathname}${url.search}`, |
| 34 | + headers: init?.headers as Record<string, string> | undefined, |
| 35 | + payload: typeof init?.body === 'string' ? init.body : undefined, |
| 36 | + }); |
| 37 | + return new Response(res.body, { |
| 38 | + status: res.statusCode, |
| 39 | + headers: { |
| 40 | + 'content-type': String(res.headers['content-type'] ?? 'application/json'), |
| 41 | + }, |
| 42 | + }); |
| 43 | + }; |
169 | 44 | } |
0 commit comments