1172 lines · 40.1 KB · TypeScript
RawDownload
import { Effect, Match, Option, Schema } from "effect";import type { Layer } from "effect";import { HttpClient } from "effect/unstable/http"; import {  ConnectionId,  ConfiguredCredentialBinding,  type CredentialBindingRef,  definePlugin,  tool,  ScopeId,  SecretId,  SourceDetectionResult,  StorageError,  type PluginCtx,  type StorageFailure,  type ToolAnnotations,  type ToolRow,} from "@executor-js/sdk/core"; import {  headersToConfigValues,  type ConfigFileSink,  type GraphqlSourceConfig as GraphqlConfigEntry,} from "@executor-js/config"; import {  introspect,  parseIntrospectionJson,  type IntrospectionResult,  type IntrospectionType,  type IntrospectionField,  type IntrospectionTypeRef,} from "./introspect";import { extract } from "./extract";import { GraphqlIntrospectionError, GraphqlInvocationError } from "./errors";import { invokeWithLayer } from "./invoke";import {  graphqlSchema,  makeDefaultGraphqlStore,  type GraphqlStore,  type StoredGraphqlSource,  type StoredOperation,} from "./store";import {  ExtractedField,  GRAPHQL_OAUTH_CONNECTION_SLOT,  GraphqlCredentialInput as GraphqlCredentialInputSchema,  GraphqlSourceAuthInput as GraphqlSourceAuthInputSchema,  GraphqlSourceBindingInput,  GraphqlSourceBindingRef,  graphqlHeaderSlot,  graphqlQueryParamSlot,  OperationBinding,  type ConfiguredGraphqlCredentialValue,  type GraphqlCredentialInput,  type GraphqlSourceAuth,  type HeaderValue as HeaderValueValue,  type GraphqlSourceAuthInput,  type GraphqlSourceBindingValue,  type GraphqlOperationKind,} from "./types"; // ---------------------------------------------------------------------------// Plugin config// --------------------------------------------------------------------------- export type HeaderValue = HeaderValueValue;export type GraphqlCredentialValue = ConfiguredGraphqlCredentialValue; export interface GraphqlSourceConfig {  /** The GraphQL endpoint URL */  readonly endpoint: string;  /**   * Executor scope id that owns this source row. Must be one of the   * executor's configured scopes. Typical shape: an admin adds the   * source at the outermost (organization) scope so it's visible to   * every inner (per-user) scope via fall-through reads.   */  readonly scope: string;  /** Display name for the source. Falls back to namespace if not provided. */  readonly name?: string;  /** Optional: introspection JSON text (if endpoint doesn't support introspection) */  readonly introspectionJson?: string;  /** Namespace for the tools (derived from endpoint if not provided) */  readonly namespace?: string;  /** Headers applied to every request. Direct secrets are rewritten to slots. */  readonly headers?: Record<string, GraphqlCredentialInput>;  /** Query parameters applied to every request. Direct secrets are rewritten to slots. */  readonly queryParams?: Record<string, GraphqlCredentialInput>;  /**   * Scope that owns any direct credentials supplied on this call. Required   * whenever headers/queryParams/auth carry direct secret or connection ids.   */  readonly credentialTargetScope?: string;  /** Optional OAuth2 credential used as a Bearer token for every request. */  readonly auth?: GraphqlSourceAuthInput;} const StaticAddSourceInputSchema = Schema.Struct({  scope: Schema.String,  endpoint: Schema.String,  name: Schema.optional(Schema.String),  introspectionJson: Schema.optional(Schema.String),  namespace: Schema.optional(Schema.String),  headers: Schema.optional(Schema.Record(Schema.String, GraphqlCredentialInputSchema)),  queryParams: Schema.optional(Schema.Record(Schema.String, GraphqlCredentialInputSchema)),  credentialTargetScope: Schema.optional(Schema.String),  auth: Schema.optional(GraphqlSourceAuthInputSchema),}); const StaticAddSourceInputStandardSchema = Schema.toStandardSchemaV1(  Schema.toStandardJSONSchemaV1(StaticAddSourceInputSchema),);const StaticAddSourceOutputStandardSchema = Schema.toStandardSchemaV1(  Schema.toStandardJSONSchemaV1(Schema.Struct({ toolCount: Schema.Number })),); // ---------------------------------------------------------------------------// Plugin extension// --------------------------------------------------------------------------- export interface GraphqlUpdateSourceInput {  readonly name?: string;  readonly endpoint?: string;  readonly headers?: Record<string, GraphqlCredentialInput>;  readonly queryParams?: Record<string, GraphqlCredentialInput>;  readonly credentialTargetScope?: string;  readonly auth?: GraphqlSourceAuthInput;} // ---------------------------------------------------------------------------// Helpers// --------------------------------------------------------------------------- /** Match `token` as a separator-bounded run inside a URL hostname or path, *  used as a low-confidence detection hint when introspection fails. *  Boundary chars are everything non-alphanumeric, so `/api/graphql`, *  `graphql.example.com`, `graphql-api`, and `graphql_v2` all match while *  `graphqlserver` and `/graphqlite` do not. */const urlMatchesToken = (url: URL, token: string): boolean => {  const re = new RegExp(`(?:^|[^a-z0-9])${token}(?:$|[^a-z0-9])`, "i");  return re.test(url.hostname) || re.test(url.pathname);}; /** Derive a namespace from an endpoint URL */const namespaceFromEndpoint = (endpoint: string): string => {  // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: URL construction throws; this helper intentionally falls back to the stable default namespace  try {    const url = new URL(endpoint);    return url.hostname.replace(/[^a-z0-9]+/gi, "_").toLowerCase();  } catch {    return "graphql";  }}; const formatTypeRef = (ref: IntrospectionTypeRef): string =>  Match.value(ref.kind).pipe(    Match.when("NON_NULL", () => (ref.ofType ? `${formatTypeRef(ref.ofType)}!` : "Unknown!")),    Match.when("LIST", () => (ref.ofType ? `[${formatTypeRef(ref.ofType)}]` : "[Unknown]")),    Match.option,    Option.getOrElse(() => ref.name ?? "Unknown"),  ); const unwrapTypeName = (ref: IntrospectionTypeRef): string => {  if (ref.name) return ref.name;  if (ref.ofType) return unwrapTypeName(ref.ofType);  return "Unknown";}; const buildSelectionSet = (  ref: IntrospectionTypeRef,  types: ReadonlyMap<string, IntrospectionType>,  depth: number,  seen: Set<string>,): string => {  if (depth > 2) return "";   const leafName = unwrapTypeName(ref);  if (seen.has(leafName)) return "";   const objectType = types.get(leafName);  if (!objectType?.fields) return "";   const kind = objectType.kind;  if (kind === "SCALAR" || kind === "ENUM") return "";   seen.add(leafName);   const subFields = objectType.fields    .filter((f) => !f.name.startsWith("__"))    .slice(0, 12)    .map((f) => {      const sub = buildSelectionSet(f.type, types, depth + 1, seen);      return sub ? `${f.name} ${sub}` : f.name;    });   seen.delete(leafName);   return subFields.length > 0 ? `{ ${subFields.join(" ")} }` : "";}; const buildOperationStringForField = (  kind: GraphqlOperationKind,  field: IntrospectionField,  types: ReadonlyMap<string, IntrospectionType>,): string => {  const opType = kind === "query" ? "query" : "mutation";   const varDefs = field.args.map((arg) => {    const typeName = formatTypeRef(arg.type);    return `$${arg.name}: ${typeName}`;  });   const argPasses = field.args.map((arg) => `${arg.name}: $${arg.name}`);  const selectionSet = buildSelectionSet(field.type, types, 0, new Set());   const varDefsStr = varDefs.length > 0 ? `(${varDefs.join(", ")})` : "";  const argPassStr = argPasses.length > 0 ? `(${argPasses.join(", ")})` : "";   return `${opType}${varDefsStr} { ${field.name}${argPassStr}${selectionSet ? ` ${selectionSet}` : ""} }`;}; interface PreparedOperation {  readonly toolPath: string;  readonly description: string;  readonly inputSchema: unknown;  readonly binding: OperationBinding;} const prepareOperations = (  fields: readonly ExtractedField[],  introspection: IntrospectionResult,): readonly PreparedOperation[] => {  const typeMap = new Map<string, IntrospectionType>();  for (const t of introspection.__schema.types) {    typeMap.set(t.name, t);  }   const fieldMap = new Map<string, { kind: GraphqlOperationKind; field: IntrospectionField }>();  const schema = introspection.__schema;  for (const rootKind of ["query", "mutation"] as const) {    const typeName = rootKind === "query" ? schema.queryType?.name : schema.mutationType?.name;    if (!typeName) continue;    const rootType = typeMap.get(typeName);    if (!rootType?.fields) continue;    for (const f of rootType.fields) {      if (!f.name.startsWith("__")) {        fieldMap.set(`${rootKind}.${f.name}`, { kind: rootKind, field: f });      }    }  }   return fields.map((extracted) => {    const prefix = extracted.kind === "mutation" ? "mutation" : "query";    const toolPath = `${prefix}.${extracted.fieldName}`;    const description = Option.getOrElse(      extracted.description,      () => `GraphQL ${extracted.kind}: ${extracted.fieldName} -> ${extracted.returnTypeName}`,    );     const key = `${extracted.kind}.${extracted.fieldName}`;    const entry = fieldMap.get(key);    const operationString = entry      ? buildOperationStringForField(entry.kind, entry.field, typeMap)      : `${extracted.kind} { ${extracted.fieldName} }`;     const binding = OperationBinding.make({      kind: extracted.kind,      fieldName: extracted.fieldName,      operationString,      variableNames: extracted.arguments.map((a) => a.name),    });     return {      toolPath,      description,      inputSchema: Option.getOrUndefined(extracted.inputSchema),      binding,    };  });}; const annotationsFor = (binding: OperationBinding): ToolAnnotations => {  if (binding.kind === "mutation") {    return {      requiresApproval: true,      approvalDescription: `mutation ${binding.fieldName}`,    };  }  return {};}; // ---------------------------------------------------------------------------// Plugin factory// --------------------------------------------------------------------------- export interface GraphqlPluginOptions {  readonly httpClientLayer?: Layer.Layer<HttpClient.HttpClient>;  /** If provided, source add/remove is mirrored to executor.jsonc   *  (best-effort — file errors are logged, not raised). */  readonly configFile?: ConfigFileSink;} const toGraphqlConfigEntry = (  namespace: string,  config: GraphqlSourceConfig,): GraphqlConfigEntry => {  const headers: Record<string, HeaderValue> = {};  for (const [name, value] of Object.entries(config.headers ?? {})) {    if (typeof value === "string" || !("kind" in value)) {      headers[name] = value;    }  }  return {    kind: "graphql",    endpoint: config.endpoint,    introspectionJson: config.introspectionJson,    namespace,    headers: headersToConfigValues(Object.keys(headers).length > 0 ? headers : undefined),  };}; const GRAPHQL_PLUGIN_ID = "graphql"; const scopeRanks = (ctx: PluginCtx<GraphqlStore>): ReadonlyMap<string, number> =>  new Map(ctx.scopes.map((scope, index) => [String(scope.id), index])); const scopeRank = (ranks: ReadonlyMap<string, number>, scopeId: string): number =>  ranks.get(scopeId) ?? Infinity; const coreBindingToGraphqlBinding = (binding: CredentialBindingRef): GraphqlSourceBindingRef =>  GraphqlSourceBindingRef.make({    sourceId: binding.sourceId,    sourceScopeId: binding.sourceScopeId,    scopeId: binding.scopeId,    slot: binding.slotKey,    value: binding.value,    createdAt: binding.createdAt,    updatedAt: binding.updatedAt,  }); const listGraphqlSourceBindings = (  ctx: PluginCtx<GraphqlStore>,  sourceId: string,  sourceScope: string,): Effect.Effect<readonly GraphqlSourceBindingRef[], StorageFailure> =>  Effect.gen(function* () {    const ranks = scopeRanks(ctx);    const sourceSourceRank = scopeRank(ranks, sourceScope);    if (sourceSourceRank === Infinity) return [];    const bindings = yield* ctx.credentialBindings.listForSource({      pluginId: GRAPHQL_PLUGIN_ID,      sourceId,      sourceScope: ScopeId.make(sourceScope),    });    return bindings      .filter((binding) => scopeRank(ranks, binding.scopeId) <= sourceSourceRank)      .map(coreBindingToGraphqlBinding);  }); const resolveGraphqlSourceBinding = (  ctx: PluginCtx<GraphqlStore>,  sourceId: string,  sourceScope: string,  slot: string,): Effect.Effect<GraphqlSourceBindingRef | null, StorageFailure> =>  Effect.gen(function* () {    const ranks = scopeRanks(ctx);    const sourceSourceRank = scopeRank(ranks, sourceScope);    if (sourceSourceRank === Infinity) return null;    const bindings = yield* ctx.credentialBindings.listForSource({      pluginId: GRAPHQL_PLUGIN_ID,      sourceId,      sourceScope: ScopeId.make(sourceScope),    });    const binding = bindings      .filter(        (candidate) =>          candidate.slotKey === slot && scopeRank(ranks, candidate.scopeId) <= sourceSourceRank,      )      .sort((a, b) => scopeRank(ranks, a.scopeId) - scopeRank(ranks, b.scopeId))[0];    return binding ? coreBindingToGraphqlBinding(binding) : null;  }); const validateGraphqlBindingTarget = (  ctx: PluginCtx<GraphqlStore>,  input: {    readonly sourceScope: string;    readonly targetScope: string;    readonly sourceId: string;  },): Effect.Effect<void, StorageFailure> =>  Effect.gen(function* () {    const ranks = scopeRanks(ctx);    const sourceSourceRank = scopeRank(ranks, input.sourceScope);    const targetRank = scopeRank(ranks, input.targetScope);    const scopeList = `[${ctx.scopes.map((s) => s.id).join(", ")}]`;    if (sourceSourceRank === Infinity) {      return yield* new StorageError({        message:          `GraphQL source binding references source scope "${input.sourceScope}" ` +          `which is not in the executor's scope stack ${scopeList}.`,        cause: undefined,      });    }    if (targetRank === Infinity) {      return yield* new StorageError({        message:          `GraphQL source binding targets scope "${input.targetScope}" which is not ` +          `in the executor's scope stack ${scopeList}.`,        cause: undefined,      });    }    if (targetRank > sourceSourceRank) {      return yield* new StorageError({        message:          `GraphQL source bindings for "${input.sourceId}" cannot be written at ` +          `outer scope "${input.targetScope}" because the base source lives at ` +          `"${input.sourceScope}"`,        cause: undefined,      });    }  }); const bindingTargetScope = (  targetScope: string | undefined,  bindings: readonly unknown[],): Effect.Effect<string | undefined, GraphqlIntrospectionError> => {  if (bindings.length === 0) return Effect.succeed(undefined);  if (targetScope) return Effect.succeed(targetScope);  return Effect.fail(    new GraphqlIntrospectionError({      message: "credentialTargetScope is required when adding direct GraphQL credentials",    }),  );}; const targetScopeForBinding = (  fallbackTargetScope: string | undefined,  binding: { readonly targetScope?: string },): Effect.Effect<string, GraphqlIntrospectionError> => {  const targetScope = binding.targetScope ?? fallbackTargetScope;  if (targetScope) return Effect.succeed(targetScope);  return Effect.fail(    new GraphqlIntrospectionError({      message: "credentialTargetScope is required when adding direct GraphQL credentials",    }),  );}; const canonicalizeCredentialMap = (  values: Record<string, GraphqlCredentialInput> | undefined,  slotForName: (name: string) => string,): {  readonly values: Record<string, ConfiguredGraphqlCredentialValue>;  readonly bindings: ReadonlyArray<{    readonly slot: string;    readonly value: GraphqlSourceBindingValue;    readonly targetScope?: string;  }>;} => {  const nextValues: Record<string, ConfiguredGraphqlCredentialValue> = {};  const bindings: Array<{    slot: string;    value: GraphqlSourceBindingValue;    targetScope?: string;  }> = [];  for (const [name, value] of Object.entries(values ?? {})) {    if (typeof value === "string") {      nextValues[name] = value;      continue;    }    if ("kind" in value) {      nextValues[name] = value;      continue;    }    const slot = slotForName(name);    nextValues[name] = ConfiguredCredentialBinding.make({      kind: "binding",      slot,      prefix: value.prefix,    });    bindings.push({      slot,      targetScope: "targetScope" in value ? value.targetScope : undefined,      value: {        kind: "secret",        secretId: SecretId.make(value.secretId),        ...("secretScopeId" in value && value.secretScopeId          ? { secretScopeId: value.secretScopeId }          : {}),      },    });  }  return { values: nextValues, bindings };}; const canonicalizeAuth = (  auth: GraphqlSourceAuthInput | undefined,

Lines 1–500 of 1172.