MCPcopy
hub / github.com/TanStack/ai / validateCapabilities

Function validateCapabilities

packages/ai/src/activities/chat/middleware/validate.ts:20–55  ·  view source on GitHub ↗
(
  middlewares: ReadonlyArray<AnyChatMiddleware>,
  adapter: CapabilityRequiringAdapter,
)

Source from the content-addressed store, hash-verified

18 * a not-yet-provided capability during `setup` fails loud via its getter.
19 */
20export function validateCapabilities(
21 middlewares: ReadonlyArray<AnyChatMiddleware>,
22 adapter: CapabilityRequiringAdapter,
23): void {
24 const provided = new Set<CapabilityHandle>()
25 for (const mw of middlewares) {
26 for (const handle of mw.provides ?? []) provided.add(handle)
27 }
28
29 const providedNames = (): string => {
30 const names = [...provided].map((h) => h.capabilityName)
31 return names.length ? names.join(', ') : 'none'
32 }
33
34 for (const handle of adapter.requires ?? []) {
35 if (!provided.has(handle)) {
36 throw new Error(
37 `Adapter "${adapter.name}" requires capability "${handle.capabilityName}". ` +
38 `Provided capabilities: ${providedNames()}. ` +
39 `Add a middleware that provides "${handle.capabilityName}".`,
40 )
41 }
42 }
43
44 for (const mw of middlewares) {
45 for (const handle of mw.requires ?? []) {
46 if (!provided.has(handle)) {
47 throw new Error(
48 `Middleware "${mw.name ?? 'unnamed'}" requires capability ` +
49 `"${handle.capabilityName}". Provided capabilities: ${providedNames()}. ` +
50 `Add a middleware that provides "${handle.capabilityName}".`,
51 )
52 }
53 }
54 }
55}

Callers 2

chatFunction · 0.90

Calls 2

providedNamesFunction · 0.85
hasMethod · 0.80

Tested by

no test coverage detected