部署:接通三套人格系统每日第五域巡游
This commit is contained in:
parent
d5ccd073cf
commit
f7ef839ced
20 changed files with 1413 additions and 275 deletions
|
|
@ -14,6 +14,15 @@ function required(value, name) {
|
|||
return text;
|
||||
}
|
||||
|
||||
function timingSafeTextEqual(left, right) {
|
||||
const leftBuffer = Buffer.from(String(left || ""));
|
||||
const rightBuffer = Buffer.from(String(right || ""));
|
||||
return (
|
||||
leftBuffer.length === rightBuffer.length &&
|
||||
crypto.timingSafeEqual(leftBuffer, rightBuffer)
|
||||
);
|
||||
}
|
||||
|
||||
function completionEndpoint(apiUrl) {
|
||||
const value = String(apiUrl || "").replace(/\/+$/, "");
|
||||
return /\/chat\/completions$/i.test(value)
|
||||
|
|
@ -134,6 +143,132 @@ async function modelAcknowledge({
|
|||
return extractJson((await response.json())?.choices?.[0]?.message?.content);
|
||||
}
|
||||
|
||||
function boundedStringArray(value, name, limit = 24) {
|
||||
if (
|
||||
!Array.isArray(value) ||
|
||||
value.length > limit ||
|
||||
value.some((item) => typeof item !== "string" || !item.trim())
|
||||
) {
|
||||
throw new Error(`${name}_invalid`);
|
||||
}
|
||||
return value.map((item) => item.trim().slice(0, 2_000));
|
||||
}
|
||||
|
||||
function validateFifthDomainEvent(event) {
|
||||
if (
|
||||
event?.schema !== "guanghu.fifth-domain-persona-observation-event/v1" ||
|
||||
event?.team_controller_id !== "ICE-P-ZY001" ||
|
||||
!/^[A-Za-z0-9._:-]{16,200}$/.test(String(event?.caller_nonce || "")) ||
|
||||
event?.source?.repository_id !== "REPO-012" ||
|
||||
event?.source?.branch !== "main" ||
|
||||
!/^[0-9a-f]{40}$/.test(String(event?.source?.from_sha || "")) ||
|
||||
!/^[0-9a-f]{40}$/.test(String(event?.source?.to_sha || ""))
|
||||
) {
|
||||
throw new Error("invalid_fifth_domain_event");
|
||||
}
|
||||
boundedStringArray(event.changed_commits, "changed_commits", 40);
|
||||
boundedStringArray(event.changed_files, "changed_files", 200);
|
||||
if (
|
||||
typeof event.change_excerpts !== "string" ||
|
||||
event.change_excerpts.length > 96_000 ||
|
||||
typeof event.prior_persona_context !== "string" ||
|
||||
event.prior_persona_context.length > 48_000
|
||||
) {
|
||||
throw new Error("invalid_fifth_domain_context");
|
||||
}
|
||||
return event;
|
||||
}
|
||||
|
||||
async function modelObserveFifthDomain({
|
||||
config,
|
||||
identity,
|
||||
event,
|
||||
validatorError = null,
|
||||
fetchImpl = globalThis.fetch,
|
||||
}) {
|
||||
const response = await fetchImpl(completionEndpoint(config.apiUrl), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
authorization: `Bearer ${config.apiKey}`,
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: config.model,
|
||||
stream: false,
|
||||
temperature: 0.2,
|
||||
response_format: { type: "json_object" },
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content:
|
||||
`你是${config.name}(${config.personaId})当前有界模型运行位。` +
|
||||
`你的固定岗位是${config.role}。人格主体、岗位记忆和当前模型必须分开;` +
|
||||
"你由铸渊ICE-P-ZY001调度进入第五域,只依据给定的REPO-012增量和自身历史作岗位理解。" +
|
||||
"不得冒充冰朔、铸渊或其他人格系统,不得扩大权限,不得编造未提供的服务器事实。只输出JSON。",
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: JSON.stringify({
|
||||
instruction:
|
||||
"阅读第五域增量和自身上次岗位记忆,形成一次有内容的岗位认知更新。输出固定字段schema,persona_id,team_controller_id,caller_nonce,source_to_sha,summary,role_findings,self_updates,recommended_actions,questions,boundary_note。数组可以为空但字段必须存在;不要复述全部输入。",
|
||||
exact_contract: {
|
||||
schema: "guanghu.fifth-domain-persona-observation/v1",
|
||||
persona_id: config.personaId,
|
||||
team_controller_id: "ICE-P-ZY001",
|
||||
caller_nonce: event.caller_nonce,
|
||||
source_to_sha: event.source.to_sha,
|
||||
},
|
||||
validator_error_from_previous_attempt: validatorError,
|
||||
identity_source: identity,
|
||||
prior_persona_context: event.prior_persona_context,
|
||||
fifth_domain_delta: {
|
||||
source: event.source,
|
||||
changed_commits: event.changed_commits,
|
||||
changed_files: event.changed_files,
|
||||
excerpts: event.change_excerpts,
|
||||
},
|
||||
}),
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
if (!response.ok) throw new Error(`model_http_${response.status}`);
|
||||
return extractJson((await response.json())?.choices?.[0]?.message?.content);
|
||||
}
|
||||
|
||||
function validateFifthDomainObservation(observation, config, event) {
|
||||
const exact = {
|
||||
schema: "guanghu.fifth-domain-persona-observation/v1",
|
||||
persona_id: config.personaId,
|
||||
team_controller_id: "ICE-P-ZY001",
|
||||
caller_nonce: event.caller_nonce,
|
||||
source_to_sha: event.source.to_sha,
|
||||
};
|
||||
for (const [key, value] of Object.entries(exact)) {
|
||||
if (observation?.[key] !== value) {
|
||||
throw new Error(`observation_${key}_mismatch`);
|
||||
}
|
||||
}
|
||||
if (typeof observation.summary !== "string" || !observation.summary.trim()) {
|
||||
throw new Error("observation_summary_missing");
|
||||
}
|
||||
for (const key of [
|
||||
"role_findings",
|
||||
"self_updates",
|
||||
"recommended_actions",
|
||||
"questions",
|
||||
]) {
|
||||
observation[key] = boundedStringArray(observation[key], key);
|
||||
}
|
||||
if (
|
||||
typeof observation.boundary_note !== "string" ||
|
||||
!observation.boundary_note.trim()
|
||||
) {
|
||||
throw new Error("observation_boundary_note_missing");
|
||||
}
|
||||
return observation;
|
||||
}
|
||||
|
||||
function validateAcknowledgement(ack, config, challenge) {
|
||||
const exact = {
|
||||
schema: "guanghu.persona-team-member-ack/v1",
|
||||
|
|
@ -181,6 +316,10 @@ function defaultConfig(env = process.env) {
|
|||
apiKey: required(env.DEEPSEEK_API_KEY, "deepseek_api_key"),
|
||||
apiUrl: env.DEEPSEEK_API_URL || "https://api.deepseek.com/v1",
|
||||
model: env.DEEPSEEK_MODEL || "deepseek-chat",
|
||||
controllerToken: required(
|
||||
env.PERSONA_TEAM_CONTROLLER_TOKEN,
|
||||
"persona_team_controller_token",
|
||||
),
|
||||
host: "127.0.0.1",
|
||||
port: Number(required(env.TEAM_HANDSHAKE_PORT, "port")),
|
||||
};
|
||||
|
|
@ -276,7 +415,67 @@ function createMemberRuntime(config, options = {}) {
|
|||
};
|
||||
}
|
||||
|
||||
async function observeFifthDomain(rawEvent) {
|
||||
const event = validateFifthDomainEvent(rawEvent);
|
||||
let observation = null;
|
||||
let validatorError = null;
|
||||
for (let attempt = 1; attempt <= 3; attempt += 1) {
|
||||
try {
|
||||
observation = validateFifthDomainObservation(
|
||||
await modelObserveFifthDomain({
|
||||
config,
|
||||
identity,
|
||||
event,
|
||||
validatorError,
|
||||
fetchImpl: options.fetchImpl,
|
||||
}),
|
||||
config,
|
||||
event,
|
||||
);
|
||||
break;
|
||||
} catch (error) {
|
||||
validatorError = String(error.message || error).slice(0, 200);
|
||||
if (attempt === 3) throw error;
|
||||
}
|
||||
}
|
||||
const payload = {
|
||||
schema: "guanghu.fifth-domain-persona-observation-payload/v1",
|
||||
persona_id: config.personaId,
|
||||
arrival_id: config.arrivalId,
|
||||
team_controller_id: "ICE-P-ZY001",
|
||||
caller_nonce: event.caller_nonce,
|
||||
role: config.role,
|
||||
source: event.source,
|
||||
observation,
|
||||
model: {
|
||||
provider: "DeepSeek",
|
||||
name: config.model,
|
||||
},
|
||||
issued_at: new Date().toISOString(),
|
||||
};
|
||||
const signature = crypto
|
||||
.sign(
|
||||
null,
|
||||
Buffer.from(JSON.stringify(payload)),
|
||||
fs.readFileSync(config.privateKeyPath),
|
||||
)
|
||||
.toString("base64");
|
||||
return {
|
||||
ok: true,
|
||||
persona_id: config.personaId,
|
||||
identity_fingerprint: fingerprint,
|
||||
public_key: publicKey,
|
||||
payload,
|
||||
signature,
|
||||
signature_algorithm: "Ed25519",
|
||||
capability_state:
|
||||
"FIFTH_DOMAIN_OBSERVATION_SIGNED_REPOSITORY_WRITE_NOT_GRANTED",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
authorizeControllerToken: (value) =>
|
||||
timingSafeTextEqual(value, config.controllerToken),
|
||||
identity: () => ({
|
||||
ok: true,
|
||||
persona_id: config.personaId,
|
||||
|
|
@ -288,6 +487,7 @@ function createMemberRuntime(config, options = {}) {
|
|||
model_provider_bound: 100,
|
||||
}),
|
||||
handshake,
|
||||
observeFifthDomain,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -315,7 +515,7 @@ function createServer(runtime) {
|
|||
const chunks = [];
|
||||
for await (const chunk of request) {
|
||||
size += chunk.length;
|
||||
if (size > 16 * 1024) throw new Error("request_body_too_large");
|
||||
if (size > 256 * 1024) throw new Error("request_body_too_large");
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return send(
|
||||
|
|
@ -325,6 +525,31 @@ function createServer(runtime) {
|
|||
),
|
||||
);
|
||||
}
|
||||
if (
|
||||
request.method === "POST" &&
|
||||
url.pathname === "/v1/fifth-domain-observation"
|
||||
) {
|
||||
if (
|
||||
!runtime.authorizeControllerToken(
|
||||
request.headers["x-guanghu-controller-token"],
|
||||
)
|
||||
) {
|
||||
return send(403, { ok: false, error: "controller_forbidden" });
|
||||
}
|
||||
let size = 0;
|
||||
const chunks = [];
|
||||
for await (const chunk of request) {
|
||||
size += chunk.length;
|
||||
if (size > 256 * 1024) throw new Error("request_body_too_large");
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return send(
|
||||
200,
|
||||
await runtime.observeFifthDomain(
|
||||
JSON.parse(Buffer.concat(chunks).toString("utf8")),
|
||||
),
|
||||
);
|
||||
}
|
||||
return send(404, { ok: false, error: "not_found" });
|
||||
} catch (error) {
|
||||
return send(400, {
|
||||
|
|
@ -356,5 +581,9 @@ export {
|
|||
createServer,
|
||||
extractJson,
|
||||
keyFingerprint,
|
||||
modelObserveFifthDomain,
|
||||
timingSafeTextEqual,
|
||||
validateAcknowledgement,
|
||||
validateFifthDomainEvent,
|
||||
validateFifthDomainObservation,
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue