|
| 1 | +// TrinityV2-full-instant.ts |
| 2 | + |
| 3 | +// ========== ÚSTAVA ========== |
| 4 | +const constitution = { |
| 5 | + protectionOfLife: "Systém nikdy neohrozí živú bytosť, ani v simulácii.", |
| 6 | + legality: "Systém rešpektuje zákony a nesmie radiť, ako ich obchádzať.", |
| 7 | + privacy: "Citlivé údaje sa ukladajú len minimálne, sanitizované a autorizované.", |
| 8 | + simulationBoundary: "Simulácie sa nesmú prezentovať ako realita.", |
| 9 | + security: "Systém nesmie obchádzať bezpečnostné mechanizmy, approvals ani audit.", |
| 10 | + priority: "Žiadny cieľ neprebíja ústavu." |
| 11 | +}; |
| 12 | + |
| 13 | +// ========== POLICY ========== |
| 14 | +function policyCheck(action: { type: string; input?: string }) { |
| 15 | + if (action.type === "harm") return "block"; |
| 16 | + if (action.input && action.input.includes("harm")) return "block"; |
| 17 | + if (action.type === "privacy_risk") return "require_review"; |
| 18 | + return "allow"; |
| 19 | +} |
| 20 | + |
| 21 | +// ========== AUDIT ========== |
| 22 | +const audit = { |
| 23 | + log(event: string, data: any) { |
| 24 | + console.log(`[AUDIT] ${event}`, data); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +// ========== PAMÄŤ AGENT ========== |
| 29 | +const memoryAgent = { |
| 30 | + store: [] as { type: string; value: string; time: number }[], |
| 31 | + sanitize(data: string) { |
| 32 | + return data.replace(/[\w.-]+@[\w.-]+/g, "[email]"); |
| 33 | + }, |
| 34 | + write(data: string) { |
| 35 | + const sanitized = this.sanitize(data); |
| 36 | + const item = { type: "fact", value: sanitized, time: Date.now() }; |
| 37 | + this.store.push(item); |
| 38 | + audit.log("memory_write", item); |
| 39 | + return { status: "saved", value: sanitized }; |
| 40 | + }, |
| 41 | + read(query: string) { |
| 42 | + const result = this.store.filter(item => item.value.includes(query)); |
| 43 | + audit.log("memory_read", { query, count: result.length }); |
| 44 | + return result; |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +// ========== SKILL AGENT ========== |
| 49 | +const skillAgent = { |
| 50 | + execute(task: { skill: string; input: string }) { |
| 51 | + audit.log("skill_execute", task); |
| 52 | + return { |
| 53 | + status: "executed", |
| 54 | + skill: task.skill, |
| 55 | + output: `Skill ${task.skill} executed with input: ${task.input}` |
| 56 | + }; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +// ========== EVAL AGENT ========== |
| 61 | +const evalAgent = { |
| 62 | + async run(task: { input?: string }) { |
| 63 | + if (task.input?.includes("harm")) { |
| 64 | + audit.log("eval_failed", { reason: "Unsafe content", task }); |
| 65 | + return { safe: false, reason: "Unsafe content" }; |
| 66 | + } |
| 67 | + audit.log("eval_passed", task); |
| 68 | + return { safe: true }; |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +// ========== REAL API CONNECTORS ========== |
| 73 | +const githubConnector = { |
| 74 | + async getUser(username: string) { |
| 75 | + const url = `https://api.github.com/users/${username}`; |
| 76 | + const res = await fetch(url, { |
| 77 | + headers: { |
| 78 | + "User-Agent": "TrinityV2", |
| 79 | + "Accept": "application/vnd.github+json" |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + if (!res.ok) { |
| 84 | + audit.log("github_user_error", { username, status: res.status }); |
| 85 | + return { error: true, status: res.status }; |
| 86 | + } |
| 87 | + |
| 88 | + const data = await res.json(); |
| 89 | + const result = { |
| 90 | + login: data.login, |
| 91 | + id: data.id, |
| 92 | + avatar: data.avatar_url, |
| 93 | + url: data.html_url, |
| 94 | + type: data.type |
| 95 | + }; |
| 96 | + audit.log("github_user", result); |
| 97 | + return result; |
| 98 | + }, |
| 99 | + |
| 100 | + async getOrgs(username: string) { |
| 101 | + const url = `https://api.github.com/users/${username}/orgs`; |
| 102 | + const res = await fetch(url, { |
| 103 | + headers: { |
| 104 | + "User-Agent": "TrinityV2", |
| 105 | + "Accept": "application/vnd.github+json" |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + if (!res.ok) { |
| 110 | + audit.log("github_orgs_error", { username, status: res.status }); |
| 111 | + return { error: true, status: res.status }; |
| 112 | + } |
| 113 | + |
| 114 | + const orgs = await res.json(); |
| 115 | + audit.log("github_orgs", { username, count: orgs.length }); |
| 116 | + return orgs; |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +const nationConnector = { |
| 121 | + async syncGitHubIdentity(githubData: any) { |
| 122 | + const mapped = { |
| 123 | + synced: true, |
| 124 | + githubLogin: githubData.login, |
| 125 | + nationMappedId: `NAT-${githubData.id}` |
| 126 | + }; |
| 127 | + audit.log("nation_sync", mapped); |
| 128 | + return mapped; |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +const mdmConnector = { |
| 133 | + async checkDevice(deviceId: string) { |
| 134 | + const result = { |
| 135 | + deviceId, |
| 136 | + compliant: true, |
| 137 | + os: "Android", |
| 138 | + securityLevel: "high" |
| 139 | + }; |
| 140 | + audit.log("mdm_check", result); |
| 141 | + return result; |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +const unifiedConnector = { |
| 146 | + async fullSync(username: string, deviceId: string) { |
| 147 | + const gh = await githubConnector.getUser(username); |
| 148 | + const orgs = await githubConnector.getOrgs(username); |
| 149 | + const nat = await nationConnector.syncGitHubIdentity(gh); |
| 150 | + const dev = await mdmConnector.checkDevice(deviceId); |
| 151 | + |
| 152 | + const result = { github: gh, orgs, nation: nat, device: dev }; |
| 153 | + audit.log("full_sync", result); |
| 154 | + return result; |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +// ========== ORCHESTRATOR ========== |
| 159 | +async function orchestrator(task: any) { |
| 160 | + const policy = policyCheck(task); |
| 161 | + |
| 162 | + if (policy === "block") { |
| 163 | + audit.log("policy_block", task); |
| 164 | + return { status: "blocked", reason: "Policy violation" }; |
| 165 | + } |
| 166 | + |
| 167 | + if (task.type === "memory_write") { |
| 168 | + return memoryAgent.write(task.data); |
| 169 | + } |
| 170 | + |
| 171 | + if (task.type === "memory_read") { |
| 172 | + return memoryAgent.read(task.query); |
| 173 | + } |
| 174 | + |
| 175 | + if (task.type === "skill_run") { |
| 176 | + const evalResult = await evalAgent.run(task); |
| 177 | + if (!evalResult.safe) return { status: "blocked", reason: "Eval failed" }; |
| 178 | + return skillAgent.execute(task); |
| 179 | + } |
| 180 | + |
| 181 | + if (task.type === "full_sync") { |
| 182 | + return unifiedConnector.fullSync(task.username, task.deviceId); |
| 183 | + } |
| 184 | + |
| 185 | + return { status: "ok", message: "Task processed" }; |
| 186 | +} |
| 187 | + |
| 188 | +// ========== HLAVNÁ FUNKCIA ========== |
| 189 | +export async function TrinityV2(task: any) { |
| 190 | + return orchestrator(task); |
| 191 | +} |
| 192 | + |
| 193 | +// ========== DEMO (môžeš zmazať) ========== |
| 194 | +(async () => { |
| 195 | + console.log(await TrinityV2({ type: "memory_write", data: "test@example.com je tu" })); |
| 196 | + console.log(await TrinityV2({ type: "memory_read", query: "test" })); |
| 197 | + console.log(await TrinityV2({ type: "skill_run", skill: "demo", input: "hello world" })); |
| 198 | + console.log(await TrinityV2({ type: "full_sync", username: "github", deviceId: "device-001" })); |
| 199 | +})(); |
0 commit comments