@sriinnu/fiche 0.0.0-stage → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +134 -0
- package/LICENSE +22 -0
- package/NOTICE +13 -0
- package/PRIVACY.md +41 -0
- package/README.md +288 -2
- package/SECURITY.md +53 -0
- package/assets/JETBRAINS_MONO_LICENSE.txt +89 -0
- package/assets/SPLEEN_LICENSE.txt +24 -0
- package/assets/UNIFONT_LICENSE.txt +476 -0
- package/assets/fiche-logo-dark.svg +61 -0
- package/assets/fiche-logo-light.svg +61 -0
- package/assets/fiche-logo.svg +71 -0
- package/assets/fiche-mark.svg +48 -0
- package/bin/cli.js +7 -0
- package/bin/mcp.js +8 -0
- package/dist/calibrate.d.ts +30 -0
- package/dist/calibrate.js +588 -0
- package/dist/core/anthropic-vision.d.ts +28 -0
- package/dist/core/anthropic-vision.js +34 -0
- package/dist/core/applicability.d.ts +63 -0
- package/dist/core/applicability.js +151 -0
- package/dist/core/atlas-gray-jbmono10.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono10.js +64 -0
- package/dist/core/atlas-gray-jbmono12.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono12.js +64 -0
- package/dist/core/atlas-gray-jbmono14.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono14.js +64 -0
- package/dist/core/atlas-gray.d.ts +26 -0
- package/dist/core/atlas-gray.js +64 -0
- package/dist/core/atlas-jbmono10.d.ts +34 -0
- package/dist/core/atlas-jbmono10.js +71 -0
- package/dist/core/atlas-jbmono12.d.ts +34 -0
- package/dist/core/atlas-jbmono12.js +71 -0
- package/dist/core/atlas-jbmono14.d.ts +34 -0
- package/dist/core/atlas-jbmono14.js +71 -0
- package/dist/core/atlas.d.ts +34 -0
- package/dist/core/atlas.js +71 -0
- package/dist/core/baseline.d.ts +121 -0
- package/dist/core/baseline.js +200 -0
- package/dist/core/calibration.d.ts +86 -0
- package/dist/core/calibration.js +547 -0
- package/dist/core/casi-delta.d.ts +63 -0
- package/dist/core/casi-delta.js +1271 -0
- package/dist/core/claude-model-profiles.d.ts +53 -0
- package/dist/core/claude-model-profiles.js +160 -0
- package/dist/core/client-tool-round.d.ts +31 -0
- package/dist/core/client-tool-round.js +58 -0
- package/dist/core/codex-cache.d.ts +20 -0
- package/dist/core/codex-cache.js +59 -0
- package/dist/core/context-mmu.d.ts +350 -0
- package/dist/core/context-mmu.js +3028 -0
- package/dist/core/cpt-fit.d.ts +70 -0
- package/dist/core/cpt-fit.js +197 -0
- package/dist/core/env.d.ts +8 -0
- package/dist/core/env.js +15 -0
- package/dist/core/export.d.ts +127 -0
- package/dist/core/export.js +385 -0
- package/dist/core/factsheet.d.ts +77 -0
- package/dist/core/factsheet.js +494 -0
- package/dist/core/gemini-model-profiles.d.ts +20 -0
- package/dist/core/gemini-model-profiles.js +134 -0
- package/dist/core/google.d.ts +76 -0
- package/dist/core/google.js +1380 -0
- package/dist/core/gpt-model-profiles.d.ts +218 -0
- package/dist/core/gpt-model-profiles.js +570 -0
- package/dist/core/gpt-tokens.d.ts +14 -0
- package/dist/core/gpt-tokens.js +165 -0
- package/dist/core/history.d.ts +197 -0
- package/dist/core/history.js +1003 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +16 -0
- package/dist/core/library.d.ts +80 -0
- package/dist/core/library.js +131 -0
- package/dist/core/measurement.d.ts +34 -0
- package/dist/core/measurement.js +280 -0
- package/dist/core/messages-chat-bridge.d.ts +23 -0
- package/dist/core/messages-chat-bridge.js +747 -0
- package/dist/core/messages-responses-bridge.d.ts +11 -0
- package/dist/core/messages-responses-bridge.js +893 -0
- package/dist/core/openai-history.d.ts +184 -0
- package/dist/core/openai-history.js +1279 -0
- package/dist/core/openai-savings.d.ts +57 -0
- package/dist/core/openai-savings.js +116 -0
- package/dist/core/openai.d.ts +68 -0
- package/dist/core/openai.js +2523 -0
- package/dist/core/pin.d.ts +199 -0
- package/dist/core/pin.js +1353 -0
- package/dist/core/png.d.ts +14 -0
- package/dist/core/png.js +188 -0
- package/dist/core/prefix-tape.d.ts +102 -0
- package/dist/core/prefix-tape.js +315 -0
- package/dist/core/price-id.d.ts +10 -0
- package/dist/core/price-id.js +26 -0
- package/dist/core/prices.generated.d.ts +36 -0
- package/dist/core/prices.generated.js +369 -0
- package/dist/core/pricing.d.ts +134 -0
- package/dist/core/pricing.js +335 -0
- package/dist/core/profile-base.d.ts +30 -0
- package/dist/core/profile-base.js +67 -0
- package/dist/core/provider-router.d.ts +51 -0
- package/dist/core/provider-router.js +121 -0
- package/dist/core/proxy.d.ts +252 -0
- package/dist/core/proxy.js +2929 -0
- package/dist/core/reader-tier.d.ts +24 -0
- package/dist/core/reader-tier.js +118 -0
- package/dist/core/render.d.ts +280 -0
- package/dist/core/render.js +1302 -0
- package/dist/core/request-guard.d.ts +31 -0
- package/dist/core/request-guard.js +126 -0
- package/dist/core/request-transform.d.ts +43 -0
- package/dist/core/request-transform.js +173 -0
- package/dist/core/schema-compiler.d.ts +64 -0
- package/dist/core/schema-compiler.js +1696 -0
- package/dist/core/schema-strip.d.ts +29 -0
- package/dist/core/schema-strip.js +173 -0
- package/dist/core/session-state.d.ts +219 -0
- package/dist/core/session-state.js +929 -0
- package/dist/core/stage-plugins.d.ts +45 -0
- package/dist/core/stage-plugins.js +205 -0
- package/dist/core/text-history.d.ts +74 -0
- package/dist/core/text-history.js +612 -0
- package/dist/core/text-hygiene.d.ts +160 -0
- package/dist/core/text-hygiene.js +2259 -0
- package/dist/core/toon.d.ts +29 -0
- package/dist/core/toon.js +425 -0
- package/dist/core/tracker.d.ts +223 -0
- package/dist/core/tracker.js +290 -0
- package/dist/core/transform.d.ts +589 -0
- package/dist/core/transform.js +3153 -0
- package/dist/core/types.d.ts +87 -0
- package/dist/core/types.js +8 -0
- package/dist/core/upstream-hosts.d.ts +127 -0
- package/dist/core/upstream-hosts.js +398 -0
- package/dist/core/vision-cost.d.ts +63 -0
- package/dist/core/vision-cost.js +137 -0
- package/dist/dashboard/fragments.d.ts +87 -0
- package/dist/dashboard/fragments.js +1515 -0
- package/dist/dashboard/types.d.ts +217 -0
- package/dist/dashboard/types.js +3 -0
- package/dist/dashboard/vendor.d.ts +3 -0
- package/dist/dashboard/vendor.js +6 -0
- package/dist/dashboard.d.ts +372 -0
- package/dist/dashboard.js +2731 -0
- package/dist/export-collect.d.ts +36 -0
- package/dist/export-collect.js +59 -0
- package/dist/file-tracker.d.ts +42 -0
- package/dist/file-tracker.js +234 -0
- package/dist/kosha-sync.d.ts +74 -0
- package/dist/kosha-sync.js +272 -0
- package/dist/launchd-socket.d.ts +22 -0
- package/dist/launchd-socket.js +210 -0
- package/dist/mcp/cli.d.ts +2 -0
- package/dist/mcp/cli.js +55 -0
- package/dist/mcp/server.d.ts +48 -0
- package/dist/mcp/server.js +987 -0
- package/dist/mmu-store.d.ts +31 -0
- package/dist/mmu-store.js +179 -0
- package/dist/node-fetch.d.ts +22 -0
- package/dist/node-fetch.js +489 -0
- package/dist/node.d.ts +9 -0
- package/dist/node.js +244396 -0
- package/dist/request-decoding.d.ts +30 -0
- package/dist/request-decoding.js +85 -0
- package/dist/sessions.d.ts +179 -0
- package/dist/sessions.js +837 -0
- package/dist/stats.d.ts +114 -0
- package/dist/stats.js +439 -0
- package/dist/turn-report.d.ts +65 -0
- package/dist/turn-report.js +724 -0
- package/dist/warp/app-launch.d.ts +26 -0
- package/dist/warp/app-launch.js +155 -0
- package/dist/warp/ca.d.ts +74 -0
- package/dist/warp/ca.js +416 -0
- package/dist/warp/connect.d.ts +42 -0
- package/dist/warp/connect.js +686 -0
- package/dist/warp/der.d.ts +57 -0
- package/dist/warp/der.js +151 -0
- package/dist/warp/index.d.ts +79 -0
- package/dist/warp/index.js +650 -0
- package/dist/warp/resolve.d.ts +12 -0
- package/dist/warp/resolve.js +49 -0
- package/dist/warp/route.d.ts +71 -0
- package/dist/warp/route.js +134 -0
- package/dist/worker.d.ts +71 -0
- package/dist/worker.js +174 -0
- package/docs/SECURITY_MODEL.md +84 -0
- package/package.json +122 -4
- package/plugins/fiche-companion/README.md +42 -0
- package/plugins/fiche-companion/assets/logo.svg +48 -0
- package/plugins/fiche-companion/hooks.json +11 -0
- package/plugins/fiche-companion/mcp_config.json +8 -0
- package/plugins/fiche-companion/plugin.json +18 -0
- package/plugins/fiche-companion/scripts/claude-statusline.mjs +35 -0
- package/plugins/fiche-companion/scripts/post-invocation.mjs +122 -0
- package/plugins/fiche-companion/scripts/resolve-turn-report.mjs +132 -0
- package/plugins/fiche-companion/skills/fiche/SKILL.md +61 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal Anthropic Messages API request types — only the fields inkling
|
|
3
|
+
* actually reads or rewrites. Anything else passes through untouched.
|
|
4
|
+
*
|
|
5
|
+
* Shape reference: https://docs.anthropic.com/en/api/messages
|
|
6
|
+
*/
|
|
7
|
+
export interface TextBlock {
|
|
8
|
+
type: 'text';
|
|
9
|
+
text: string;
|
|
10
|
+
cache_control?: CacheControl;
|
|
11
|
+
}
|
|
12
|
+
export interface ImageBlock {
|
|
13
|
+
type: 'image';
|
|
14
|
+
source: {
|
|
15
|
+
type: 'base64';
|
|
16
|
+
media_type: 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp';
|
|
17
|
+
data: string;
|
|
18
|
+
};
|
|
19
|
+
cache_control?: CacheControl;
|
|
20
|
+
}
|
|
21
|
+
export interface ToolUseBlock {
|
|
22
|
+
type: 'tool_use';
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
input: unknown;
|
|
26
|
+
}
|
|
27
|
+
export interface ToolResultBlock {
|
|
28
|
+
type: 'tool_result';
|
|
29
|
+
tool_use_id: string;
|
|
30
|
+
content: string | Array<TextBlock | ImageBlock>;
|
|
31
|
+
is_error?: boolean;
|
|
32
|
+
cache_control?: CacheControl;
|
|
33
|
+
}
|
|
34
|
+
export type ContentBlock = TextBlock | ImageBlock | ToolUseBlock | ToolResultBlock;
|
|
35
|
+
export interface CacheControl {
|
|
36
|
+
type: 'ephemeral';
|
|
37
|
+
ttl?: '5m' | '1h';
|
|
38
|
+
}
|
|
39
|
+
export interface Message {
|
|
40
|
+
role: 'user' | 'assistant';
|
|
41
|
+
content: string | ContentBlock[];
|
|
42
|
+
}
|
|
43
|
+
export interface ToolDef {
|
|
44
|
+
/** Absent or "custom" = client-defined tool (description/input_schema allowed).
|
|
45
|
+
* Any other value is a native server-side tool with a fixed API schema that
|
|
46
|
+
* rejects extra fields — inkling must pass those entries through untouched (#43). */
|
|
47
|
+
type?: string;
|
|
48
|
+
name: string;
|
|
49
|
+
description?: string;
|
|
50
|
+
input_schema?: unknown;
|
|
51
|
+
cache_control?: CacheControl;
|
|
52
|
+
}
|
|
53
|
+
export type SystemField = string | Array<TextBlock | ImageBlock>;
|
|
54
|
+
/** Anthropic token usage block — same shape on streaming (message_start) and non-streaming. */
|
|
55
|
+
export interface Usage {
|
|
56
|
+
input_tokens?: number;
|
|
57
|
+
output_tokens?: number;
|
|
58
|
+
cache_creation_input_tokens?: number;
|
|
59
|
+
cache_read_input_tokens?: number;
|
|
60
|
+
/** Split by tier (5m=1.25×, 1h=2× base) for honest cost; absent on older API / non-cache requests. */
|
|
61
|
+
cache_creation?: {
|
|
62
|
+
ephemeral_5m_input_tokens?: number;
|
|
63
|
+
ephemeral_1h_input_tokens?: number;
|
|
64
|
+
};
|
|
65
|
+
/** Server tool billing (web search = per-request, not per-token); absent when unused. */
|
|
66
|
+
server_tool_use?: {
|
|
67
|
+
web_search_requests?: number;
|
|
68
|
+
};
|
|
69
|
+
/** OpenAI prompt-cache hits — the subset of `input_tokens` served from cache
|
|
70
|
+
* (billed at ~0.1× for the gpt-5 family). Mapped from `input_tokens_details`
|
|
71
|
+
* / `prompt_tokens_details.cached_tokens`. Anthropic uses cache_read instead. */
|
|
72
|
+
cached_tokens?: number;
|
|
73
|
+
/** OpenAI-compatible prompt-cache writes — diagnostic subset of `input_tokens`,
|
|
74
|
+
* never added to the input total. Anthropic uses cache_creation instead. */
|
|
75
|
+
cache_write_tokens?: number;
|
|
76
|
+
/** Reasoning tokens — subset of `output_tokens`. Mapped from
|
|
77
|
+
* `output_tokens_details` / `completion_tokens_details.reasoning_tokens`. */
|
|
78
|
+
reasoning_output_tokens?: number;
|
|
79
|
+
}
|
|
80
|
+
export interface MessagesRequest {
|
|
81
|
+
model: string;
|
|
82
|
+
messages: Message[];
|
|
83
|
+
system?: SystemField;
|
|
84
|
+
tools?: ToolDef[];
|
|
85
|
+
[k: string]: unknown;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal Anthropic Messages API request types — only the fields inkling
|
|
3
|
+
* actually reads or rewrites. Anything else passes through untouched.
|
|
4
|
+
*
|
|
5
|
+
* Shape reference: https://docs.anthropic.com/en/api/messages
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider hosts inkling knows, and the "follow the agent" resolver.
|
|
3
|
+
*
|
|
4
|
+
* Following means: forward a request to the host the agent was actually talking
|
|
5
|
+
* to, instead of to whichever upstream inkling's env vars name. It applies to
|
|
6
|
+
* third-party (`compat`) hosts only. First-party hosts keep the configured
|
|
7
|
+
* routing (ANTHROPIC/OPENAI/Google upstreams, gateways, bridges), so nothing
|
|
8
|
+
* that works today changes.
|
|
9
|
+
*
|
|
10
|
+
* Two ways in, one registry:
|
|
11
|
+
* - Host: under `inkling warp` the agent still addresses its own provider and
|
|
12
|
+
* warp keeps that Host when diverting into inkling.
|
|
13
|
+
* - Path: `http://127.0.0.1:47821/to/<host>/<rest>` for agents configured by
|
|
14
|
+
* base URL, so one inkling serves many providers with no per-provider env.
|
|
15
|
+
*
|
|
16
|
+
* Both can only ever select a registry host, over https on 443, so neither is an
|
|
17
|
+
* open proxy: an arbitrary host, port, IP literal or local name is refused or
|
|
18
|
+
* left to legacy routing. The followed request carries only the agent's own
|
|
19
|
+
* credential; configured keys stay bound to their configured upstreams (see
|
|
20
|
+
* proxy.ts).
|
|
21
|
+
*/
|
|
22
|
+
export type UpstreamFamily = 'anthropic' | 'openai' | 'google' | 'compat';
|
|
23
|
+
export type ProviderId = 'anthropic' | 'openai' | 'google' | 'deepseek' | 'kimi' | 'glm' | 'minimax' | 'qwen' | 'openrouter' | 'xai' | 'groq' | 'together' | 'fireworks' | 'cerebras' | 'mistral' | 'perplexity' | 'cohere' | 'sambanova' | 'github' | 'azure' | 'vercel';
|
|
24
|
+
export type ProviderTier = 'first-party' | 'third-party';
|
|
25
|
+
export interface ProviderDescriptor {
|
|
26
|
+
readonly id: ProviderId;
|
|
27
|
+
readonly label: string;
|
|
28
|
+
readonly tier: ProviderTier;
|
|
29
|
+
readonly defaultEnabled: boolean;
|
|
30
|
+
readonly hosts: readonly string[];
|
|
31
|
+
readonly aliases?: readonly string[];
|
|
32
|
+
readonly envKeys?: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
export interface ProviderStatus {
|
|
35
|
+
readonly id: ProviderId;
|
|
36
|
+
readonly label: string;
|
|
37
|
+
readonly tier: ProviderTier;
|
|
38
|
+
readonly hosts: readonly string[];
|
|
39
|
+
readonly enabled: boolean;
|
|
40
|
+
}
|
|
41
|
+
export interface UpstreamHost {
|
|
42
|
+
/** Exact lowercase hostname; no wildcards. */
|
|
43
|
+
readonly host: string;
|
|
44
|
+
readonly family: UpstreamFamily;
|
|
45
|
+
/** Canonical provider bucket for opt-in scoping; undefined for custom FICHE_UPSTREAM_HOSTS entries. */
|
|
46
|
+
readonly provider?: ProviderId;
|
|
47
|
+
/**
|
|
48
|
+
* API path prefixes on this host, longest first when resolved. Stripping one
|
|
49
|
+
* gives the canonical wire path (`/v1/messages`, `/v1/chat/completions`, ...)
|
|
50
|
+
* that the wire classifiers understand; the upstream still gets the original
|
|
51
|
+
* path byte-for-byte. `''` means the API sits at the root.
|
|
52
|
+
*/
|
|
53
|
+
readonly prefixes: readonly string[];
|
|
54
|
+
}
|
|
55
|
+
export declare const PROVIDER_CATALOG: readonly ProviderDescriptor[];
|
|
56
|
+
export declare const DEFAULT_ENABLED_PROVIDERS: readonly ProviderId[];
|
|
57
|
+
/** Map a user-supplied provider name or alias (`grok` → `xai`, `claude` → `anthropic`) to a canonical ProviderId. */
|
|
58
|
+
export declare function normalizeProviderId(raw: string): ProviderId | null;
|
|
59
|
+
/** Parse a CSV string, special keyword (`all`, `*`, `off`, `none`), or string array into ProviderIds. */
|
|
60
|
+
export declare function parseProvidersSpec(raw: string | readonly string[] | undefined): {
|
|
61
|
+
providers: ProviderId[] | null;
|
|
62
|
+
unknown: string[];
|
|
63
|
+
};
|
|
64
|
+
/** Current effective enabled provider list. Defaults to first-party providers only (`anthropic`, `openai`, `google`). */
|
|
65
|
+
export declare function getEnabledProviders(): ProviderId[];
|
|
66
|
+
/** Env / config configured providers, ignoring temporary in-memory overrides. */
|
|
67
|
+
export declare function getConfiguredProviders(): ProviderId[];
|
|
68
|
+
/** Replace the runtime provider override, or pass `null` to fall back to `FICHE_PROVIDERS` / default. */
|
|
69
|
+
export declare function setEnabledProviders(list: readonly ProviderId[] | null): void;
|
|
70
|
+
/** Return the full catalog annotated with live `enabled` flags. */
|
|
71
|
+
export declare function listProviderStatuses(enabled?: readonly ProviderId[]): ProviderStatus[];
|
|
72
|
+
/** Detect providers whose standard API key env var is non-empty (presence only; never returns secret values). */
|
|
73
|
+
export declare function detectProvidersFromEnv(env?: Readonly<Record<string, string | undefined>>): Array<{
|
|
74
|
+
id: ProviderId;
|
|
75
|
+
matchedEnvKey: string;
|
|
76
|
+
}>;
|
|
77
|
+
/**
|
|
78
|
+
* Could this name ever be an outbound follow target? Syntax only: a public,
|
|
79
|
+
* dotted DNS name. IP literals, single labels, and local suffixes are inbound
|
|
80
|
+
* conveniences in the request guard; they must never become outbound targets.
|
|
81
|
+
*/
|
|
82
|
+
export declare function isFollowableHostname(host: string): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* `INKLING_UPSTREAM_HOSTS=host[/prefix][,host[/prefix]...]`. Extra prefixes for
|
|
85
|
+
* one host may be listed as separate entries. Invalid names are dropped with no
|
|
86
|
+
* fallback to "allow anything": a typo must not widen the list.
|
|
87
|
+
*/
|
|
88
|
+
export declare function parseUpstreamHosts(spec: string | undefined): UpstreamHost[];
|
|
89
|
+
export type UpstreamRegistry = ReadonlyMap<string, UpstreamHost>;
|
|
90
|
+
/** Built-in hosts plus extensions. An extension can add hosts or prefixes, but
|
|
91
|
+
* never re-families or removes a first-party host. */
|
|
92
|
+
export declare function buildUpstreamRegistry(extra?: readonly UpstreamHost[]): UpstreamRegistry;
|
|
93
|
+
/** The built-in registry; `INKLING_UPSTREAM_HOSTS` is applied by the Node entry. */
|
|
94
|
+
export declare const DEFAULT_UPSTREAM_REGISTRY: UpstreamRegistry;
|
|
95
|
+
export interface FollowTarget {
|
|
96
|
+
readonly host: string;
|
|
97
|
+
readonly entry: UpstreamHost;
|
|
98
|
+
/** `https://<host>`; never another scheme or port. */
|
|
99
|
+
readonly origin: string;
|
|
100
|
+
/** Path + query sent upstream, exactly as the agent meant it. */
|
|
101
|
+
readonly outboundPath: string;
|
|
102
|
+
/** Path with the host's API prefix removed, for the wire classifiers. */
|
|
103
|
+
readonly wirePathname: string;
|
|
104
|
+
readonly via: 'host' | 'path';
|
|
105
|
+
}
|
|
106
|
+
export interface FollowRefusal {
|
|
107
|
+
readonly status: 400 | 421;
|
|
108
|
+
readonly reason: 'follow_refused';
|
|
109
|
+
readonly message: string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Decide whether this request follows the agent's host.
|
|
113
|
+
*
|
|
114
|
+
* - `null`: not a follow request; legacy routing applies.
|
|
115
|
+
* - `FollowRefusal`: a `/to/` request that names something inkling will not
|
|
116
|
+
* forward to. Refused before any upstream call.
|
|
117
|
+
* - `FollowTarget`: forward to `origin + outboundPath`.
|
|
118
|
+
*/
|
|
119
|
+
export declare function resolveFollowTarget(url: URL, hostHeader: string | null, registry?: UpstreamRegistry, enabledProviders?: ReadonlySet<string>): FollowTarget | FollowRefusal | null;
|
|
120
|
+
/**
|
|
121
|
+
* For `followHosts: false`: a Host naming a registered third-party provider
|
|
122
|
+
* cannot be served, because legacy routing would send it to the configured
|
|
123
|
+
* first-party upstream with the configured keys. Returns the refusal, or null.
|
|
124
|
+
*/
|
|
125
|
+
export declare function refuseUnfollowedProviderHost(hostHeader: string | null, registry?: UpstreamRegistry): FollowRefusal | null;
|
|
126
|
+
export declare function isFollowRefusal(v: FollowTarget | FollowRefusal | null): v is FollowRefusal;
|
|
127
|
+
//# sourceMappingURL=upstream-hosts.d.ts.map
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider hosts inkling knows, and the "follow the agent" resolver.
|
|
3
|
+
*
|
|
4
|
+
* Following means: forward a request to the host the agent was actually talking
|
|
5
|
+
* to, instead of to whichever upstream inkling's env vars name. It applies to
|
|
6
|
+
* third-party (`compat`) hosts only. First-party hosts keep the configured
|
|
7
|
+
* routing (ANTHROPIC/OPENAI/Google upstreams, gateways, bridges), so nothing
|
|
8
|
+
* that works today changes.
|
|
9
|
+
*
|
|
10
|
+
* Two ways in, one registry:
|
|
11
|
+
* - Host: under `inkling warp` the agent still addresses its own provider and
|
|
12
|
+
* warp keeps that Host when diverting into inkling.
|
|
13
|
+
* - Path: `http://127.0.0.1:47821/to/<host>/<rest>` for agents configured by
|
|
14
|
+
* base URL, so one inkling serves many providers with no per-provider env.
|
|
15
|
+
*
|
|
16
|
+
* Both can only ever select a registry host, over https on 443, so neither is an
|
|
17
|
+
* open proxy: an arbitrary host, port, IP literal or local name is refused or
|
|
18
|
+
* left to legacy routing. The followed request carries only the agent's own
|
|
19
|
+
* credential; configured keys stay bound to their configured upstreams (see
|
|
20
|
+
* proxy.ts).
|
|
21
|
+
*/
|
|
22
|
+
import { readBrandEnv } from './env.js';
|
|
23
|
+
/** First-party hosts: trusted as inbound Host names, never followed. */
|
|
24
|
+
const FIRST_PARTY = [
|
|
25
|
+
{ host: 'api.anthropic.com', family: 'anthropic', provider: 'anthropic', prefixes: [''] },
|
|
26
|
+
{ host: 'api.openai.com', family: 'openai', provider: 'openai', prefixes: [''] },
|
|
27
|
+
{ host: 'chatgpt.com', family: 'openai', provider: 'openai', prefixes: ['/backend-api/codex'] },
|
|
28
|
+
{ host: 'generativelanguage.googleapis.com', family: 'google', provider: 'google', prefixes: [''] },
|
|
29
|
+
{ host: 'cloudcode-pa.googleapis.com', family: 'google', provider: 'google', prefixes: [''] },
|
|
30
|
+
{ host: 'daily-cloudcode-pa.googleapis.com', family: 'google', provider: 'google', prefixes: [''] },
|
|
31
|
+
];
|
|
32
|
+
/**
|
|
33
|
+
* Third-party providers with Anthropic- and/or OpenAI-compatible endpoints.
|
|
34
|
+
* Hostnames and prefixes are from the providers' published Claude Code setup
|
|
35
|
+
* guides (2026-11); `FICHE_UPSTREAM_HOSTS` adds more without a release.
|
|
36
|
+
*/
|
|
37
|
+
const COMPAT = [
|
|
38
|
+
{ host: 'api.deepseek.com', family: 'compat', provider: 'deepseek', prefixes: ['/anthropic', ''] },
|
|
39
|
+
{ host: 'api.moonshot.ai', family: 'compat', provider: 'kimi', prefixes: ['/anthropic', ''] },
|
|
40
|
+
{ host: 'api.moonshot.cn', family: 'compat', provider: 'kimi', prefixes: ['/anthropic', ''] },
|
|
41
|
+
{ host: 'api.kimi.com', family: 'compat', provider: 'kimi', prefixes: ['/coding', ''] },
|
|
42
|
+
{ host: 'api.z.ai', family: 'compat', provider: 'glm', prefixes: ['/api/anthropic', '/api/coding/paas/v4', '/api/paas/v4'] },
|
|
43
|
+
{ host: 'open.bigmodel.cn', family: 'compat', provider: 'glm', prefixes: ['/api/anthropic', '/api/coding/paas/v4', '/api/paas/v4'] },
|
|
44
|
+
{ host: 'api.minimax.io', family: 'compat', provider: 'minimax', prefixes: ['/anthropic', ''] },
|
|
45
|
+
{ host: 'api.minimaxi.com', family: 'compat', provider: 'minimax', prefixes: ['/anthropic', ''] },
|
|
46
|
+
{ host: 'dashscope-intl.aliyuncs.com', family: 'compat', provider: 'qwen', prefixes: ['/apps/anthropic', '/compatible-mode'] },
|
|
47
|
+
{ host: 'dashscope.aliyuncs.com', family: 'compat', provider: 'qwen', prefixes: ['/apps/anthropic', '/compatible-mode'] },
|
|
48
|
+
{ host: 'openrouter.ai', family: 'compat', provider: 'openrouter', prefixes: ['/api'] },
|
|
49
|
+
{ host: 'api.x.ai', family: 'compat', provider: 'xai', prefixes: [''] },
|
|
50
|
+
{ host: 'api.groq.com', family: 'compat', provider: 'groq', prefixes: ['/openai', ''] },
|
|
51
|
+
{ host: 'api.together.xyz', family: 'compat', provider: 'together', prefixes: [''] },
|
|
52
|
+
{ host: 'api.fireworks.ai', family: 'compat', provider: 'fireworks', prefixes: ['/inference', ''] },
|
|
53
|
+
{ host: 'api.cerebras.ai', family: 'compat', provider: 'cerebras', prefixes: [''] },
|
|
54
|
+
{ host: 'api.mistral.ai', family: 'compat', provider: 'mistral', prefixes: [''] },
|
|
55
|
+
{ host: 'api.perplexity.ai', family: 'compat', provider: 'perplexity', prefixes: [''] },
|
|
56
|
+
{ host: 'api.cohere.com', family: 'compat', provider: 'cohere', prefixes: ['/compatibility', ''] },
|
|
57
|
+
{ host: 'api.sambanova.ai', family: 'compat', provider: 'sambanova', prefixes: [''] },
|
|
58
|
+
{ host: 'models.github.ai', family: 'compat', provider: 'github', prefixes: ['/inference', ''] },
|
|
59
|
+
{ host: 'models.inference.ai.azure.com', family: 'compat', provider: 'azure', prefixes: [''] },
|
|
60
|
+
{ host: 'ai-gateway.vercel.sh', family: 'compat', provider: 'vercel', prefixes: [''] },
|
|
61
|
+
];
|
|
62
|
+
const PROVIDER_META = [
|
|
63
|
+
{ id: 'anthropic', label: 'Anthropic / Claude', tier: 'first-party', defaultEnabled: true, aliases: ['claude'], envKeys: ['ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_TOKEN'] },
|
|
64
|
+
{ id: 'openai', label: 'OpenAI / Codex', tier: 'first-party', defaultEnabled: true, aliases: ['codex', 'chatgpt'], envKeys: ['OPENAI_API_KEY'] },
|
|
65
|
+
{ id: 'google', label: 'Google / Gemini', tier: 'first-party', defaultEnabled: true, aliases: ['gemini', 'antigravity'], envKeys: ['GEMINI_API_KEY', 'GOOGLE_API_KEY'] },
|
|
66
|
+
{ id: 'deepseek', label: 'DeepSeek', tier: 'third-party', defaultEnabled: false, envKeys: ['DEEPSEEK_API_KEY'] },
|
|
67
|
+
{ id: 'kimi', label: 'Kimi / Moonshot', tier: 'third-party', defaultEnabled: false, aliases: ['moonshot'], envKeys: ['MOONSHOT_API_KEY', 'KIMI_API_KEY'] },
|
|
68
|
+
{ id: 'glm', label: 'GLM / Z.ai', tier: 'third-party', defaultEnabled: false, aliases: ['zai', 'z.ai', 'bigmodel'], envKeys: ['ZAI_API_KEY', 'ZHIPU_API_KEY'] },
|
|
69
|
+
{ id: 'minimax', label: 'MiniMax', tier: 'third-party', defaultEnabled: false, envKeys: ['MINIMAX_API_KEY'] },
|
|
70
|
+
{ id: 'qwen', label: 'Qwen / DashScope', tier: 'third-party', defaultEnabled: false, aliases: ['dashscope', 'alibaba'], envKeys: ['DASHSCOPE_API_KEY'] },
|
|
71
|
+
{ id: 'openrouter', label: 'OpenRouter', tier: 'third-party', defaultEnabled: false, envKeys: ['OPENROUTER_API_KEY'] },
|
|
72
|
+
{ id: 'xai', label: 'xAI / Grok', tier: 'third-party', defaultEnabled: false, aliases: ['grok'], envKeys: ['XAI_API_KEY'] },
|
|
73
|
+
{ id: 'groq', label: 'Groq', tier: 'third-party', defaultEnabled: false, envKeys: ['GROQ_API_KEY'] },
|
|
74
|
+
{ id: 'together', label: 'Together AI', tier: 'third-party', defaultEnabled: false, envKeys: ['TOGETHER_API_KEY'] },
|
|
75
|
+
{ id: 'fireworks', label: 'Fireworks AI', tier: 'third-party', defaultEnabled: false, envKeys: ['FIREWORKS_API_KEY'] },
|
|
76
|
+
{ id: 'cerebras', label: 'Cerebras', tier: 'third-party', defaultEnabled: false, envKeys: ['CEREBRAS_API_KEY'] },
|
|
77
|
+
{ id: 'mistral', label: 'Mistral', tier: 'third-party', defaultEnabled: false, envKeys: ['MISTRAL_API_KEY'] },
|
|
78
|
+
{ id: 'perplexity', label: 'Perplexity', tier: 'third-party', defaultEnabled: false, envKeys: ['PERPLEXITY_API_KEY'] },
|
|
79
|
+
{ id: 'cohere', label: 'Cohere', tier: 'third-party', defaultEnabled: false, envKeys: ['COHERE_API_KEY'] },
|
|
80
|
+
{ id: 'sambanova', label: 'SambaNova', tier: 'third-party', defaultEnabled: false, envKeys: ['SAMBANOVA_API_KEY'] },
|
|
81
|
+
{ id: 'github', label: 'GitHub Models', tier: 'third-party', defaultEnabled: false, aliases: ['github-models'], envKeys: ['GITHUB_MODELS_TOKEN'] },
|
|
82
|
+
{ id: 'azure', label: 'Azure AI Inference', tier: 'third-party', defaultEnabled: false, aliases: ['azure-ai'], envKeys: ['AZURE_AI_API_KEY'] },
|
|
83
|
+
{ id: 'vercel', label: 'Vercel AI Gateway', tier: 'third-party', defaultEnabled: false, aliases: ['vercel-ai'], envKeys: ['AI_GATEWAY_API_KEY', 'VERCEL_AI_GATEWAY_KEY'] },
|
|
84
|
+
];
|
|
85
|
+
const ALL_BUILTIN_HOSTS = [...FIRST_PARTY, ...COMPAT];
|
|
86
|
+
export const PROVIDER_CATALOG = PROVIDER_META.map((meta) => ({
|
|
87
|
+
...meta,
|
|
88
|
+
hosts: ALL_BUILTIN_HOSTS.filter((h) => h.provider === meta.id).map((h) => h.host),
|
|
89
|
+
}));
|
|
90
|
+
export const DEFAULT_ENABLED_PROVIDERS = ['anthropic', 'openai', 'google'];
|
|
91
|
+
const PROVIDER_ALIAS_MAP = (() => {
|
|
92
|
+
const map = new Map();
|
|
93
|
+
for (const p of PROVIDER_CATALOG) {
|
|
94
|
+
map.set(p.id.toLowerCase(), p.id);
|
|
95
|
+
for (const alias of p.aliases ?? [])
|
|
96
|
+
map.set(alias.toLowerCase(), p.id);
|
|
97
|
+
}
|
|
98
|
+
return map;
|
|
99
|
+
})();
|
|
100
|
+
/** Map a user-supplied provider name or alias (`grok` → `xai`, `claude` → `anthropic`) to a canonical ProviderId. */
|
|
101
|
+
export function normalizeProviderId(raw) {
|
|
102
|
+
const key = raw.trim().toLowerCase();
|
|
103
|
+
if (!key)
|
|
104
|
+
return null;
|
|
105
|
+
return PROVIDER_ALIAS_MAP.get(key) ?? null;
|
|
106
|
+
}
|
|
107
|
+
/** Parse a CSV string, special keyword (`all`, `*`, `off`, `none`), or string array into ProviderIds. */
|
|
108
|
+
export function parseProvidersSpec(raw) {
|
|
109
|
+
if (raw === undefined)
|
|
110
|
+
return { providers: null, unknown: [] };
|
|
111
|
+
let tokens;
|
|
112
|
+
if (typeof raw === 'string') {
|
|
113
|
+
const trimmed = raw.trim();
|
|
114
|
+
if (!trimmed)
|
|
115
|
+
return { providers: null, unknown: [] };
|
|
116
|
+
if (/^(?:all|\*)$/i.test(trimmed)) {
|
|
117
|
+
return { providers: PROVIDER_CATALOG.map((p) => p.id), unknown: [] };
|
|
118
|
+
}
|
|
119
|
+
if (/^(?:0|false|no|off|none)$/i.test(trimmed)) {
|
|
120
|
+
return { providers: [], unknown: [] };
|
|
121
|
+
}
|
|
122
|
+
tokens = trimmed.split(',').map((s) => s.trim()).filter(Boolean);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
if (raw.length === 0)
|
|
126
|
+
return { providers: [], unknown: [] };
|
|
127
|
+
tokens = raw.map((s) => String(s).trim()).filter(Boolean);
|
|
128
|
+
if (tokens.length === 0)
|
|
129
|
+
return { providers: [], unknown: [] };
|
|
130
|
+
if (tokens.length === 1 && /^(?:all|\*)$/i.test(tokens[0])) {
|
|
131
|
+
return { providers: PROVIDER_CATALOG.map((p) => p.id), unknown: [] };
|
|
132
|
+
}
|
|
133
|
+
if (tokens.length === 1 && /^(?:0|false|no|off|none)$/i.test(tokens[0])) {
|
|
134
|
+
return { providers: [], unknown: [] };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const seen = new Set();
|
|
138
|
+
const unknown = [];
|
|
139
|
+
for (const tok of tokens) {
|
|
140
|
+
const id = normalizeProviderId(tok);
|
|
141
|
+
if (id)
|
|
142
|
+
seen.add(id);
|
|
143
|
+
else
|
|
144
|
+
unknown.push(tok);
|
|
145
|
+
}
|
|
146
|
+
return { providers: [...seen], unknown };
|
|
147
|
+
}
|
|
148
|
+
let runtimeEnabledProviders = null;
|
|
149
|
+
function envOrDefaultProviders() {
|
|
150
|
+
const { providers } = parseProvidersSpec(readBrandEnv('PROVIDERS'));
|
|
151
|
+
return providers !== null ? providers : [...DEFAULT_ENABLED_PROVIDERS];
|
|
152
|
+
}
|
|
153
|
+
/** Current effective enabled provider list. Defaults to first-party providers only (`anthropic`, `openai`, `google`). */
|
|
154
|
+
export function getEnabledProviders() {
|
|
155
|
+
return runtimeEnabledProviders !== null ? [...runtimeEnabledProviders] : envOrDefaultProviders();
|
|
156
|
+
}
|
|
157
|
+
/** Env / config configured providers, ignoring temporary in-memory overrides. */
|
|
158
|
+
export function getConfiguredProviders() {
|
|
159
|
+
return envOrDefaultProviders();
|
|
160
|
+
}
|
|
161
|
+
/** Replace the runtime provider override, or pass `null` to fall back to `FICHE_PROVIDERS` / default. */
|
|
162
|
+
export function setEnabledProviders(list) {
|
|
163
|
+
if (list === null) {
|
|
164
|
+
runtimeEnabledProviders = null;
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const seen = new Set();
|
|
168
|
+
for (const entry of list) {
|
|
169
|
+
const normalized = normalizeProviderId(String(entry));
|
|
170
|
+
if (normalized)
|
|
171
|
+
seen.add(normalized);
|
|
172
|
+
}
|
|
173
|
+
runtimeEnabledProviders = [...seen];
|
|
174
|
+
}
|
|
175
|
+
/** Return the full catalog annotated with live `enabled` flags. */
|
|
176
|
+
export function listProviderStatuses(enabled = getEnabledProviders()) {
|
|
177
|
+
const on = new Set(enabled);
|
|
178
|
+
return PROVIDER_CATALOG.map((p) => ({
|
|
179
|
+
id: p.id,
|
|
180
|
+
label: p.label,
|
|
181
|
+
tier: p.tier,
|
|
182
|
+
hosts: [...p.hosts],
|
|
183
|
+
enabled: on.has(p.id),
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
/** Detect providers whose standard API key env var is non-empty (presence only; never returns secret values). */
|
|
187
|
+
export function detectProvidersFromEnv(env = typeof process !== 'undefined' ? process.env : {}) {
|
|
188
|
+
const found = [];
|
|
189
|
+
for (const p of PROVIDER_CATALOG) {
|
|
190
|
+
for (const key of p.envKeys ?? []) {
|
|
191
|
+
if (env[key]?.trim()) {
|
|
192
|
+
found.push({ id: p.id, matchedEnvKey: key });
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return found;
|
|
198
|
+
}
|
|
199
|
+
const LABEL_RE = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
200
|
+
/**
|
|
201
|
+
* Could this name ever be an outbound follow target? Syntax only: a public,
|
|
202
|
+
* dotted DNS name. IP literals, single labels, and local suffixes are inbound
|
|
203
|
+
* conveniences in the request guard; they must never become outbound targets.
|
|
204
|
+
*/
|
|
205
|
+
export function isFollowableHostname(host) {
|
|
206
|
+
if (host.length === 0 || host.length > 253)
|
|
207
|
+
return false;
|
|
208
|
+
const labels = host.split('.');
|
|
209
|
+
if (labels.length < 2)
|
|
210
|
+
return false;
|
|
211
|
+
if (!labels.every((l) => LABEL_RE.test(l)))
|
|
212
|
+
return false;
|
|
213
|
+
if (/^\d+$/.test(labels.at(-1)))
|
|
214
|
+
return false; // 10.0.0.1, 1.2.3.4
|
|
215
|
+
const tld = labels.at(-1);
|
|
216
|
+
if (['localhost', 'local', 'internal', 'lan', 'home', 'arpa', 'test', 'invalid', 'example'].includes(tld))
|
|
217
|
+
return false;
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
function normalizePrefix(prefix) {
|
|
221
|
+
const trimmed = prefix.trim().replace(/\/+$/, '');
|
|
222
|
+
if (trimmed === '')
|
|
223
|
+
return '';
|
|
224
|
+
return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* `INKLING_UPSTREAM_HOSTS=host[/prefix][,host[/prefix]...]`. Extra prefixes for
|
|
228
|
+
* one host may be listed as separate entries. Invalid names are dropped with no
|
|
229
|
+
* fallback to "allow anything": a typo must not widen the list.
|
|
230
|
+
*/
|
|
231
|
+
export function parseUpstreamHosts(spec) {
|
|
232
|
+
if (!spec)
|
|
233
|
+
return [];
|
|
234
|
+
const byHost = new Map();
|
|
235
|
+
for (const raw of spec.split(',')) {
|
|
236
|
+
const item = raw.trim().toLowerCase();
|
|
237
|
+
if (item === '')
|
|
238
|
+
continue;
|
|
239
|
+
const slash = item.indexOf('/');
|
|
240
|
+
const host = (slash < 0 ? item : item.slice(0, slash)).replace(/\.$/, '');
|
|
241
|
+
if (!isFollowableHostname(host))
|
|
242
|
+
continue;
|
|
243
|
+
const prefix = normalizePrefix(slash < 0 ? '' : item.slice(slash));
|
|
244
|
+
if (!/^(?:\/[a-z0-9._~-]+)*$/.test(prefix))
|
|
245
|
+
continue;
|
|
246
|
+
const set = byHost.get(host) ?? new Set();
|
|
247
|
+
set.add(prefix);
|
|
248
|
+
byHost.set(host, set);
|
|
249
|
+
}
|
|
250
|
+
return [...byHost].map(([host, prefixes]) => ({ host, family: 'compat', prefixes: [...prefixes, ''] }));
|
|
251
|
+
}
|
|
252
|
+
/** Built-in hosts plus extensions. An extension can add hosts or prefixes, but
|
|
253
|
+
* never re-families or removes a first-party host. */
|
|
254
|
+
export function buildUpstreamRegistry(extra = []) {
|
|
255
|
+
const map = new Map();
|
|
256
|
+
for (const entry of [...FIRST_PARTY, ...COMPAT])
|
|
257
|
+
map.set(entry.host, entry);
|
|
258
|
+
for (const entry of extra) {
|
|
259
|
+
if (!isFollowableHostname(entry.host))
|
|
260
|
+
continue;
|
|
261
|
+
const existing = map.get(entry.host);
|
|
262
|
+
if (existing && existing.family !== 'compat')
|
|
263
|
+
continue;
|
|
264
|
+
const prefixes = new Set([...(existing?.prefixes ?? []), ...entry.prefixes]);
|
|
265
|
+
const provider = existing?.provider ?? entry.provider;
|
|
266
|
+
map.set(entry.host, {
|
|
267
|
+
host: entry.host,
|
|
268
|
+
family: 'compat',
|
|
269
|
+
...(provider !== undefined ? { provider } : {}),
|
|
270
|
+
prefixes: [...prefixes],
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return map;
|
|
274
|
+
}
|
|
275
|
+
/** The built-in registry; `INKLING_UPSTREAM_HOSTS` is applied by the Node entry. */
|
|
276
|
+
export const DEFAULT_UPSTREAM_REGISTRY = buildUpstreamRegistry();
|
|
277
|
+
function wirePathFor(entry, pathname) {
|
|
278
|
+
const prefixes = [...entry.prefixes].sort((a, b) => b.length - a.length);
|
|
279
|
+
for (const prefix of prefixes) {
|
|
280
|
+
if (prefix === '')
|
|
281
|
+
return pathname;
|
|
282
|
+
if (pathname === prefix)
|
|
283
|
+
return '/';
|
|
284
|
+
if (pathname.startsWith(`${prefix}/`))
|
|
285
|
+
return pathname.slice(prefix.length);
|
|
286
|
+
}
|
|
287
|
+
return pathname;
|
|
288
|
+
}
|
|
289
|
+
/** Parse "host[:port]" from a Host header: lowercase, trailing dot removed. */
|
|
290
|
+
function splitHostHeader(value) {
|
|
291
|
+
const v = value.trim().toLowerCase();
|
|
292
|
+
if (v === '' || v.startsWith('['))
|
|
293
|
+
return null; // IPv6 literal: never followed
|
|
294
|
+
const m = /^([^:]+)(?::(\d{1,5}))?$/.exec(v);
|
|
295
|
+
if (!m)
|
|
296
|
+
return null;
|
|
297
|
+
return { host: m[1].replace(/\.$/, ''), port: m[2] ?? null };
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Decide whether this request follows the agent's host.
|
|
301
|
+
*
|
|
302
|
+
* - `null`: not a follow request; legacy routing applies.
|
|
303
|
+
* - `FollowRefusal`: a `/to/` request that names something inkling will not
|
|
304
|
+
* forward to. Refused before any upstream call.
|
|
305
|
+
* - `FollowTarget`: forward to `origin + outboundPath`.
|
|
306
|
+
*/
|
|
307
|
+
export function resolveFollowTarget(url, hostHeader, registry = DEFAULT_UPSTREAM_REGISTRY, enabledProviders) {
|
|
308
|
+
// `url.pathname` is already dot-segment-normalized by the URL parser, and
|
|
309
|
+
// percent-escapes stay escaped, so `%2f`/`%40` tricks fail the syntax check.
|
|
310
|
+
if (url.pathname === '/to' || url.pathname.startsWith('/to/')) {
|
|
311
|
+
const rest = url.pathname.slice('/to/'.length);
|
|
312
|
+
const slash = rest.indexOf('/');
|
|
313
|
+
const hostPart = slash < 0 ? rest : rest.slice(0, slash);
|
|
314
|
+
const host = hostPart.toLowerCase().replace(/\.$/, '');
|
|
315
|
+
const refuse = (message) => ({ status: 400, reason: 'follow_refused', message });
|
|
316
|
+
if (!isFollowableHostname(host))
|
|
317
|
+
return refuse(`fiche: /to/ needs a provider hostname, got ${JSON.stringify(hostPart.slice(0, 100))}`);
|
|
318
|
+
const entry = registry.get(host);
|
|
319
|
+
if (!entry)
|
|
320
|
+
return refuse(`fiche: ${host} is not a known provider host; add it with FICHE_UPSTREAM_HOSTS`);
|
|
321
|
+
if (entry.family !== 'compat') {
|
|
322
|
+
return refuse(`fiche: ${host} is first-party; point the agent at fiche's plain base URL instead of /to/`);
|
|
323
|
+
}
|
|
324
|
+
if (enabledProviders && entry.provider !== undefined && !enabledProviders.has(entry.provider)) {
|
|
325
|
+
return refuse(`fiche: ${host} (provider ${entry.provider}) is disabled; enable it via \`fiche providers enable ${entry.provider}\` or FICHE_PROVIDERS`);
|
|
326
|
+
}
|
|
327
|
+
const tail = slash < 0 ? '/' : rest.slice(slash);
|
|
328
|
+
return {
|
|
329
|
+
host,
|
|
330
|
+
entry,
|
|
331
|
+
origin: `https://${host}`,
|
|
332
|
+
outboundPath: tail + url.search,
|
|
333
|
+
wirePathname: wirePathFor(entry, tail),
|
|
334
|
+
via: 'path',
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
if (hostHeader === null)
|
|
338
|
+
return null;
|
|
339
|
+
const parsed = splitHostHeader(hostHeader);
|
|
340
|
+
if (!parsed)
|
|
341
|
+
return null;
|
|
342
|
+
const entry = registry.get(parsed.host);
|
|
343
|
+
if (!entry || entry.family !== 'compat')
|
|
344
|
+
return null;
|
|
345
|
+
// Defense in depth: a registry handed in by a caller is still never allowed
|
|
346
|
+
// to point at an IP literal or a local name.
|
|
347
|
+
if (!isFollowableHostname(parsed.host))
|
|
348
|
+
return null;
|
|
349
|
+
// Never fall back to legacy first-party routing when a registered third-party
|
|
350
|
+
// provider is disabled — return an explicit refusal instead.
|
|
351
|
+
if (enabledProviders && entry.provider !== undefined && !enabledProviders.has(entry.provider)) {
|
|
352
|
+
return {
|
|
353
|
+
status: 421,
|
|
354
|
+
reason: 'follow_refused',
|
|
355
|
+
message: `fiche: ${parsed.host} (provider ${entry.provider}) is disabled; enable it via \`fiche providers enable ${entry.provider}\` or FICHE_PROVIDERS`,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
// A third-party Host that is not followed must not fall back to legacy
|
|
359
|
+
// routing: that would hand its request, and the configured first-party keys,
|
|
360
|
+
// to the default upstream.
|
|
361
|
+
if (parsed.port !== null && parsed.port !== '443') {
|
|
362
|
+
return {
|
|
363
|
+
status: 421,
|
|
364
|
+
reason: 'follow_refused',
|
|
365
|
+
message: `fiche: ${parsed.host}:${parsed.port} refused; provider hosts are followed on https:443 only`,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
return {
|
|
369
|
+
host: parsed.host,
|
|
370
|
+
entry,
|
|
371
|
+
origin: `https://${parsed.host}`,
|
|
372
|
+
outboundPath: url.pathname + url.search,
|
|
373
|
+
wirePathname: wirePathFor(entry, url.pathname),
|
|
374
|
+
via: 'host',
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* For `followHosts: false`: a Host naming a registered third-party provider
|
|
379
|
+
* cannot be served, because legacy routing would send it to the configured
|
|
380
|
+
* first-party upstream with the configured keys. Returns the refusal, or null.
|
|
381
|
+
*/
|
|
382
|
+
export function refuseUnfollowedProviderHost(hostHeader, registry = DEFAULT_UPSTREAM_REGISTRY) {
|
|
383
|
+
const parsed = hostHeader === null ? null : splitHostHeader(hostHeader);
|
|
384
|
+
if (!parsed)
|
|
385
|
+
return null;
|
|
386
|
+
const entry = registry.get(parsed.host);
|
|
387
|
+
if (!entry || entry.family !== 'compat')
|
|
388
|
+
return null;
|
|
389
|
+
return {
|
|
390
|
+
status: 421,
|
|
391
|
+
reason: 'follow_refused',
|
|
392
|
+
message: `fiche: ${parsed.host} is a third-party provider and following is off (FICHE_FOLLOW_HOSTS=0)`,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
export function isFollowRefusal(v) {
|
|
396
|
+
return v !== null && 'reason' in v;
|
|
397
|
+
}
|
|
398
|
+
//# sourceMappingURL=upstream-hosts.js.map
|