MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / resolveAnnotatedValue

Function resolveAnnotatedValue

dev/local/env-sync/parse.ts:207–312  ·  view source on GitHub ↗
(
  key: string,
  entry: ExampleEntry,
  envLocal: Map<string, string>,
  lanIp: string | undefined,
  serviceUsesLanIp: boolean
)

Source from the content-addressed store, hash-verified

205// ---------------------------------------------------------------------------
206// Annotation-based value resolution
207// ---------------------------------------------------------------------------
208
209const WORKER_LOCALHOST_URL_KEYS = new Set(['KILOCODE_BACKEND_BASE_URL', 'KILO_OPENROUTER_BASE']);
210
211function resolveAnnotatedValue(
212 key: string,
213 entry: ExampleEntry,
214 envLocal: Map<string, string>,
215 lanIp: string | undefined,
216 serviceUsesLanIp: boolean
217): { value: string; resolved: boolean; source: ResolvedValueSource } {
218 switch (entry.annotation.type) {
219 case 'override':
220 return { value: entry.defaultValue, resolved: true, source: 'override' };
221
222 case 'from': {
223 const val = envLocal.get(entry.annotation.envLocalKey);
224 if (val) return { value: val, resolved: true, source: 'env-local' };
225 if (val === '') return { value: '', resolved: false, source: 'missing' };
226 if (entry.defaultValue) {
227 return { value: entry.defaultValue, resolved: true, source: 'default' };
228 }
229 return { value: '', resolved: false, source: 'missing' };
230 }
231
232 case 'url': {
233 const isOrigins = key.includes('ORIGINS');
234 const isHostname = key.includes('HOSTNAME') && !key.includes('URL');
235 const isWs =
236 key.includes('_WS_') ||
237 entry.defaultValue.startsWith('ws://') ||
238 entry.defaultValue.startsWith('wss://');
239 const defaultUsesDockerHost = entry.defaultValue.includes('host.docker.internal');
240 const defaultUsesWorkerLocalhost =
241 WORKER_LOCALHOST_URL_KEYS.has(key) &&
242 (entry.defaultValue.includes('localhost') || entry.defaultValue.includes('127.0.0.1'));
243 // LAN IP for container services, except for ORIGINS keys where the host is
244 // always localhost for the base entry and a LAN entry is additionally emitted
245 // when serviceUsesLanIp is set. Preserve host.docker.internal when the example
246 // default uses it (sandbox containers need it to reach the host from inside Docker).
247 // Preserve localhost for worker-side URLs that are translated separately
248 // before being sent into sandbox containers.
249 const host = defaultUsesDockerHost
250 ? 'host.docker.internal'
251 : defaultUsesWorkerLocalhost
252 ? 'localhost'
253 : serviceUsesLanIp && !isOrigins && lanIp
254 ? lanIp
255 : 'localhost';
256 const protocol = isWs ? 'ws' : 'http';
257
258 const resolvedParts: string[] = [];
259 for (const svcRef of entry.annotation.services) {
260 const port = servicePort(svcRef.name);
261 if (port === undefined) {
262 console.warn(`⚠ Unknown service "${svcRef.name}" in @url annotation for ${key}`);
263 continue;
264 }

Callers 2

computePlanFunction · 0.90

Calls 7

servicePortFunction · 0.85
toPkcs8IfNeededFunction · 0.85
hasMethod · 0.80
pushMethod · 0.80
joinMethod · 0.80
getMethod · 0.65
warnMethod · 0.65

Tested by

no test coverage detected