Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * A short digest of a world's state, so two worlds can be compared without |
| 3 | * diffing them: two rigs that start from the same seed hash the same, and a |
| 4 | * request that changed anything changes the hash. |
| 5 | */ |
| 6 | |
| 7 | import { createHash } from 'node:crypto'; |
| 8 | import type { World } from './world'; |
| 9 | |
| 10 | /** JSON with every object's keys sorted, so equal content serialises one way. */ |
| 11 | const canonical = (value: unknown): string => { |
| 12 | if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`; |
| 13 | if (value !== null && typeof value === 'object') { |
| 14 | const entries = Object.entries(value) |
| 15 | .filter(([, field]) => field !== undefined) |
| 16 | .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)); |
| 17 | return `{${entries.map(([key, field]) => `${JSON.stringify(key)}:${canonical(field)}`).join(',')}}`; |
| 18 | } |
| 19 | return JSON.stringify(value); |
| 20 | }; |
| 21 | |
| 22 | export const worldHash = (world: World): string => |
| 23 | createHash('sha256').update(canonical(world.snapshot())).digest('hex').slice(0, 16); |