Open History to see the last commit
1import type { AnyHarness, HarnessHost, HarnessUser } from '@tanstack/ai-harness'
2import type { OpenAccess } from '@tanstack/ai-harness/server'
3
4// The packages behind `--acp`, `--mcp`, and `/mcp` are optional. The
5// specifier is a variable, so a build or a type check never needs them, and
6// a version without harness support is the same as none.
7
8/**
9 * Import `specifier` and check that it has the functions in `names`.
10 * Resolves to `undefined` when the package is not installed, when its
11 * version does not export `specifier`, or when it has no such functions.
12 * Any other error (a syntax error, or a failing import inside the module)
13 * throws, with `specifier` in the message.
14 */
15export async function loadOptional<T>(
16 specifier: string,
17 ...names: Array<keyof T & string>
18) {
19 // Resolve first: a resolve error is about `specifier` itself, so its code
20 // alone says "not installed". A missing package that the module imports
21 // fails the import below, and throws.
22 try {
23 import.meta.resolve(specifier)
24 } catch (error) {
25 if (isMissing(error)) return undefined
26 throw loadError(specifier, error)
27 }
28 let loaded: unknown
29 try {
30 loaded = await import(/* @vite-ignore */ specifier)
31 } catch (error) {
32 throw loadError(specifier, error)
33 }
34 return hasFunctions<T>(loaded, names) ? loaded : undefined
35}
36
37/** True when a resolve error says that the package or its export is not there. */
38function isMissing(error: unknown) {
39 if (!(error instanceof Error) || !('code' in error)) return false
40 return (
41 error.code === 'ERR_MODULE_NOT_FOUND' ||
42 error.code === 'MODULE_NOT_FOUND' ||
43 error.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED'
44 )
45}
46
47function loadError(specifier: string, error: unknown) {
48 const reason = error instanceof Error ? error.message : String(error)
49 return new Error(`Could not load ${specifier}: ${reason}`, { cause: error })
50}
51
52function hasFunctions<T>(
53 value: unknown,
54 names: ReadonlyArray<keyof T & string>,
55): value is T {
56 return (
57 typeof value === 'object' &&
58 value !== null &&
59 names.every((name) => typeof Reflect.get(value, name) === 'function')
60 )
61}
62
63/** A server that answers MCP requests, from `@tanstack/ai-mcp/harness`. */
64export interface McpFetchServer {
65 fetch: (request: Request) => Promise<Response>
66}
67
68/** `@tanstack/ai-mcp/harness` */
69export interface McpHarnessModule {
70 createHarnessMcpServer: <U extends HarnessUser>(options: {
71 host: HarnessHost<U>
72 harness: AnyHarness
73 /** For a host without access: who the calls run as. */
74 openAccess?: OpenAccess<U>
75 threadId?: string
76 approvals?: 'ask' | 'auto'
77 filePaths?: Array<string>
78 }) => Promise<McpFetchServer>
79}
80
81/** `@tanstack/ai-mcp/server/stdio` */
82export interface McpStdioModule {
83 serveMCPStdio: (server: McpFetchServer) => { close: () => unknown }
84}
85
86/** `@tanstack/ai-acp/agent` */
87export interface AcpAgentModule {
88 serveAcp: <U extends HarnessUser>(options: {
89 host: HarnessHost<U>
90 harness: AnyHarness
91 /** The local user. Required on a host with access. */
92 user?: U
93 }) => {
94 closed: Promise<unknown>
95 close: () => void
96 }
97}