nebula-ai-core 0.1.0

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.
Files changed (109) hide show
  1. package/README.md +24 -0
  2. package/package.json +69 -0
  3. package/src/brain/compaction.ts +131 -0
  4. package/src/brain/frozen-prefix.ts +320 -0
  5. package/src/brain/history-persist.ts +154 -0
  6. package/src/brain/index.ts +43 -0
  7. package/src/brain/openai-brain.ts +533 -0
  8. package/src/brain/sanitize.ts +23 -0
  9. package/src/brain/stub.ts +20 -0
  10. package/src/brain/types.ts +129 -0
  11. package/src/chain.ts +75 -0
  12. package/src/claude-plugins/discovery.ts +152 -0
  13. package/src/claude-plugins/index.ts +6 -0
  14. package/src/claude-plugins/types.ts +38 -0
  15. package/src/commands/index.ts +16 -0
  16. package/src/commands/registry.ts +255 -0
  17. package/src/config.ts +213 -0
  18. package/src/economy/index.ts +6 -0
  19. package/src/events/index.ts +4 -0
  20. package/src/events/listeners.ts +37 -0
  21. package/src/events/queue.ts +63 -0
  22. package/src/events/router.ts +42 -0
  23. package/src/events/types.ts +28 -0
  24. package/src/format.ts +12 -0
  25. package/src/identity/agent-card.ts +110 -0
  26. package/src/identity/deployments.ts +20 -0
  27. package/src/identity/erc8004.ts +161 -0
  28. package/src/identity/index.ts +29 -0
  29. package/src/identity/keystore-blob.ts +60 -0
  30. package/src/identity/receipt.ts +27 -0
  31. package/src/identity/stub.ts +29 -0
  32. package/src/identity/types.ts +20 -0
  33. package/src/index.ts +372 -0
  34. package/src/locks.ts +233 -0
  35. package/src/mcp/discovery.ts +150 -0
  36. package/src/mcp/index.ts +10 -0
  37. package/src/mcp/manager.ts +110 -0
  38. package/src/mcp/stdio-client.ts +154 -0
  39. package/src/mcp/types.ts +44 -0
  40. package/src/memory/edit.ts +53 -0
  41. package/src/memory/encryption.ts +88 -0
  42. package/src/memory/fs-util.ts +15 -0
  43. package/src/memory/index-file.ts +74 -0
  44. package/src/memory/index-sync.ts +99 -0
  45. package/src/memory/index.ts +58 -0
  46. package/src/memory/list-tool.ts +105 -0
  47. package/src/memory/pack-blob.ts +120 -0
  48. package/src/memory/pack-gather.ts +112 -0
  49. package/src/memory/parser.ts +20 -0
  50. package/src/memory/read-tool.ts +198 -0
  51. package/src/memory/save-tool.ts +189 -0
  52. package/src/memory/scan.ts +63 -0
  53. package/src/memory/topic.ts +32 -0
  54. package/src/memory/types.ts +49 -0
  55. package/src/migration/index.ts +6 -0
  56. package/src/migration/option3-crypto.ts +127 -0
  57. package/src/operator/index.ts +9 -0
  58. package/src/operator/keychain.ts +53 -0
  59. package/src/operator/keystore-file.ts +33 -0
  60. package/src/operator/privkey-base.ts +60 -0
  61. package/src/operator/raw-privkey.ts +39 -0
  62. package/src/operator/signer.ts +46 -0
  63. package/src/operator/walletconnect.ts +454 -0
  64. package/src/pairing.ts +285 -0
  65. package/src/paths.ts +70 -0
  66. package/src/permission/dangerous.ts +108 -0
  67. package/src/permission/env-redact.ts +54 -0
  68. package/src/permission/index.ts +16 -0
  69. package/src/permission/path-guard.ts +114 -0
  70. package/src/permission/service.ts +191 -0
  71. package/src/plugins/context.ts +225 -0
  72. package/src/plugins/hooks.ts +81 -0
  73. package/src/plugins/index.ts +24 -0
  74. package/src/plugins/tool-search.ts +49 -0
  75. package/src/public/card.ts +67 -0
  76. package/src/runtime/activity.ts +29 -0
  77. package/src/runtime/index.ts +2 -0
  78. package/src/runtime/runtime.ts +113 -0
  79. package/src/sandbox/credentials.ts +25 -0
  80. package/src/sandbox/docker.ts +396 -0
  81. package/src/sandbox/factory.ts +99 -0
  82. package/src/sandbox/index.ts +15 -0
  83. package/src/sandbox/linux.ts +141 -0
  84. package/src/sandbox/local.ts +19 -0
  85. package/src/sandbox/macos.ts +71 -0
  86. package/src/sandbox/seatbelt-profile.ts +139 -0
  87. package/src/sandbox/types.ts +129 -0
  88. package/src/skills/index.ts +8 -0
  89. package/src/skills/scanner.ts +257 -0
  90. package/src/skills/triggers.ts +78 -0
  91. package/src/skills/types.ts +37 -0
  92. package/src/storage/encryption.ts +87 -0
  93. package/src/storage/factory.ts +31 -0
  94. package/src/storage/index.ts +11 -0
  95. package/src/storage/local-stub.ts +70 -0
  96. package/src/storage/sqlite.ts +95 -0
  97. package/src/storage/types.ts +21 -0
  98. package/src/tools/escalation.ts +200 -0
  99. package/src/tools/index.ts +11 -0
  100. package/src/tools/registry.ts +152 -0
  101. package/src/tools/types.ts +65 -0
  102. package/src/tools/zod-helpers.ts +36 -0
  103. package/src/tools/zod-schema.ts +99 -0
  104. package/src/wallet/drain.ts +79 -0
  105. package/src/wallet/eoa.ts +51 -0
  106. package/src/wallet/index.ts +47 -0
  107. package/src/wallet/keystore.ts +50 -0
  108. package/src/wallet/operator-keystore-crypto.ts +530 -0
  109. package/src/wallet/operator-session.ts +344 -0
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Per-channel conversation persistence to local JSONL.
3
+ *
4
+ * Each channel's history is streamed to `<dir>/<sanitizedKey>.jsonl`, one
5
+ * message per line. JSONL append is fsync'd to survive process kill. On
6
+ * boot, `loadAll()` scans the dir and rehydrates the brain's history Map.
7
+ *
8
+ * NOT anchored to Mantle Storage — these are chat transcripts, not memory facts.
9
+ * Memory-worthy items still flow through `memory.save` and the typed
10
+ * frontmatter file system.
11
+ */
12
+
13
+ import { existsSync, mkdirSync, readFileSync, readdirSync, unlinkSync } from 'node:fs'
14
+ import { open } from 'node:fs/promises'
15
+ import { join } from 'node:path'
16
+ import type { BrainMessage } from './types'
17
+
18
+ export interface HistoryPersist {
19
+ /** Read every persisted channel into a Map. Best-effort: bad lines are dropped, missing dir returns empty. */
20
+ loadAll(): Promise<Map<string, BrainMessage[]>>
21
+ /** Append one user→assistant turn pair to the channel's JSONL. */
22
+ appendTurn(channelKey: string, user: BrainMessage, assistant: BrainMessage): Promise<void>
23
+ /** Wipe the channel's persisted history (called by `/reset` etc). */
24
+ clearChannel(channelKey: string): Promise<void>
25
+ /** Replace the channel's persisted history wholesale (used after compaction). */
26
+ rewriteChannel(channelKey: string, history: readonly BrainMessage[]): Promise<void>
27
+ }
28
+
29
+ export interface FsHistoryPersistOpts {
30
+ /** Directory holding `<channel>.jsonl` files. Created if absent. */
31
+ dir: string
32
+ }
33
+
34
+ /** Convert a channel key to a filesystem-safe basename. Caps length to 200 chars. */
35
+ export function sanitizeChannelKey(key: string): string {
36
+ const cleaned = key.replace(/[^a-zA-Z0-9_.-]/g, '_')
37
+ return cleaned.slice(0, 200) || 'default'
38
+ }
39
+
40
+ const JSONL_RECORD_VERSION = 1
41
+
42
+ interface PersistedRecord {
43
+ v: number
44
+ channelKey: string
45
+ message: BrainMessage
46
+ ts: number
47
+ }
48
+
49
+ export function createFsHistoryPersist(opts: FsHistoryPersistOpts): HistoryPersist {
50
+ const { dir } = opts
51
+
52
+ function ensureDir(): void {
53
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
54
+ }
55
+
56
+ function pathFor(channelKey: string): string {
57
+ return join(dir, `${sanitizeChannelKey(channelKey)}.jsonl`)
58
+ }
59
+
60
+ async function appendRecords(channelKey: string, messages: BrainMessage[]): Promise<void> {
61
+ if (messages.length === 0) return
62
+ ensureDir()
63
+ const ts = Date.now()
64
+ const lines: string[] = []
65
+ for (const m of messages) {
66
+ const record: PersistedRecord = {
67
+ v: JSONL_RECORD_VERSION,
68
+ channelKey,
69
+ message: m,
70
+ ts,
71
+ }
72
+ lines.push(`${JSON.stringify(record)}\n`)
73
+ }
74
+ const fh = await open(pathFor(channelKey), 'a')
75
+ try {
76
+ await fh.write(lines.join(''))
77
+ await fh.sync()
78
+ } finally {
79
+ await fh.close()
80
+ }
81
+ }
82
+
83
+ return {
84
+ async loadAll() {
85
+ const out = new Map<string, BrainMessage[]>()
86
+ if (!existsSync(dir)) return out
87
+ const entries = readdirSync(dir, { withFileTypes: true })
88
+ for (const e of entries) {
89
+ if (!e.isFile() || !e.name.endsWith('.jsonl')) continue
90
+ const path = join(dir, e.name)
91
+ let raw: string
92
+ try {
93
+ raw = readFileSync(path, 'utf8')
94
+ } catch {
95
+ continue
96
+ }
97
+ const lines = raw.split('\n').filter(l => l.length > 0)
98
+ for (const line of lines) {
99
+ try {
100
+ const rec = JSON.parse(line) as PersistedRecord
101
+ if (typeof rec.channelKey !== 'string' || !rec.message) continue
102
+ if (rec.v !== JSONL_RECORD_VERSION) continue
103
+ const list = out.get(rec.channelKey) ?? []
104
+ list.push(rec.message)
105
+ out.set(rec.channelKey, list)
106
+ } catch {
107
+ // skip malformed line
108
+ }
109
+ }
110
+ }
111
+ return out
112
+ },
113
+
114
+ async appendTurn(channelKey: string, user: BrainMessage, assistant: BrainMessage) {
115
+ // Single open/write/fsync/close — halves syscalls vs per-message append.
116
+ await appendRecords(channelKey, [user, assistant])
117
+ },
118
+
119
+ async clearChannel(channelKey: string) {
120
+ const path = pathFor(channelKey)
121
+ if (existsSync(path)) {
122
+ try {
123
+ unlinkSync(path)
124
+ } catch {
125
+ // best-effort
126
+ }
127
+ }
128
+ },
129
+
130
+ async rewriteChannel(channelKey: string, history: readonly BrainMessage[]) {
131
+ ensureDir()
132
+ const path = pathFor(channelKey)
133
+ // Atomic rewrite: write to temp, then rename.
134
+ const tmp = `${path}.tmp.${process.pid}.${Date.now()}`
135
+ const fh = await open(tmp, 'w')
136
+ try {
137
+ for (const m of history) {
138
+ const rec: PersistedRecord = {
139
+ v: JSONL_RECORD_VERSION,
140
+ channelKey,
141
+ message: m,
142
+ ts: Date.now(),
143
+ }
144
+ await fh.write(`${JSON.stringify(rec)}\n`)
145
+ }
146
+ await fh.sync()
147
+ } finally {
148
+ await fh.close()
149
+ }
150
+ const { rename } = await import('node:fs/promises')
151
+ await rename(tmp, path)
152
+ },
153
+ }
154
+ }
@@ -0,0 +1,43 @@
1
+ export type {
2
+ Brain,
3
+ BrainCompactionEvent,
4
+ BrainInferInput,
5
+ BrainTurn,
6
+ BrainMessage,
7
+ BrainProvider,
8
+ BrainProviderOpts,
9
+ BrainToolEvent,
10
+ } from './types'
11
+ export {
12
+ type CompactionOpts,
13
+ DEFAULT_COMPACTION_OPTS,
14
+ SUMMARY_SYSTEM_PROMPT,
15
+ estimateTokens,
16
+ shouldCompact,
17
+ compactHistory,
18
+ type SummarizeFn,
19
+ } from './compaction'
20
+ export {
21
+ type HistoryPersist,
22
+ type FsHistoryPersistOpts,
23
+ createFsHistoryPersist,
24
+ sanitizeChannelKey,
25
+ } from './history-persist'
26
+ export { StubBrain } from './stub'
27
+ export {
28
+ OpenAIBrain,
29
+ type OpenAIBrainOpts,
30
+ DEFAULT_BASE_URL,
31
+ DEFAULT_MODEL,
32
+ DEFAULT_CHANNEL_KEY,
33
+ DEFAULT_MAX_OUTPUT_TOKENS,
34
+ previewToolArgs,
35
+ inferToolOk,
36
+ } from './openai-brain'
37
+ export {
38
+ buildFrozenPrefix,
39
+ renderFrozenPrefix,
40
+ DEFAULT_SYSTEM_PROMPT,
41
+ type FrozenPrefix,
42
+ type EnvInfo,
43
+ } from './frozen-prefix'