evolcore 0.0.47 → 0.0.49
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 +90 -1
- package/bin/ec-build.mjs +86 -0
- package/dist/agent-template/jobs.js +12 -4
- package/dist/agent-template/runtime.js +143 -40
- package/dist/agent-template/service.js +66 -13
- package/dist/agents/baseagent.js +60 -20
- package/dist/agents/claude-runner.js +34 -17
- package/dist/agents/codex-runner.js +36 -17
- package/dist/agents/ecagent-runner.js +7 -4
- package/dist/agents/gemini-runner.js +5 -2
- package/dist/agents/runner-types.js +5 -21
- package/dist/aun/aid/agentmd.js +5 -1
- package/dist/aun/msg/p2p.js +2 -1
- package/dist/aun/service-proxy.js +4 -2
- package/dist/build-identity.json +1 -0
- package/dist/channels/aun.js +722 -295
- package/dist/channels/daemon.js +9 -25
- package/dist/cli/aun-commands.js +86 -51
- package/dist/cli/code-stats.js +19 -8
- package/dist/cli/command-log.js +1 -1
- package/dist/cli/daemon-commands.js +44 -43
- package/dist/cli/directory.js +13 -2
- package/dist/cli/ecmsg-gateway.js +5 -2
- package/dist/cli/index.js +9 -9
- package/dist/cli/init-channel.js +4 -6
- package/dist/cli/init.js +72 -50
- package/dist/cli/invoke-command.js +1 -1
- package/dist/cli/managed-command-guard.js +2 -0
- package/dist/cli/model.js +20 -5
- package/dist/cli/provider-command.js +74 -13
- package/dist/cli/quota.js +89 -0
- package/dist/cli/role.js +62 -17
- package/dist/cli/services-command.js +1 -0
- package/dist/cli/stats.js +3 -3
- package/dist/cli/team-workspace-command.js +56 -2
- package/dist/cli/thread-command.js +16 -51
- package/dist/config/boot-log.js +3 -9
- package/dist/config/builtin-roles.js +5 -7
- package/dist/config/config-field-policy.js +71 -1
- package/dist/config/config-manager.js +13 -7
- package/dist/config/daemon-services.js +30 -3
- package/dist/config/gateway-config.js +2 -2
- package/dist/config/invoke-ipc-service.js +6 -4
- package/dist/config/peer-bind-code.js +3 -4
- package/dist/config/peer-book-store.js +62 -229
- package/dist/config/peer-book.js +26 -27
- package/dist/config/peer-role-resolver.js +16 -1
- package/dist/config/provider-identity-migration.js +4 -1
- package/dist/config/provider-ipc-service.js +56 -11
- package/dist/config/provider-menu-request.js +24 -5
- package/dist/config/provider-templates.js +127 -0
- package/dist/config/providers-config.js +67 -12
- package/dist/config/providers-management.js +477 -51
- package/dist/config/resolved-config-op.js +3 -2
- package/dist/config/role-command-service.js +248 -1
- package/dist/config/schema-migration.js +105 -4
- package/dist/core/agent-application-service.js +6 -1
- package/dist/core/agent-runtime.js +4 -0
- package/dist/core/auth/agent-delegation.js +2 -1
- package/dist/core/auth/auth-gateway.js +2 -1
- package/dist/core/auth/delegation-authorization.js +36 -26
- package/dist/core/auth/operation-authorizer.js +25 -0
- package/dist/core/auth/operation-catalog.js +37 -64
- package/dist/core/capability/providers/codex-capability-provider.js +7 -8
- package/dist/core/channel-loader.js +18 -0
- package/dist/core/command/agent-control.js +32 -0
- package/dist/core/command/cli-intent-parser.js +13 -0
- package/dist/core/command/command-handler.js +126 -87
- package/dist/core/command/menu-catalog.js +1 -0
- package/dist/core/command/menu-handler.js +67 -150
- package/dist/core/command/menu-protocol.js +31 -1
- package/dist/core/command/provider-menu.js +13 -3
- package/dist/core/command/remote-shell.js +565 -0
- package/dist/core/command/role-menu.js +47 -23
- package/dist/core/command/slash-handler.js +26 -43
- package/dist/core/command/team-workspace-items.js +81 -12
- package/dist/core/command/team-workspace-team.js +9 -71
- package/dist/core/command/topic-slash.js +9 -30
- package/dist/core/daemon-bootstrap.js +8 -1
- package/dist/core/daemon-control.js +64 -8
- package/dist/core/daemon-lifecycle.js +16 -2
- package/dist/core/evolagent-registry.js +20 -3
- package/dist/core/file-integrity/store.js +2 -0
- package/dist/core/handoff/runtime.js +37 -1
- package/dist/core/inference/text-inference.js +3 -1
- package/dist/core/initialization/service.js +11 -6
- package/dist/core/interaction-router.js +3 -0
- package/dist/core/invoke/executors/text.js +72 -19
- package/dist/core/ipc-registration.js +3 -1
- package/dist/core/message/aun-ipc-sender.js +279 -107
- package/dist/core/message/contact-admission.js +91 -6
- package/dist/core/message/contact-pending-inbound.js +113 -0
- package/dist/core/message/contact-thread-approval.js +212 -0
- package/dist/core/message/im-renderer.js +41 -2
- package/dist/core/message/message-admission.js +5 -39
- package/dist/core/message/message-bridge.js +140 -370
- package/dist/core/message/message-log.js +29 -0
- package/dist/core/message/message-queue.js +119 -15
- package/dist/core/message/message-utils.js +8 -0
- package/dist/core/message/response-engine.js +580 -134
- package/dist/core/message/send-routing.js +5 -1
- package/dist/core/model/catalog-service.js +31 -11
- package/dist/core/model/config-scope.js +6 -14
- package/dist/core/model/model-capabilities.js +22 -4
- package/dist/core/model/model-catalog.js +1 -0
- package/dist/core/model/model-diagnostics.js +19 -0
- package/dist/core/model/model-fallback.js +13 -2
- package/dist/core/ownership/owner-resolver.js +328 -50
- package/dist/core/pack-auth.js +3 -0
- package/dist/core/pack-install.js +1 -1
- package/dist/core/permission/ec-command-parser.js +106 -19
- package/dist/core/permission/protected-paths.js +6 -6
- package/dist/core/permission/services-path-acl.js +451 -0
- package/dist/core/permission/tool-policy.js +5 -1
- package/dist/core/relation/peer-identity.js +36 -1
- package/dist/core/relation/same-owner-proof.js +81 -112
- package/dist/core/session/adapters/codex-session-file-adapter.js +96 -15
- package/dist/core/session/session-file-health.js +13 -7
- package/dist/core/session/session-fs-store.js +91 -35
- package/dist/core/session/session-manager.js +68 -24
- package/dist/core/session/session-mapper.js +14 -34
- package/dist/core/session/session-transcript.js +5 -1
- package/dist/core/task/task-activity-store.js +84 -0
- package/dist/core/task/task-recovery-service.js +13 -0
- package/dist/core/thread/command-parser.js +7 -12
- package/dist/core/thread/thread-context-storage.js +3 -2
- package/dist/core/thread/thread-operation.js +2 -13
- package/dist/core/thread/thread-service.js +15 -46
- package/dist/core/thread/topic-id.js +15 -0
- package/dist/directory/owner-directory-admission.js +12 -9
- package/dist/directory/owner-directory-cache.js +64 -28
- package/dist/directory/owner-directory-markdown.js +14 -2
- package/dist/directory/owner-directory-policy.js +5 -30
- package/dist/directory/owner-directory-query-operation.js +29 -3
- package/dist/directory/owner-directory-remarks.js +11 -3
- package/dist/directory/owner-directory-request-operation.js +5 -2
- package/dist/eck/kit-renderer.js +1 -1
- package/dist/eck/message-renderer.js +112 -29
- package/dist/ecweb/basic-settings-service.js +3 -1
- package/dist/ecweb/message-context-service.js +1 -1
- package/dist/ecweb/pairing.js +4 -3
- package/dist/ecweb/providers-service.js +26 -1
- package/dist/ecweb/runtime/lifecycle.js +18 -8
- package/dist/ecweb/runtime/proxy-access.js +74 -0
- package/dist/ecweb/runtime/supervisor.js +36 -6
- package/dist/event-center/center-config.js +9 -5
- package/dist/event-center/dispatcher.js +10 -1
- package/dist/event-center/ipc-operation.js +19 -0
- package/dist/event-center/registry.js +29 -3
- package/dist/event-center/runtime.js +106 -11
- package/dist/event-center/script-runner.js +5 -5
- package/dist/index.js +96 -59
- package/dist/ipc.js +38 -3
- package/dist/outputs/authorization.js +16 -0
- package/dist/outputs/cards.js +336 -0
- package/dist/outputs/events.js +63 -0
- package/dist/outputs/protocol.js +103 -0
- package/dist/outputs/service.js +266 -0
- package/dist/paths.js +3 -0
- package/dist/proxy/manager.js +43 -13
- package/dist/services/index.js +114 -5
- package/dist/services/ipc-operation.js +88 -15
- package/dist/services/pack-manager.js +13 -1
- package/dist/services/router.js +21 -6
- package/dist/services/script-runner.js +5 -4
- package/dist/services/service-payload.js +13 -7
- package/dist/services/validation.js +1 -1
- package/dist/stats/budget.js +2 -2
- package/dist/stats/db.js +19 -0
- package/dist/stats/index.js +1 -1
- package/dist/stats/{price-resolver.js → model-resolver.js} +304 -23
- package/dist/stats/normalizer.js +2 -0
- package/dist/stats/query.js +35 -27
- package/dist/stats/quota-query.js +95 -0
- package/dist/stats/role-budget.js +5 -2
- package/dist/stats/writer.js +10 -6
- package/dist/trigger/audit.js +7 -0
- package/dist/trigger/authorization.js +105 -11
- package/dist/trigger/feedback.js +10 -0
- package/dist/trigger/history.js +37 -11
- package/dist/trigger/ipc-service.js +12 -1
- package/dist/trigger/manager.js +1 -0
- package/dist/trigger/scheduler.js +7 -5
- package/dist/trigger/script-executor.js +2 -1
- package/dist/trigger/script-sandbox.js +21 -3
- package/dist/trigger/validation.js +22 -0
- package/dist/utils/atomic-write.js +22 -8
- package/dist/utils/cross-platform.js +73 -26
- package/dist/utils/error-dict.json +94 -4
- package/dist/utils/error-utils.js +27 -4
- package/dist/utils/evolcore-version.js +36 -65
- package/dist/utils/host-info.js +100 -1
- package/dist/utils/msg-diag.js +1 -0
- package/dist/utils/npm-ops.js +43 -8
- package/dist/utils/process-introspect.js +12 -7
- package/dist/utils/process-tree.js +50 -2
- package/dist/utils/windows-shell-trust.js +2 -0
- package/dist/workbench/protocol.js +80 -0
- package/dist/workbench/service.js +359 -0
- package/ecagent/dist/types.d.ts +6 -2
- package/kits/docs/context-assembly.md +1 -1
- package/kits/docs/evolcore/INDEX.md +4 -2
- package/kits/docs/evolcore/aid.md +25 -0
- package/kits/docs/evolcore/config.md +16 -1
- package/kits/docs/evolcore/directory.md +30 -12
- package/kits/docs/evolcore/ecweb-lifecycle.md +15 -0
- package/kits/docs/evolcore/model.md +10 -0
- package/kits/docs/evolcore/msg.md +9 -0
- package/kits/docs/evolcore/peer.md +19 -65
- package/kits/docs/evolcore/provider.md +124 -4
- package/kits/docs/evolcore/quota.md +54 -0
- package/kits/docs/evolcore/role.md +83 -0
- package/kits/docs/evolcore/services.md +6 -6
- package/kits/docs/evolcore/stats.md +2 -0
- package/kits/docs/evolcore/team-workspace.md +1 -1
- package/kits/docs/evolcore/thread-chain.md +14 -15
- package/kits/docs/evolcore/tool-authorization.md +5 -0
- package/kits/docs/evolcore/trigger.md +12 -0
- package/kits/eck_message_manifest.json +12 -0
- package/kits/provider-templates.json +1213 -0
- package/kits/rules/04-relation.md +3 -3
- package/kits/schemas/_meta.json +14 -2
- package/kits/schemas/agent-config.schema.20.json +24 -0
- package/kits/schemas/contact-book.schema.4.json +52 -0
- package/kits/schemas/providers.schema.3.json +200 -0
- package/kits/skills/.switches.json +8 -0
- package/kits/skills/ability/references/calling.md +1 -1
- package/kits/skills/agent-output-cards/SKILL.md +57 -0
- package/kits/skills/ec-group/SKILL.md +2 -2
- package/kits/skills/ec-identity/SKILL.md +19 -72
- package/kits/skills/ec-provider/SKILL.md +270 -0
- package/kits/skills/ec-provider/scripts/check-model.mjs +80 -0
- package/kits/skills/ec-trigger/SKILL.md +1 -1
- package/kits/skills/thread-ops/SKILL.md +34 -21
- package/kits/templates/message-fragments/group-batch-item.md +1 -1
- package/kits/templates/message-fragments/group-batch.md +1 -1
- package/kits/templates/message-fragments/inject-default.md +1 -1
- package/kits/templates/message-fragments/interrupt-insert.md +1 -0
- package/kits/templates/message-fragments/item-group.md +1 -1
- package/kits/templates/message-fragments/item-private.md +1 -1
- package/kits/templates/message-fragments/thread-context.md +1 -1
- package/kits/templates/message-fragments/trigger-session.md +1 -1
- package/kits/templates/message-fragments/trigger-thread-check.md +1 -1
- package/kits/templates/roles/admin.json +2 -19
- package/kits/templates/roles/member.json +0 -3
- package/kits/templates/roles/visitor.json +1 -0
- package/kits/templates/system-fragments/baseagent.md +6 -0
- package/kits/templates/system-fragments/commands.md +4 -3
- package/kits/templates/system-fragments/directory-contacts.md +3 -4
- package/kits/templates/system-fragments/managed-runtime.md +1 -1
- package/kits/templates/system-fragments/relation.md +1 -1
- package/kits/templates/system-fragments/thread.md +7 -5
- package/kits/templates/system-fragments/venue.md +11 -0
- package/package.json +6 -4
- package/scripts/agent-template/inspect.mjs +50 -11
- package/scripts/agent-template/shared/common.mjs +11 -6
- package/scripts/agent-template/shared/team-schema.json +6 -4
- package/scripts/agent-template/shared/template-contract.mjs +62 -11
- package/scripts/agent-template/shared/template-package.mjs +189 -21
- package/scripts/agent-template/shared/template-runtime.mjs +54 -6
- package/scripts/agent-template/shared/template.mjs +2 -2
- package/scripts/agent-template/team-pack.mjs +52 -14
- package/dist/cli/peer.js +0 -127
- package/dist/config/peer-operation-service.js +0 -132
- package/dist/config/peer-request-service.js +0 -532
- package/dist/core/command/connect-menu.js +0 -624
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto';
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
2
|
import { isDeepStrictEqual } from 'node:util';
|
|
3
3
|
import fs from 'node:fs';
|
|
4
4
|
import path, { posix } from 'node:path';
|
|
@@ -7,8 +7,398 @@ import { ConfigTarget, mutateConfig, notifyConfigWrite } from './config-manager.
|
|
|
7
7
|
import { atomicReadJson } from '../utils/atomic-write.js';
|
|
8
8
|
import { readProvidersFile, validateProvidersFile } from './providers.js';
|
|
9
9
|
import { initializationRuntimePlan, providerEnvironmentVariable, providerNameFromBaseUrlIfStatic, resolveProviderId, resolveProviderService, resolveProviderServiceForBaseagent, validateProviderName } from './providers-config.js';
|
|
10
|
-
import { providerIdentity } from './provider-identity.js';
|
|
10
|
+
import { providerIdentity as storedProviderIdentity } from './provider-identity.js';
|
|
11
11
|
import { planProviderIdentityMigration } from './provider-identity-migration.js';
|
|
12
|
+
import { isInteractionSendAccepted, sendInteractionPayload } from '../core/message/message-utils.js';
|
|
13
|
+
import { providerServiceCredentials, providerServiceTypeForBaseagent, resolveProviderBaseUrl } from './providers-config.js';
|
|
14
|
+
import { generateProviderTemplate, parseProviderTemplateInput } from './provider-templates.js';
|
|
15
|
+
/** Derive a missing identity during management without changing the stored URL reference. */
|
|
16
|
+
function providerIdentity(provider) {
|
|
17
|
+
if (provider.domain !== undefined || !providerEnvironmentVariable(provider.common.baseUrl)) {
|
|
18
|
+
return storedProviderIdentity(provider);
|
|
19
|
+
}
|
|
20
|
+
const baseUrl = resolveProviderBaseUrl('draft', provider.common.baseUrl);
|
|
21
|
+
return storedProviderIdentity({ ...provider, common: { ...provider.common, baseUrl } });
|
|
22
|
+
}
|
|
23
|
+
/** Shared template preview/merge; raw credentials never belong in a public response. */
|
|
24
|
+
export function prepareProviderTemplate(templateId, value, file = resolvePaths().providersConfig, draft) {
|
|
25
|
+
const input = parseProviderTemplateInput(value);
|
|
26
|
+
const registry = readProvidersFile(file);
|
|
27
|
+
let generated = generateProviderTemplate(templateId, input);
|
|
28
|
+
const identity = providerIdentity(generated.provider);
|
|
29
|
+
let existing = Object.entries(registry.providers).find(([, provider]) => providerIdentity(provider).domain === identity.domain);
|
|
30
|
+
if (draft) {
|
|
31
|
+
const previous = registry.providers[resolveProviderId(draft.providerId, registry)];
|
|
32
|
+
const provider = resolveDraftProvider(draft.value, previous, draft.imports);
|
|
33
|
+
if (providerIdentity(provider).domain !== identity.domain)
|
|
34
|
+
throw new Error('Template and draft domains differ');
|
|
35
|
+
existing = [draft.providerId, provider];
|
|
36
|
+
}
|
|
37
|
+
if (existing && generated.product === 'api' && input.serviceTypes === undefined
|
|
38
|
+
&& Object.keys(existing[1].services ?? {}).some(type => ['claude', 'codex', 'ecagent', 'gemini'].includes(type))) {
|
|
39
|
+
generated = generateProviderTemplate(templateId, input, ['text']);
|
|
40
|
+
}
|
|
41
|
+
const selected = Object.keys(generated.provider.services ?? {});
|
|
42
|
+
const provider = existing ? mergeImportedProvider(existing[1], generated.provider, new Set(selected), true) : generated.provider;
|
|
43
|
+
const normalized = validateProvidersFile({ providers: { draft: provider } }).providers.draft;
|
|
44
|
+
// Resolve each service to enforce the same origin and credential constraints before preview/save.
|
|
45
|
+
for (const type of Object.keys(normalized.services ?? {}))
|
|
46
|
+
resolveProviderService('draft', type, { providers: { draft: normalized } });
|
|
47
|
+
return { ...generated, provider: normalized, incoming: generated.provider, providerId: existing?.[0] ?? identity.providerId,
|
|
48
|
+
existing: !!existing, conflicts: selected.filter(type => Object.hasOwn(existing?.[1].services ?? {}, type)),
|
|
49
|
+
serviceTypes: selected, revision: providersRevision(registry), before: existing?.[1] };
|
|
50
|
+
}
|
|
51
|
+
export async function applyProviderTemplate(input, file = resolvePaths().providersConfig) {
|
|
52
|
+
const config = parseProviderTemplateInput(input.config);
|
|
53
|
+
if (!config.apiKey)
|
|
54
|
+
throw new Error('A product API key is required');
|
|
55
|
+
const draft = prepareProviderTemplate(input.templateId, config, file);
|
|
56
|
+
if (input.providerId !== undefined) {
|
|
57
|
+
const targetId = resolveProviderId(input.providerId, readProvidersFile(file));
|
|
58
|
+
if (!draft.existing || targetId !== draft.providerId)
|
|
59
|
+
throw new Error('Template domain does not match the target Provider');
|
|
60
|
+
const data = await updateProvider({ providerId: targetId, revision: input.revision ?? draft.revision, value: draft.provider }, file);
|
|
61
|
+
return { ...data, providerId: targetId };
|
|
62
|
+
}
|
|
63
|
+
if (draft.existing)
|
|
64
|
+
throw Object.assign(new Error('Provider domain already exists; explicitly update the existing Provider'), {
|
|
65
|
+
code: 'PROVIDER_CONFLICT', statusCode: 409,
|
|
66
|
+
data: { kind: 'provider_domain_exists', providerId: draft.providerId, domain: draft.provider.domain },
|
|
67
|
+
});
|
|
68
|
+
return createProvider({ revision: input.revision ?? draft.revision, value: draft.provider }, file);
|
|
69
|
+
}
|
|
70
|
+
export function previewProviderTemplate(templateId, config, context) {
|
|
71
|
+
const draft = prepareProviderTemplate(templateId, config, undefined, context);
|
|
72
|
+
const services = Object.keys(draft.provider.services ?? {}).map(serviceType => {
|
|
73
|
+
const connection = resolveProviderService('draft', serviceType, { providers: { draft: draft.provider } });
|
|
74
|
+
return { serviceType, style: connection.style, baseUrl: connection.baseUrl, catalogBaseUrl: connection.catalogBaseUrl,
|
|
75
|
+
credentialSource: Object.hasOwn(draft.provider.services[serviceType], 'apiKey') ? 'service' : 'common' };
|
|
76
|
+
});
|
|
77
|
+
return { templateId: draft.templateId, templateRevision: draft.templateRevision, revision: draft.revision,
|
|
78
|
+
providerId: draft.providerId, existing: draft.existing, conflicts: draft.conflicts, serviceTypes: draft.serviceTypes,
|
|
79
|
+
services,
|
|
80
|
+
provider: previewProviderValue(draft.provider).provider,
|
|
81
|
+
before: draft.before ? previewProviderValue(draft.before).provider : undefined,
|
|
82
|
+
references: draft.existing ? providerReferences(draft.providerId, resolvePaths().providersConfig) : [],
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Capture facts before an attempt; never infer a service or protocol from error text. */
|
|
86
|
+
export function providerConnectionContext(baseagent, providerId, nativeBaseUrl, nativeApiKey, nativeMetadata) {
|
|
87
|
+
if (!['claude', 'codex', 'gemini', 'ecagent'].includes(baseagent))
|
|
88
|
+
return undefined;
|
|
89
|
+
const registry = readProvidersFile();
|
|
90
|
+
const id = providerId ? resolveProviderId(providerId, registry) : undefined;
|
|
91
|
+
const provider = id ? registry.providers[id] : undefined;
|
|
92
|
+
if (id && !provider)
|
|
93
|
+
return undefined;
|
|
94
|
+
const serviceType = providerServiceTypeForBaseagent(baseagent, provider);
|
|
95
|
+
const service = provider?.services?.[serviceType];
|
|
96
|
+
if (provider && !service)
|
|
97
|
+
return undefined;
|
|
98
|
+
const style = ['claude', 'codex', 'gemini'].includes(serviceType) ? serviceType
|
|
99
|
+
: service?.style ?? 'openai-compatible';
|
|
100
|
+
const raw = provider?.common.baseUrl ?? nativeBaseUrl;
|
|
101
|
+
const variable = raw ? providerEnvironmentVariable(raw) : undefined;
|
|
102
|
+
let baseUrl = variable ? process.env[variable]?.trim() : raw;
|
|
103
|
+
if (baseUrl) {
|
|
104
|
+
try {
|
|
105
|
+
const url = new URL(baseUrl);
|
|
106
|
+
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search || url.hash)
|
|
107
|
+
baseUrl = undefined;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
baseUrl = undefined;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// A legacy unresolved reference without a hostname cannot establish which
|
|
114
|
+
// existing Provider identity the submitted URL is allowed to repair.
|
|
115
|
+
if (provider && !baseUrl && !provider.domain)
|
|
116
|
+
return undefined;
|
|
117
|
+
return { providerId: id, provider, baseagent, serviceType, style, path: service?.path,
|
|
118
|
+
baseUrl, nativeApiKey, nativeHeaders: nativeMetadata?.headers, nativeQueryParams: nativeMetadata?.queryParams,
|
|
119
|
+
environmentRevision: providerEnvironmentRevision(provider), revision: providersRevision(registry) };
|
|
120
|
+
}
|
|
121
|
+
/** Raw references can stay unchanged while the effective endpoint or key changes. */
|
|
122
|
+
function providerEnvironmentRevision(provider) {
|
|
123
|
+
const connections = [provider?.common, ...Object.values(provider?.services ?? {})];
|
|
124
|
+
return createHash('sha256').update(JSON.stringify(connections.map(connection => ['baseUrl', 'apiKey'].map(field => {
|
|
125
|
+
const raw = connection?.[field];
|
|
126
|
+
const variable = raw ? providerEnvironmentVariable(raw) : undefined;
|
|
127
|
+
return variable ? process.env[variable] : raw;
|
|
128
|
+
})))).digest('hex');
|
|
129
|
+
}
|
|
130
|
+
/** Known protocol authentication fields must not keep overriding the repaired key. */
|
|
131
|
+
function replaceConnectionKey(provider, apiKey, serviceType) {
|
|
132
|
+
const target = serviceType ? provider.services?.[serviceType] : provider.common;
|
|
133
|
+
if (!target)
|
|
134
|
+
throw new Error('Provider repair service no longer exists');
|
|
135
|
+
if (serviceType) {
|
|
136
|
+
const effective = providerServiceCredentials(provider, serviceType);
|
|
137
|
+
if (effective.headers)
|
|
138
|
+
target.headers = { ...effective.headers };
|
|
139
|
+
if (effective.queryParams)
|
|
140
|
+
target.queryParams = { ...effective.queryParams };
|
|
141
|
+
}
|
|
142
|
+
target.apiKey = apiKey;
|
|
143
|
+
for (const name of Object.keys(target.headers ?? {})) {
|
|
144
|
+
const lower = name.toLowerCase();
|
|
145
|
+
if (lower === 'authorization' && /^Bearer\s/iu.test(target.headers[name])) {
|
|
146
|
+
target.headers[name] = `Bearer ${apiKey}`;
|
|
147
|
+
}
|
|
148
|
+
else if (['x-api-key', 'x-goog-api-key', 'api-key'].includes(lower)) {
|
|
149
|
+
target.headers[name] = apiKey;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const name of Object.keys(target.queryParams ?? {})) {
|
|
153
|
+
if (['key', 'api_key', 'apikey', 'access_token'].includes(name.toLowerCase())) {
|
|
154
|
+
target.queryParams[name] = apiKey;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/** Daemon-only staged connection repair. No answer is persisted until all fields are collected. */
|
|
159
|
+
export class ProviderConnectionQuestions {
|
|
160
|
+
router;
|
|
161
|
+
pending = new Map();
|
|
162
|
+
constructor(router) {
|
|
163
|
+
this.router = router;
|
|
164
|
+
}
|
|
165
|
+
async present(input) {
|
|
166
|
+
const { context, session, message, adapter, rule } = input;
|
|
167
|
+
if (rule.action !== 'ask' || !rule.question || !adapter.capabilities?.interaction || message.peerType !== 'human'
|
|
168
|
+
|| message.chatType === 'group' || (message.channelType ?? message.channel.split('#')[0]) !== 'aun'
|
|
169
|
+
|| !message.peerId || !(await input.authorize()))
|
|
170
|
+
return false;
|
|
171
|
+
const key = `${context.providerId ?? session.id}:${context.serviceType}`;
|
|
172
|
+
const previous = this.pending.get(key);
|
|
173
|
+
if (previous) {
|
|
174
|
+
if (await previous.isCurrent().catch(() => false))
|
|
175
|
+
return true;
|
|
176
|
+
await previous.cancel();
|
|
177
|
+
}
|
|
178
|
+
if (!(await input.isCurrent()) || providersRevision(readProvidersFile()) !== context.revision
|
|
179
|
+
|| providerEnvironmentRevision(context.provider) !== context.environmentRevision)
|
|
180
|
+
return false;
|
|
181
|
+
// Authorization/binding checks yield. Recheck before claiming the pending slot.
|
|
182
|
+
if (this.pending.has(key))
|
|
183
|
+
return true;
|
|
184
|
+
const fields = new Set(rule.question.fields);
|
|
185
|
+
const supplied = (field) => {
|
|
186
|
+
const raw = field === 'baseUrl' ? context.baseUrl : context.provider
|
|
187
|
+
? providerServiceCredentials(context.provider, context.serviceType).apiKey : context.nativeApiKey;
|
|
188
|
+
const variable = raw ? providerEnvironmentVariable(raw) : undefined;
|
|
189
|
+
return variable ? process.env[variable] : raw;
|
|
190
|
+
};
|
|
191
|
+
for (const field of rule.question.includeMissing ?? [])
|
|
192
|
+
if (!supplied(field)?.trim())
|
|
193
|
+
fields.add(field);
|
|
194
|
+
// The endpoint must be known before a credential is collected.
|
|
195
|
+
const remaining = ['baseUrl', 'apiKey'].filter(field => fields.has(field));
|
|
196
|
+
const staged = {};
|
|
197
|
+
const token = Symbol(key);
|
|
198
|
+
let closed = false;
|
|
199
|
+
let activeId;
|
|
200
|
+
let deadline = Infinity;
|
|
201
|
+
const live = (id) => !closed && Date.now() < deadline && this.pending.get(key)?.token === token
|
|
202
|
+
&& (id === undefined || activeId === id);
|
|
203
|
+
const finish = () => {
|
|
204
|
+
closed = true;
|
|
205
|
+
delete staged.apiKey;
|
|
206
|
+
delete staged.baseUrl;
|
|
207
|
+
if (this.pending.get(key)?.token === token)
|
|
208
|
+
this.pending.delete(key);
|
|
209
|
+
};
|
|
210
|
+
const envelope = { ...input.envelope, replyContext: { ...input.envelope.replyContext,
|
|
211
|
+
delivery: { chatType: 'private' }, metadata: { ...input.envelope.replyContext?.metadata, encrypted: true } } };
|
|
212
|
+
const notice = async (text) => {
|
|
213
|
+
await adapter.send(envelope, { kind: 'system.notice', text, subtype: 'provider_connection' });
|
|
214
|
+
};
|
|
215
|
+
const valid = async (id) => live(id) && await input.authorize()
|
|
216
|
+
&& live(id) && await input.isCurrent() && live(id)
|
|
217
|
+
&& providersRevision(readProvidersFile()) === context.revision
|
|
218
|
+
&& providerEnvironmentRevision(context.provider) === context.environmentRevision;
|
|
219
|
+
this.pending.set(key, { token,
|
|
220
|
+
isCurrent: () => activeId ? valid(activeId) : Promise.resolve(false),
|
|
221
|
+
cancel: async () => {
|
|
222
|
+
if (activeId)
|
|
223
|
+
await this.router.cancel(activeId, 'connection_changed');
|
|
224
|
+
finish();
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
const next = async (hint) => {
|
|
228
|
+
if (!live())
|
|
229
|
+
return false;
|
|
230
|
+
const field = remaining[0];
|
|
231
|
+
const id = `pcq-${randomUUID()}`;
|
|
232
|
+
activeId = id;
|
|
233
|
+
deadline = Date.now() + 15 * 60_000;
|
|
234
|
+
const target = context.provider?.name ?? context.providerId ?? '当前模型连接';
|
|
235
|
+
const interaction = {
|
|
236
|
+
type: 'interaction', id, channelId: message.channelId, sessionId: session.id,
|
|
237
|
+
initiatorId: message.peerId, blocking: false, sensitive: true, expiresAt: deadline,
|
|
238
|
+
kind: { kind: 'action', title: field === 'baseUrl' ? '补充服务地址' : '更新 API Key / Token',
|
|
239
|
+
body: `${target}\n${hint ?? rule.message ?? '请补充连接信息'}\n`
|
|
240
|
+
+ (field === 'baseUrl' ? '请输入完整 baseURL;已有服务路径保持不变。'
|
|
241
|
+
: `服务地址:${staged.baseUrl ?? context.baseUrl ?? '未配置'}\n请输入 API Key / Token,仅保存到宿主服务商配置。`),
|
|
242
|
+
buttons: [{ key: 'cancel', label: '取消' }], allowCustomInput: true },
|
|
243
|
+
};
|
|
244
|
+
this.router.register(id, session.id, async (action, values) => {
|
|
245
|
+
if (!live(id))
|
|
246
|
+
return false;
|
|
247
|
+
if (action === 'cancel' || action === 'dismiss' || values?.behavior === 'dismiss') {
|
|
248
|
+
finish();
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
if (!(await valid(id))) {
|
|
252
|
+
if (live(id)) {
|
|
253
|
+
finish();
|
|
254
|
+
await notice('连接配置或管理权限已变化,请重新发起。');
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
const raw = action === '_custom_input' ? values?.custom_text : undefined;
|
|
259
|
+
const value = typeof raw === 'string' ? raw.trim() : '';
|
|
260
|
+
let acceptable = !!value && value.length <= (field === 'baseUrl' ? 4096 : 16384)
|
|
261
|
+
&& !/[\r\n\0]/u.test(value) && value !== PROVIDER_SECRET_MASK && !providerEnvironmentVariable(value);
|
|
262
|
+
if (field === 'baseUrl' && acceptable) {
|
|
263
|
+
try {
|
|
264
|
+
const parsed = new URL(value);
|
|
265
|
+
acceptable = ['http:', 'https:'].includes(parsed.protocol) && !parsed.username && !parsed.password
|
|
266
|
+
&& !parsed.search && !parsed.hash;
|
|
267
|
+
if (acceptable && context.provider) {
|
|
268
|
+
const identity = providerIdentity({ ...context.provider,
|
|
269
|
+
common: { ...context.provider.common, baseUrl: value } });
|
|
270
|
+
acceptable = identity.domain === providerIdentity(context.provider).domain;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
acceptable = false;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else if (acceptable) {
|
|
278
|
+
try {
|
|
279
|
+
new Headers({ authorization: `Bearer ${value}` });
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
acceptable = false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (!acceptable) {
|
|
286
|
+
await next(field === 'baseUrl' ? '地址无效,请重新填写;已有服务商不能更换域名。'
|
|
287
|
+
: '密钥格式无效,请填写原始的单行 API Key / Token。');
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
staged[field] = value;
|
|
291
|
+
remaining.shift();
|
|
292
|
+
if (remaining.length) {
|
|
293
|
+
await next();
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
let saved = false;
|
|
297
|
+
try {
|
|
298
|
+
// Validate the entire candidate and CAS-write through existing Provider management.
|
|
299
|
+
const candidate = context.provider ? structuredClone(context.provider) : {
|
|
300
|
+
name: `${context.baseagent} API`,
|
|
301
|
+
common: { baseUrl: staged.baseUrl ?? context.baseUrl ?? '',
|
|
302
|
+
...(context.nativeApiKey ? { apiKey: context.nativeApiKey } : {}),
|
|
303
|
+
...(context.nativeHeaders ? { headers: { ...context.nativeHeaders } } : {}),
|
|
304
|
+
...(context.nativeQueryParams ? { queryParams: { ...context.nativeQueryParams } } : {}) },
|
|
305
|
+
services: { [context.serviceType]: { style: context.style, path: context.path ?? '' } },
|
|
306
|
+
};
|
|
307
|
+
if (staged.baseUrl)
|
|
308
|
+
candidate.common.baseUrl = staged.baseUrl;
|
|
309
|
+
if (staged.apiKey)
|
|
310
|
+
replaceConnectionKey(candidate, staged.apiKey, context.provider ? context.serviceType : undefined);
|
|
311
|
+
let providerId = context.providerId;
|
|
312
|
+
if (providerId) {
|
|
313
|
+
await updateProvider({ providerId, revision: context.revision, value: candidate });
|
|
314
|
+
saved = true;
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
providerId = (await createProvider({ revision: context.revision, value: candidate })).providerId;
|
|
318
|
+
saved = true;
|
|
319
|
+
try {
|
|
320
|
+
await input.bindCreated(providerId);
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
finish();
|
|
324
|
+
await notice('服务商已保存,但原连接已变化,未绑定。请在模型设置中选择服务商。');
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const verified = await input.verify(providerId, context.serviceType).catch(() => false);
|
|
329
|
+
if (!live(id))
|
|
330
|
+
return true;
|
|
331
|
+
await notice(verified ? '服务商连接已保存,目标模型调用验证通过。' : '服务商连接已保存,目标模型调用暂未验证通过,请检查模型及凭据后重试。');
|
|
332
|
+
const canRetry = live(id);
|
|
333
|
+
finish();
|
|
334
|
+
if (verified && canRetry && input.retry) {
|
|
335
|
+
try {
|
|
336
|
+
await this.offerRetry(input, envelope, providerId);
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
await notice('连接已保存,请重新发送原请求继续。');
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
finish();
|
|
345
|
+
await notice(saved ? '服务商连接已保存,后续验证或提示未完成,请在管理界面检查。'
|
|
346
|
+
: '服务商连接未保存,配置已变化或输入不符合要求,请重新发起。').catch(() => undefined);
|
|
347
|
+
}
|
|
348
|
+
return true;
|
|
349
|
+
}, { blocking: false, requireEncrypted: true, initiatorId: message.peerId,
|
|
350
|
+
initiatorChannelKey: adapter.channelKey, timeoutMs: 15 * 60_000,
|
|
351
|
+
onCancel: async () => {
|
|
352
|
+
if (activeId === id)
|
|
353
|
+
finish();
|
|
354
|
+
await adapter.invalidateInteraction?.(id, 'cancelled');
|
|
355
|
+
} });
|
|
356
|
+
const receipt = await sendInteractionPayload(adapter, { ...envelope, operationId: id }, interaction, '请通过加密卡片补充服务商连接;不支持卡片时请使用宿主管理界面。');
|
|
357
|
+
if (!isInteractionSendAccepted(receipt)) {
|
|
358
|
+
await this.router.cancel(id, 'delivery_failed');
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
return true;
|
|
362
|
+
};
|
|
363
|
+
try {
|
|
364
|
+
return await next();
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
if (activeId)
|
|
368
|
+
await this.router.cancel(activeId, 'delivery_failed');
|
|
369
|
+
finish();
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
async offerRetry(input, envelope, providerId) {
|
|
374
|
+
const id = `pcq-${randomUUID()}`;
|
|
375
|
+
let active = true;
|
|
376
|
+
const expiresAt = Date.now() + 15 * 60_000;
|
|
377
|
+
const revision = providersRevision(readProvidersFile());
|
|
378
|
+
const environmentRevision = providerEnvironmentRevision(readProvidersFile().providers[providerId]);
|
|
379
|
+
const request = { type: 'interaction', id, sessionId: input.session.id,
|
|
380
|
+
channelId: input.message.channelId, initiatorId: input.message.peerId, blocking: false, sensitive: true,
|
|
381
|
+
expiresAt,
|
|
382
|
+
kind: { kind: 'action', title: '连接已修复', body: '可重新执行原请求,或关闭此卡片。',
|
|
383
|
+
buttons: [{ key: 'retry', label: '重试原请求' }, { key: 'cancel', label: '关闭' }] } };
|
|
384
|
+
this.router.register(id, input.session.id, async (action) => {
|
|
385
|
+
if (action !== 'retry')
|
|
386
|
+
return true;
|
|
387
|
+
if (!(await input.authorize()) || !(await input.isCurrent())
|
|
388
|
+
|| !active || Date.now() >= expiresAt
|
|
389
|
+
|| providersRevision(readProvidersFile()) !== revision || !readProvidersFile().providers[providerId]
|
|
390
|
+
|| providerEnvironmentRevision(readProvidersFile().providers[providerId]) !== environmentRevision)
|
|
391
|
+
return false;
|
|
392
|
+
await input.retry?.();
|
|
393
|
+
return true;
|
|
394
|
+
}, { blocking: false, requireEncrypted: true, initiatorId: input.message.peerId,
|
|
395
|
+
initiatorChannelKey: input.adapter.channelKey, timeoutMs: 15 * 60_000,
|
|
396
|
+
onCancel: async () => { active = false; await input.adapter.invalidateInteraction?.(id, 'cancelled'); } });
|
|
397
|
+
const receipt = await sendInteractionPayload(input.adapter, { ...envelope, operationId: id }, request, '连接已保存,请重新发送原请求继续。');
|
|
398
|
+
if (!isInteractionSendAccepted(receipt))
|
|
399
|
+
await this.router.cancel(id, 'delivery_failed');
|
|
400
|
+
}
|
|
401
|
+
}
|
|
12
402
|
export const PROVIDER_SECRET_MASK = '<redacted>';
|
|
13
403
|
function providerReferences(providerId, providerFile) {
|
|
14
404
|
const registry = readProvidersFile(providerFile);
|
|
@@ -126,25 +516,39 @@ export function readProviderForExport(providerId, serviceType, file = resolvePat
|
|
|
126
516
|
if (!provider)
|
|
127
517
|
throw Object.assign(new Error('Provider not found'), { statusCode: 404 });
|
|
128
518
|
const exported = structuredClone(provider);
|
|
519
|
+
if (serviceType !== undefined) {
|
|
520
|
+
if (!exported.services?.[serviceType])
|
|
521
|
+
throw Object.assign(new Error('Service type not found'), { statusCode: 404 });
|
|
522
|
+
exported.services = { [serviceType]: exported.services[serviceType] };
|
|
523
|
+
// A service export must not include another product's inherited credentials.
|
|
524
|
+
for (const field of ['apiKey', 'headers', 'queryParams']) {
|
|
525
|
+
const effective = providerServiceCredentials(provider, serviceType)[field];
|
|
526
|
+
if (effective === undefined)
|
|
527
|
+
delete exported.common[field];
|
|
528
|
+
else
|
|
529
|
+
Object.assign(exported.common, { [field]: structuredClone(effective) });
|
|
530
|
+
}
|
|
531
|
+
}
|
|
129
532
|
if (!includeApiKey)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return { ...exported, services: { [serviceType]: structuredClone(service) } };
|
|
533
|
+
for (const connection of [exported.common, ...Object.values(exported.services ?? {})]) {
|
|
534
|
+
delete connection.apiKey;
|
|
535
|
+
delete connection.headers;
|
|
536
|
+
delete connection.queryParams;
|
|
537
|
+
}
|
|
538
|
+
return exported;
|
|
137
539
|
}
|
|
138
540
|
function providersSnapshot(value) {
|
|
139
541
|
const providers = structuredClone(value.providers);
|
|
140
542
|
for (const provider of Object.values(providers)) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
543
|
+
for (const connection of [provider.common, ...Object.values(provider.services ?? {})]) {
|
|
544
|
+
if (connection.apiKey && !providerEnvironmentVariable(connection.apiKey))
|
|
545
|
+
connection.apiKey = PROVIDER_SECRET_MASK;
|
|
546
|
+
for (const field of ['headers', 'queryParams']) {
|
|
547
|
+
const values = connection[field];
|
|
548
|
+
if (values)
|
|
549
|
+
for (const key of Object.keys(values))
|
|
550
|
+
values[key] = PROVIDER_SECRET_MASK;
|
|
551
|
+
}
|
|
148
552
|
}
|
|
149
553
|
}
|
|
150
554
|
return {
|
|
@@ -161,22 +565,27 @@ function retainSecrets(value, previous) {
|
|
|
161
565
|
const provider = structuredClone(value);
|
|
162
566
|
if (!provider.common || typeof provider.common !== 'object' || Array.isArray(provider.common))
|
|
163
567
|
return provider;
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
common.apiKey = previous.common.apiKey;
|
|
169
|
-
}
|
|
170
|
-
for (const field of ['headers', 'queryParams']) {
|
|
171
|
-
if (!common[field] || typeof common[field] !== 'object' || Array.isArray(common[field]))
|
|
568
|
+
const connections = [[provider.common, previous?.common],
|
|
569
|
+
...Object.entries(provider.services ?? {}).map(([type, service]) => [service, previous?.services?.[type]])];
|
|
570
|
+
for (const [common, old] of connections) {
|
|
571
|
+
if (!common || typeof common !== 'object')
|
|
172
572
|
continue;
|
|
173
|
-
|
|
174
|
-
if (
|
|
573
|
+
if (common.apiKey === PROVIDER_SECRET_MASK) {
|
|
574
|
+
if (old?.apiKey === undefined)
|
|
575
|
+
throw new Error('A replacement API key is required');
|
|
576
|
+
common.apiKey = old.apiKey;
|
|
577
|
+
}
|
|
578
|
+
for (const field of ['headers', 'queryParams']) {
|
|
579
|
+
if (!common[field] || typeof common[field] !== 'object' || Array.isArray(common[field]))
|
|
175
580
|
continue;
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
581
|
+
for (const key of Object.keys(common[field])) {
|
|
582
|
+
if (common[field][key] !== PROVIDER_SECRET_MASK)
|
|
583
|
+
continue;
|
|
584
|
+
const existing = old?.[field]?.[key];
|
|
585
|
+
if (existing === undefined)
|
|
586
|
+
throw new Error(`A replacement ${field} value is required`);
|
|
587
|
+
common[field][key] = existing;
|
|
588
|
+
}
|
|
180
589
|
}
|
|
181
590
|
}
|
|
182
591
|
return provider;
|
|
@@ -231,13 +640,15 @@ export async function updateProvider(input, file = resolvePaths().providersConfi
|
|
|
231
640
|
}
|
|
232
641
|
function rejectMaskedImportSecrets(providers) {
|
|
233
642
|
for (const [providerId, provider] of Object.entries(providers)) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
for (const
|
|
239
|
-
|
|
240
|
-
|
|
643
|
+
for (const connection of [provider.common, ...Object.values(provider.services ?? {})]) {
|
|
644
|
+
if (connection.apiKey === PROVIDER_SECRET_MASK) {
|
|
645
|
+
throw new Error(`Provider ${providerId} has a redacted API key; import a file with the original secret`);
|
|
646
|
+
}
|
|
647
|
+
for (const field of ['headers', 'queryParams']) {
|
|
648
|
+
for (const [key, value] of Object.entries(connection[field] ?? {})) {
|
|
649
|
+
if (value === PROVIDER_SECRET_MASK) {
|
|
650
|
+
throw new Error(`Provider ${providerId} ${field}.${key} is redacted; import a file with the original secret`);
|
|
651
|
+
}
|
|
241
652
|
}
|
|
242
653
|
}
|
|
243
654
|
}
|
|
@@ -302,7 +713,7 @@ function assertProviderDomainUnchanged(previous, next) {
|
|
|
302
713
|
}
|
|
303
714
|
catch (error) {
|
|
304
715
|
const code = error?.code;
|
|
305
|
-
if (previous.domain === undefined &&
|
|
716
|
+
if (previous.domain === undefined && ['PROVIDER_DOMAIN_REQUIRED', 'PROVIDER_DOMAIN_INVALID', 'PROVIDER_BASE_URL_MISSING'].includes(code ?? '')) {
|
|
306
717
|
if (previous.common.baseUrl === next.common.baseUrl) {
|
|
307
718
|
if (next.domain !== undefined)
|
|
308
719
|
next.domain = providerIdentity(next).domain;
|
|
@@ -444,24 +855,34 @@ function providerHostname(baseUrl) {
|
|
|
444
855
|
return undefined;
|
|
445
856
|
return new URL(baseUrl).hostname.toLowerCase().replace(/\.$/u, '');
|
|
446
857
|
}
|
|
447
|
-
function mergeImportedProvider(existing, imported, replaceServiceTypes = new Set()) {
|
|
858
|
+
function mergeImportedProvider(existing, imported, replaceServiceTypes = new Set(), preserveCommon = false) {
|
|
448
859
|
const services = structuredClone(existing.services || {});
|
|
860
|
+
const common = { ...existing.common,
|
|
861
|
+
...(!preserveCommon && !existing.common.apiKey?.trim() && imported.common.apiKey ? { apiKey: imported.common.apiKey } : {}),
|
|
862
|
+
};
|
|
449
863
|
const existingVariable = providerEnvironmentVariable(existing.common.baseUrl);
|
|
450
864
|
const importedVariable = providerEnvironmentVariable(imported.common.baseUrl);
|
|
865
|
+
const importService = (type) => {
|
|
866
|
+
const result = structuredClone(imported.services[type]);
|
|
867
|
+
const credentials = providerServiceCredentials(imported, type);
|
|
868
|
+
for (const field of ['apiKey', 'headers', 'queryParams']) {
|
|
869
|
+
if (result[field] === undefined && !isDeepStrictEqual(credentials[field], common[field])) {
|
|
870
|
+
Object.assign(result, { [field]: credentials[field] ?? (field === 'apiKey' ? '' : {}) });
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
return result;
|
|
874
|
+
};
|
|
451
875
|
if (existingVariable || importedVariable) {
|
|
452
876
|
if (existingVariable !== importedVariable) {
|
|
453
877
|
throw new Error('Cannot merge Providers with different environment-variable base URLs');
|
|
454
878
|
}
|
|
455
879
|
for (const [serviceType, service] of Object.entries(imported.services || {})) {
|
|
456
880
|
if (replaceServiceTypes.has(serviceType) || !Object.hasOwn(services, serviceType))
|
|
457
|
-
services[serviceType] =
|
|
881
|
+
services[serviceType] = importService(serviceType);
|
|
458
882
|
}
|
|
459
883
|
return {
|
|
460
884
|
...existing,
|
|
461
|
-
common
|
|
462
|
-
...existing.common,
|
|
463
|
-
...(!existing.common.apiKey?.trim() && imported.common.apiKey ? { apiKey: imported.common.apiKey } : {}),
|
|
464
|
-
},
|
|
885
|
+
common,
|
|
465
886
|
services,
|
|
466
887
|
};
|
|
467
888
|
}
|
|
@@ -474,14 +895,17 @@ function mergeImportedProvider(existing, imported, replaceServiceTypes = new Set
|
|
|
474
895
|
throw new Error('Cannot merge this domain: the source uses a different protocol or port');
|
|
475
896
|
}
|
|
476
897
|
const relativePath = posix.relative(baseUrl.pathname.replace(/\/+$/u, '') || '/', endpoint.pathname.replace(/\/+$/u, '') || '/');
|
|
477
|
-
services[serviceType] = { ...
|
|
898
|
+
services[serviceType] = { ...importService(serviceType), path: relativePath ? `./${relativePath}` : '' };
|
|
899
|
+
if (service.catalogPath !== undefined) {
|
|
900
|
+
const sourceBase = imported.common.baseUrl.replace(/\/+$/u, '') + '/';
|
|
901
|
+
const directory = new URL(service.catalogPath, sourceBase);
|
|
902
|
+
const relativeCatalog = posix.relative(baseUrl.pathname.replace(/\/+$/u, '') || '/', directory.pathname);
|
|
903
|
+
services[serviceType].catalogPath = relativeCatalog ? `./${relativeCatalog}` : '';
|
|
904
|
+
}
|
|
478
905
|
}
|
|
479
906
|
return {
|
|
480
907
|
...existing,
|
|
481
|
-
common
|
|
482
|
-
...existing.common,
|
|
483
|
-
...(!existing.common.apiKey?.trim() && imported.common.apiKey ? { apiKey: imported.common.apiKey } : {}),
|
|
484
|
-
},
|
|
908
|
+
common,
|
|
485
909
|
services,
|
|
486
910
|
};
|
|
487
911
|
}
|
|
@@ -490,12 +914,14 @@ function resolveDraftProvider(value, previous, imports = []) {
|
|
|
490
914
|
throw new Error('Provider imports must be an array');
|
|
491
915
|
let secretSource = previous;
|
|
492
916
|
for (const item of imports) {
|
|
493
|
-
const
|
|
917
|
+
const source = item && typeof item === 'object' && Object.hasOwn(item, 'templateProvider')
|
|
918
|
+
? item : undefined;
|
|
919
|
+
const imported = validateProvidersFile({ providers: { imported: source ? source.templateProvider : item } }).providers.imported;
|
|
494
920
|
rejectMaskedImportSecrets({ imported });
|
|
495
921
|
if (secretSource && providerIdentity(secretSource).domain !== providerIdentity(imported).domain) {
|
|
496
922
|
throw new Error('Imported Provider domain does not match the target Provider');
|
|
497
923
|
}
|
|
498
|
-
secretSource = secretSource ? mergeImportedProvider(secretSource, imported) : imported;
|
|
924
|
+
secretSource = secretSource ? mergeImportedProvider(secretSource, imported, source ? new Set(Object.keys(imported.services ?? {})) : undefined, !!source) : imported;
|
|
499
925
|
}
|
|
500
926
|
const provider = validateProvidersFile({ providers: { draft: retainSecrets(value, secretSource) } }).providers.draft;
|
|
501
927
|
if (imports.length && secretSource && providerIdentity(provider).domain !== providerIdentity(secretSource).domain) {
|
|
@@ -122,14 +122,15 @@ function resolveFieldCommand(argv, subcommand, options = {}) {
|
|
|
122
122
|
reason: 'Agent role policies are managed through the role policy editor',
|
|
123
123
|
};
|
|
124
124
|
}
|
|
125
|
+
// defaults.json 仍只允许手工编辑;Agent 级的 baseagents.<ba>.fallback 走正常字段写入。
|
|
125
126
|
const canContainFallback = field === 'baseagents'
|
|
126
127
|
|| /^baseagents\.[^.]+$/.test(field)
|
|
127
128
|
|| /^baseagents\.[^.]+\.fallback(?:\.|$)/.test(field);
|
|
128
|
-
if (subcommand !== 'get' && canContainFallback) {
|
|
129
|
+
if (subcommand !== 'get' && canContainFallback && selector.scope === 'defaults') {
|
|
129
130
|
return {
|
|
130
131
|
ok: false,
|
|
131
132
|
code: 'DEFAULTS_FILE_ONLY',
|
|
132
|
-
reason: 'Baseagent fallback is configured
|
|
133
|
+
reason: 'Baseagent fallback is configured in agents/defaults.json; edit that file and run `ec config validate --default`',
|
|
133
134
|
};
|
|
134
135
|
}
|
|
135
136
|
const fieldRule = resolveConfigFieldRule(field, selector.scope);
|