@digimuza/system-core 0.2.0-rc.2 → 0.3.0-rc.1

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 (151) hide show
  1. package/INTEGRATION.md +209 -3
  2. package/README.md +23 -0
  3. package/dist/ai/api.d.ts +120 -0
  4. package/dist/ai/api.d.ts.map +1 -0
  5. package/dist/ai/api.js +446 -0
  6. package/dist/ai/api.js.map +1 -0
  7. package/dist/ai/card.d.ts +27 -0
  8. package/dist/ai/card.d.ts.map +1 -0
  9. package/dist/ai/card.js +84 -0
  10. package/dist/ai/card.js.map +1 -0
  11. package/dist/ai/contract.d.ts +364 -0
  12. package/dist/ai/contract.d.ts.map +1 -0
  13. package/dist/ai/contract.js +101 -0
  14. package/dist/ai/contract.js.map +1 -0
  15. package/dist/ai/display.d.ts +20 -0
  16. package/dist/ai/display.d.ts.map +1 -0
  17. package/dist/ai/display.js +35 -0
  18. package/dist/ai/display.js.map +1 -0
  19. package/dist/ai/index.d.ts +7 -0
  20. package/dist/ai/index.d.ts.map +1 -0
  21. package/dist/ai/index.js +10 -0
  22. package/dist/ai/index.js.map +1 -0
  23. package/dist/ai/manifest.d.ts +25 -0
  24. package/dist/ai/manifest.d.ts.map +1 -0
  25. package/dist/ai/manifest.js +15 -0
  26. package/dist/ai/manifest.js.map +1 -0
  27. package/dist/ai/runner.d.ts +71 -0
  28. package/dist/ai/runner.d.ts.map +1 -0
  29. package/dist/ai/runner.js +246 -0
  30. package/dist/ai/runner.js.map +1 -0
  31. package/dist/ai/tools.d.ts +42 -0
  32. package/dist/ai/tools.d.ts.map +1 -0
  33. package/dist/ai/tools.js +117 -0
  34. package/dist/ai/tools.js.map +1 -0
  35. package/dist/audit/csv.d.ts +31 -0
  36. package/dist/audit/csv.d.ts.map +1 -0
  37. package/dist/audit/csv.js +39 -0
  38. package/dist/audit/csv.js.map +1 -0
  39. package/dist/audit/index.d.ts +5 -0
  40. package/dist/audit/index.d.ts.map +1 -0
  41. package/dist/audit/index.js +6 -0
  42. package/dist/audit/index.js.map +1 -0
  43. package/dist/convex/functions.d.ts +39 -0
  44. package/dist/convex/functions.d.ts.map +1 -1
  45. package/dist/convex/functions.js +38 -6
  46. package/dist/convex/functions.js.map +1 -1
  47. package/dist/convex/index.d.ts +1 -1
  48. package/dist/convex/index.d.ts.map +1 -1
  49. package/dist/convex/index.js.map +1 -1
  50. package/dist/core/access.d.ts +14 -0
  51. package/dist/core/access.d.ts.map +1 -1
  52. package/dist/core/access.js +62 -0
  53. package/dist/core/access.js.map +1 -1
  54. package/dist/core/audit-module.d.ts +5 -0
  55. package/dist/core/audit-module.d.ts.map +1 -0
  56. package/dist/core/audit-module.js +25 -0
  57. package/dist/core/audit-module.js.map +1 -0
  58. package/dist/core/audit.d.ts +31 -0
  59. package/dist/core/audit.d.ts.map +1 -1
  60. package/dist/core/audit.js +46 -2
  61. package/dist/core/audit.js.map +1 -1
  62. package/dist/core/catalog.d.ts +41 -0
  63. package/dist/core/catalog.d.ts.map +1 -1
  64. package/dist/core/catalog.js +45 -0
  65. package/dist/core/catalog.js.map +1 -1
  66. package/dist/core/context.d.ts +8 -2
  67. package/dist/core/context.d.ts.map +1 -1
  68. package/dist/core/context.js +4 -1
  69. package/dist/core/context.js.map +1 -1
  70. package/dist/core/index.d.ts +2 -0
  71. package/dist/core/index.d.ts.map +1 -1
  72. package/dist/core/index.js +2 -0
  73. package/dist/core/index.js.map +1 -1
  74. package/dist/core/notify.d.ts +37 -0
  75. package/dist/core/notify.d.ts.map +1 -0
  76. package/dist/core/notify.js +16 -0
  77. package/dist/core/notify.js.map +1 -0
  78. package/dist/core/roles.d.ts.map +1 -1
  79. package/dist/core/roles.js +5 -1
  80. package/dist/core/roles.js.map +1 -1
  81. package/dist/core/system-modules.d.ts +4 -0
  82. package/dist/core/system-modules.d.ts.map +1 -1
  83. package/dist/core/system-modules.js +11 -0
  84. package/dist/core/system-modules.js.map +1 -1
  85. package/dist/finance/ai-tools.d.ts +55 -0
  86. package/dist/finance/ai-tools.d.ts.map +1 -0
  87. package/dist/finance/ai-tools.js +65 -0
  88. package/dist/finance/ai-tools.js.map +1 -0
  89. package/dist/finance/index.d.ts +87 -1
  90. package/dist/finance/index.d.ts.map +1 -1
  91. package/dist/finance/index.js +92 -38
  92. package/dist/finance/index.js.map +1 -1
  93. package/dist/finance/search.d.ts +15 -0
  94. package/dist/finance/search.d.ts.map +1 -0
  95. package/dist/finance/search.js +51 -0
  96. package/dist/finance/search.js.map +1 -0
  97. package/dist/index.d.ts +2 -0
  98. package/dist/index.d.ts.map +1 -1
  99. package/dist/index.js +2 -0
  100. package/dist/index.js.map +1 -1
  101. package/dist/modules/index.d.ts +1 -1
  102. package/dist/modules/index.d.ts.map +1 -1
  103. package/dist/modules/index.js +1 -1
  104. package/dist/modules/index.js.map +1 -1
  105. package/dist/notifications/api.d.ts +130 -0
  106. package/dist/notifications/api.d.ts.map +1 -0
  107. package/dist/notifications/api.js +384 -0
  108. package/dist/notifications/api.js.map +1 -0
  109. package/dist/notifications/contract.d.ts +132 -0
  110. package/dist/notifications/contract.d.ts.map +1 -0
  111. package/dist/notifications/contract.js +59 -0
  112. package/dist/notifications/contract.js.map +1 -0
  113. package/dist/notifications/index.d.ts +6 -0
  114. package/dist/notifications/index.d.ts.map +1 -0
  115. package/dist/notifications/index.js +9 -0
  116. package/dist/notifications/index.js.map +1 -0
  117. package/dist/search/index.d.ts +95 -0
  118. package/dist/search/index.d.ts.map +1 -0
  119. package/dist/search/index.js +151 -0
  120. package/dist/search/index.js.map +1 -0
  121. package/package.json +41 -2
  122. package/src/ai/api.ts +535 -0
  123. package/src/ai/card.ts +91 -0
  124. package/src/ai/contract.ts +245 -0
  125. package/src/ai/display.ts +38 -0
  126. package/src/ai/index.ts +9 -0
  127. package/src/ai/manifest.ts +16 -0
  128. package/src/ai/runner.ts +275 -0
  129. package/src/ai/tools.ts +128 -0
  130. package/src/audit/csv.ts +60 -0
  131. package/src/audit/index.ts +7 -0
  132. package/src/convex/functions.ts +54 -7
  133. package/src/convex/index.ts +1 -1
  134. package/src/core/access.ts +55 -1
  135. package/src/core/audit-module.ts +26 -0
  136. package/src/core/audit.ts +68 -2
  137. package/src/core/catalog.ts +63 -0
  138. package/src/core/context.ts +13 -2
  139. package/src/core/index.ts +2 -0
  140. package/src/core/notify.ts +52 -0
  141. package/src/core/roles.ts +5 -1
  142. package/src/core/system-modules.ts +12 -0
  143. package/src/finance/ai-tools.ts +68 -0
  144. package/src/finance/index.ts +119 -42
  145. package/src/finance/search.ts +58 -0
  146. package/src/index.ts +2 -0
  147. package/src/modules/index.ts +3 -0
  148. package/src/notifications/api.ts +477 -0
  149. package/src/notifications/contract.ts +142 -0
  150. package/src/notifications/index.ts +8 -0
  151. package/src/search/index.ts +198 -0
@@ -0,0 +1,364 @@
1
+ import { type GenericId, type Infer } from "convex/values";
2
+ import type { z } from "zod";
3
+ import type { Actor, ActorRef } from "../core/access.js";
4
+ import type { SystemContext } from "../core/context.js";
5
+ export declare const actorRefValidator: import("convex/values").VUnion<{
6
+ kind: "user";
7
+ userId: string;
8
+ } | {
9
+ apiKeyId: string;
10
+ kind: "agent";
11
+ }, [import("convex/values").VObject<{
12
+ kind: "user";
13
+ userId: string;
14
+ }, {
15
+ kind: import("convex/values").VLiteral<"user", "required">;
16
+ userId: import("convex/values").VString<string, "required">;
17
+ }, "required", "kind" | "userId">, import("convex/values").VObject<{
18
+ apiKeyId: string;
19
+ kind: "agent";
20
+ }, {
21
+ kind: import("convex/values").VLiteral<"agent", "required">;
22
+ apiKeyId: import("convex/values").VString<string, "required">;
23
+ }, "required", "apiKeyId" | "kind">], "required", "apiKeyId" | "kind" | "userId">;
24
+ export declare const toolCallState: import("convex/values").VUnion<"approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running", [import("convex/values").VLiteral<"awaiting-approval", "required">, import("convex/values").VLiteral<"approved", "required">, import("convex/values").VLiteral<"rejected", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"done", "required">, import("convex/values").VLiteral<"error", "required">], "required", never>;
25
+ export declare const messagePart: import("convex/values").VUnion<{
26
+ text: string;
27
+ type: "text";
28
+ } | {
29
+ args: any;
30
+ error?: string | undefined;
31
+ result?: any;
32
+ state: "approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running";
33
+ toolCallId: string;
34
+ toolName: string;
35
+ type: "tool-call";
36
+ }, [import("convex/values").VObject<{
37
+ text: string;
38
+ type: "text";
39
+ }, {
40
+ type: import("convex/values").VLiteral<"text", "required">;
41
+ text: import("convex/values").VString<string, "required">;
42
+ }, "required", "text" | "type">, import("convex/values").VObject<{
43
+ args: any;
44
+ error?: string | undefined;
45
+ result?: any;
46
+ state: "approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running";
47
+ toolCallId: string;
48
+ toolName: string;
49
+ type: "tool-call";
50
+ }, {
51
+ type: import("convex/values").VLiteral<"tool-call", "required">;
52
+ toolCallId: import("convex/values").VString<string, "required">;
53
+ toolName: import("convex/values").VString<string, "required">;
54
+ args: import("convex/values").VAny<any, "required", string>;
55
+ state: import("convex/values").VUnion<"approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running", [import("convex/values").VLiteral<"awaiting-approval", "required">, import("convex/values").VLiteral<"approved", "required">, import("convex/values").VLiteral<"rejected", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"done", "required">, import("convex/values").VLiteral<"error", "required">], "required", never>;
56
+ result: import("convex/values").VAny<any, "optional", string>;
57
+ /** Stable system-core error code (FORBIDDEN, VALIDATION, …) or a short message. */
58
+ error: import("convex/values").VString<string | undefined, "optional">;
59
+ }, "required", "args" | "error" | "result" | "state" | "toolCallId" | "toolName" | "type" | `args.${string}` | `result.${string}`>], "required", "args" | "error" | "result" | "state" | "text" | "toolCallId" | "toolName" | "type" | `args.${string}` | `result.${string}`>;
60
+ export declare const threadStatus: import("convex/values").VUnion<"idle" | "running" | "waiting-approval", [import("convex/values").VLiteral<"idle", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"waiting-approval", "required">], "required", never>;
61
+ export declare const messageStatus: import("convex/values").VUnion<"done" | "error" | "stopped" | "streaming", [import("convex/values").VLiteral<"streaming", "required">, import("convex/values").VLiteral<"done", "required">, import("convex/values").VLiteral<"error", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
62
+ export declare const runStatus: import("convex/values").VUnion<"done" | "error" | "queued" | "running" | "stopped" | "waiting-approval", [import("convex/values").VLiteral<"queued", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"waiting-approval", "required">, import("convex/values").VLiteral<"done", "required">, import("convex/values").VLiteral<"error", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
63
+ /** Spread into the app schema next to `systemTables`. */
64
+ export declare const aiTables: {
65
+ aiThreads: import("convex/server").TableDefinition<import("convex/values").VObject<{
66
+ agentId: string;
67
+ archived: boolean;
68
+ createdAt: number;
69
+ ownerId: string;
70
+ status: "idle" | "running" | "waiting-approval";
71
+ title: string;
72
+ updatedAt: number;
73
+ }, {
74
+ /** system-core user key of the owner. Only the owner can see a thread. */
75
+ ownerId: import("convex/values").VString<string, "required">;
76
+ title: import("convex/values").VString<string, "required">;
77
+ agentId: import("convex/values").VString<string, "required">;
78
+ status: import("convex/values").VUnion<"idle" | "running" | "waiting-approval", [import("convex/values").VLiteral<"idle", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"waiting-approval", "required">], "required", never>;
79
+ archived: import("convex/values").VBoolean<boolean, "required">;
80
+ createdAt: import("convex/values").VFloat64<number, "required">;
81
+ updatedAt: import("convex/values").VFloat64<number, "required">;
82
+ }, "required", "agentId" | "archived" | "createdAt" | "ownerId" | "status" | "title" | "updatedAt">, {
83
+ by_owner: ["ownerId", "archived", "updatedAt", "_creationTime"];
84
+ }, {}, {}>;
85
+ aiMessages: import("convex/server").TableDefinition<import("convex/values").VObject<{
86
+ createdAt: number;
87
+ error?: {
88
+ code: string;
89
+ message: string;
90
+ operationId?: string | undefined;
91
+ } | undefined;
92
+ parts: ({
93
+ text: string;
94
+ type: "text";
95
+ } | {
96
+ args: any;
97
+ error?: string | undefined;
98
+ result?: any;
99
+ state: "approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running";
100
+ toolCallId: string;
101
+ toolName: string;
102
+ type: "tool-call";
103
+ })[];
104
+ role: "assistant" | "user";
105
+ runId?: GenericId<"aiRuns"> | undefined;
106
+ status: "done" | "error" | "stopped" | "streaming";
107
+ threadId: GenericId<"aiThreads">;
108
+ }, {
109
+ threadId: import("convex/values").VId<GenericId<"aiThreads">, "required">;
110
+ role: import("convex/values").VUnion<"assistant" | "user", [import("convex/values").VLiteral<"user", "required">, import("convex/values").VLiteral<"assistant", "required">], "required", never>;
111
+ parts: import("convex/values").VArray<({
112
+ text: string;
113
+ type: "text";
114
+ } | {
115
+ args: any;
116
+ error?: string | undefined;
117
+ result?: any;
118
+ state: "approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running";
119
+ toolCallId: string;
120
+ toolName: string;
121
+ type: "tool-call";
122
+ })[], import("convex/values").VUnion<{
123
+ text: string;
124
+ type: "text";
125
+ } | {
126
+ args: any;
127
+ error?: string | undefined;
128
+ result?: any;
129
+ state: "approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running";
130
+ toolCallId: string;
131
+ toolName: string;
132
+ type: "tool-call";
133
+ }, [import("convex/values").VObject<{
134
+ text: string;
135
+ type: "text";
136
+ }, {
137
+ type: import("convex/values").VLiteral<"text", "required">;
138
+ text: import("convex/values").VString<string, "required">;
139
+ }, "required", "text" | "type">, import("convex/values").VObject<{
140
+ args: any;
141
+ error?: string | undefined;
142
+ result?: any;
143
+ state: "approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running";
144
+ toolCallId: string;
145
+ toolName: string;
146
+ type: "tool-call";
147
+ }, {
148
+ type: import("convex/values").VLiteral<"tool-call", "required">;
149
+ toolCallId: import("convex/values").VString<string, "required">;
150
+ toolName: import("convex/values").VString<string, "required">;
151
+ args: import("convex/values").VAny<any, "required", string>;
152
+ state: import("convex/values").VUnion<"approved" | "awaiting-approval" | "done" | "error" | "rejected" | "running", [import("convex/values").VLiteral<"awaiting-approval", "required">, import("convex/values").VLiteral<"approved", "required">, import("convex/values").VLiteral<"rejected", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"done", "required">, import("convex/values").VLiteral<"error", "required">], "required", never>;
153
+ result: import("convex/values").VAny<any, "optional", string>;
154
+ /** Stable system-core error code (FORBIDDEN, VALIDATION, …) or a short message. */
155
+ error: import("convex/values").VString<string | undefined, "optional">;
156
+ }, "required", "args" | "error" | "result" | "state" | "toolCallId" | "toolName" | "type" | `args.${string}` | `result.${string}`>], "required", "args" | "error" | "result" | "state" | "text" | "toolCallId" | "toolName" | "type" | `args.${string}` | `result.${string}`>, "required">;
157
+ status: import("convex/values").VUnion<"done" | "error" | "stopped" | "streaming", [import("convex/values").VLiteral<"streaming", "required">, import("convex/values").VLiteral<"done", "required">, import("convex/values").VLiteral<"error", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
158
+ /** `operationId` is the run's, so support can find the logs of a failed answer. */
159
+ error: import("convex/values").VObject<{
160
+ code: string;
161
+ message: string;
162
+ operationId?: string | undefined;
163
+ } | undefined, {
164
+ code: import("convex/values").VString<string, "required">;
165
+ message: import("convex/values").VString<string, "required">;
166
+ operationId: import("convex/values").VString<string | undefined, "optional">;
167
+ }, "optional", "code" | "message" | "operationId">;
168
+ runId: import("convex/values").VId<GenericId<"aiRuns"> | undefined, "optional">;
169
+ createdAt: import("convex/values").VFloat64<number, "required">;
170
+ }, "required", "createdAt" | "error" | "error.code" | "error.message" | "error.operationId" | "parts" | "role" | "runId" | "status" | "threadId">, {
171
+ by_thread: ["threadId", "createdAt", "_creationTime"];
172
+ }, {}, {}>;
173
+ aiRuns: import("convex/server").TableDefinition<import("convex/values").VObject<{
174
+ actor: {
175
+ kind: "user";
176
+ userId: string;
177
+ } | {
178
+ apiKeyId: string;
179
+ kind: "agent";
180
+ };
181
+ agentId: string;
182
+ createdAt: number;
183
+ error?: {
184
+ code: string;
185
+ message: string;
186
+ } | undefined;
187
+ messageId: GenericId<"aiMessages">;
188
+ model: string;
189
+ operationId: string;
190
+ status: "done" | "error" | "queued" | "running" | "stopped" | "waiting-approval";
191
+ stopRequested: boolean;
192
+ threadId: GenericId<"aiThreads">;
193
+ updatedAt: number;
194
+ }, {
195
+ threadId: import("convex/values").VId<GenericId<"aiThreads">, "required">;
196
+ /** The assistant message this run writes into. */
197
+ messageId: import("convex/values").VId<GenericId<"aiMessages">, "required">;
198
+ /** Who the run acts for; re-authorized on every tool call (system-core authorizeJob). */
199
+ actor: import("convex/values").VUnion<{
200
+ kind: "user";
201
+ userId: string;
202
+ } | {
203
+ apiKeyId: string;
204
+ kind: "agent";
205
+ }, [import("convex/values").VObject<{
206
+ kind: "user";
207
+ userId: string;
208
+ }, {
209
+ kind: import("convex/values").VLiteral<"user", "required">;
210
+ userId: import("convex/values").VString<string, "required">;
211
+ }, "required", "kind" | "userId">, import("convex/values").VObject<{
212
+ apiKeyId: string;
213
+ kind: "agent";
214
+ }, {
215
+ kind: import("convex/values").VLiteral<"agent", "required">;
216
+ apiKeyId: import("convex/values").VString<string, "required">;
217
+ }, "required", "apiKeyId" | "kind">], "required", "apiKeyId" | "kind" | "userId">;
218
+ agentId: import("convex/values").VString<string, "required">;
219
+ model: import("convex/values").VString<string, "required">;
220
+ status: import("convex/values").VUnion<"done" | "error" | "queued" | "running" | "stopped" | "waiting-approval", [import("convex/values").VLiteral<"queued", "required">, import("convex/values").VLiteral<"running", "required">, import("convex/values").VLiteral<"waiting-approval", "required">, import("convex/values").VLiteral<"done", "required">, import("convex/values").VLiteral<"error", "required">, import("convex/values").VLiteral<"stopped", "required">], "required", never>;
221
+ stopRequested: import("convex/values").VBoolean<boolean, "required">;
222
+ operationId: import("convex/values").VString<string, "required">;
223
+ error: import("convex/values").VObject<{
224
+ code: string;
225
+ message: string;
226
+ } | undefined, {
227
+ code: import("convex/values").VString<string, "required">;
228
+ message: import("convex/values").VString<string, "required">;
229
+ }, "optional", "code" | "message">;
230
+ createdAt: import("convex/values").VFloat64<number, "required">;
231
+ updatedAt: import("convex/values").VFloat64<number, "required">;
232
+ }, "required", "actor" | "actor.apiKeyId" | "actor.kind" | "actor.userId" | "agentId" | "createdAt" | "error" | "error.code" | "error.message" | "messageId" | "model" | "operationId" | "status" | "stopRequested" | "threadId" | "updatedAt">, {
233
+ by_message: ["messageId", "_creationTime"];
234
+ by_thread: ["threadId", "createdAt", "_creationTime"];
235
+ }, {}, {}>;
236
+ };
237
+ export type MessagePart = Infer<typeof messagePart>;
238
+ export type ToolCallState = Infer<typeof toolCallState>;
239
+ export type ThreadStatus = Infer<typeof threadStatus>;
240
+ export type MessageStatus = Infer<typeof messageStatus>;
241
+ export type RunStatus = Infer<typeof runStatus>;
242
+ export type AiThreadId = GenericId<"aiThreads">;
243
+ export type AiMessageId = GenericId<"aiMessages">;
244
+ export type AiRunId = GenericId<"aiRuns">;
245
+ export interface ThreadView {
246
+ id: AiThreadId;
247
+ title: string;
248
+ agentId: string;
249
+ status: ThreadStatus;
250
+ createdAt: number;
251
+ updatedAt: number;
252
+ }
253
+ export interface MessageView {
254
+ id: AiMessageId;
255
+ role: "user" | "assistant";
256
+ parts: MessagePart[];
257
+ status: MessageStatus;
258
+ /** Set when the answer failed; `operationId` is the run's (show it so support can trace it). */
259
+ error: {
260
+ code: string;
261
+ message: string;
262
+ operationId?: string;
263
+ } | null;
264
+ createdAt: number;
265
+ }
266
+ export interface AgentToolView {
267
+ id: string;
268
+ description: string;
269
+ requiresApproval: boolean;
270
+ }
271
+ export interface AgentView {
272
+ id: string;
273
+ name: string;
274
+ description: string;
275
+ /** Only the tools the caller holds the permission for right now. */
276
+ tools: AgentToolView[];
277
+ }
278
+ /** What a tool handler gets. It runs inside an internal mutation, as the run's actor. */
279
+ export interface AiToolContext {
280
+ db: any;
281
+ sys: SystemContext;
282
+ /** Re-authorized for `permission` right before the handler runs. */
283
+ actor: Actor;
284
+ }
285
+ export interface AiTool<I extends z.ZodType = z.ZodType, O = unknown> {
286
+ /** Stable, `module.verb_object` style, e.g. `finance.list_invoices`. Shown to the model. */
287
+ id: string;
288
+ /** For the model: what it does and when to use it. */
289
+ description: string;
290
+ /** Zod schema of the arguments (also sent to the model as JSON schema). */
291
+ input: I;
292
+ /** system-core permission required to run it, e.g. `finance:invoice:read`. */
293
+ permission: string;
294
+ /** Writes or anything risky: the user approves each call first. */
295
+ requiresApproval?: boolean;
296
+ /**
297
+ * Must be JSON-serializable. Throw SystemError for expected failures.
298
+ *
299
+ * Validate everything before the first write: a failed handler does not roll
300
+ * back. `executeAiTool` returns the failure instead of throwing (so the
301
+ * `ai.tool.run` audit survives), therefore writes made before the throw are
302
+ * committed with the surrounding mutation.
303
+ */
304
+ handler(ctx: AiToolContext, input: z.infer<I>): Promise<O>;
305
+ }
306
+ export interface AiAgentDefinition {
307
+ /** Stable id, e.g. `assistant`. */
308
+ id: string;
309
+ name: string;
310
+ description: string;
311
+ /** System prompt. */
312
+ instructions: string;
313
+ /** Model id for this agent; falls back to `defaultModel`. OpenAI only in v1. */
314
+ model?: string;
315
+ tools: readonly AiTool<any, any>[];
316
+ }
317
+ export interface AiOptions {
318
+ agents: readonly AiAgentDefinition[];
319
+ /** Default OpenAI model id when an agent has none (env AI_MODEL wins if set). */
320
+ defaultModel: string;
321
+ /**
322
+ * Convex module paths (relative to convex/) where the app exports the AI
323
+ * functions and the Node runner. Defaults: "ai" and "aiRunner".
324
+ */
325
+ functionsModule?: string;
326
+ runnerModule?: string;
327
+ }
328
+ /** Public functions in `functionsModule` (default convex/ai.ts). */
329
+ export declare const AI_PUBLIC_FUNCTIONS: readonly ["listAgents", "listThreads", "getThread", "listMessages", "createThread", "sendMessage", "stopRun", "respondToToolCall", "renameThread", "archiveThread"];
330
+ /** Internal functions in `functionsModule`, called by the runner. */
331
+ export declare const AI_INTERNAL_FUNCTIONS: readonly ["_startRun", "_appendText", "_addToolCall", "_executeTool", "_setToolCallState", "_finishRun", "_isStopRequested"];
332
+ /** Node runner in `runnerModule` (default convex/aiRunner.ts, "use node"). */
333
+ export declare const AI_RUNNER_FUNCTION = "runAgent";
334
+ /** What `_startRun` gives the runner. */
335
+ export interface RunContext {
336
+ runId: AiRunId;
337
+ threadId: AiThreadId;
338
+ messageId: AiMessageId;
339
+ agentId: string;
340
+ model: string;
341
+ actor: ActorRef;
342
+ operationId: string;
343
+ /** Full history in order, including this run's assistant message so far. */
344
+ history: MessageView[];
345
+ /** Tool ids of the agent the actor may use right now (permission held). */
346
+ allowedToolIds: string[];
347
+ }
348
+ export type ToolExecutionResult = {
349
+ ok: true;
350
+ result: unknown;
351
+ } | {
352
+ ok: false;
353
+ code: string;
354
+ message: string;
355
+ };
356
+ export declare const AI_AUDIT: {
357
+ readonly threadCreate: "ai.thread.create";
358
+ readonly messageSend: "ai.message.send";
359
+ readonly toolRun: "ai.tool.run";
360
+ readonly toolApprove: "ai.tool.approve";
361
+ readonly toolReject: "ai.tool.reject";
362
+ readonly runStop: "ai.run.stop";
363
+ };
364
+ //# sourceMappingURL=contract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/ai/contract.ts"],"names":[],"mappings":"AAKA,OAAO,EAAK,KAAK,SAAS,EAAE,KAAK,KAAK,EAAE,MAAM,eAAe,CAAA;AAC7D,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAC5B,OAAO,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAIvD,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;iFAAmI,CAAA;AAEjK,eAAO,MAAM,aAAa,geAOzB,CAAA;AAED,eAAO,MAAM,WAAW;;;;;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EAAE,CAAA;IACtB,2EAA2E;IAC3E,cAAc,EAAE,MAAM,EAAE,CAAA;CACzB;AAED,MAAM,MAAM,mBAAmB,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAA;AAI9G,eAAO,MAAM,QAAQ;aACnB,YAAY,EAAE,kBAAkB;aAChC,WAAW,EAAE,iBAAiB;aAC9B,OAAO,EAAE,aAAa;aACtB,WAAW,EAAE,iBAAiB;aAC9B,UAAU,EAAE,gBAAgB;aAC5B,OAAO,EAAE,aAAa;CACd,CAAA"}
@@ -0,0 +1,101 @@
1
+ // Shared contract of the AI module (DIG-71). Every part of `@digimuza/system-core/ai`
2
+ // builds on these shapes: tables, views, function names, the tool and agent API.
3
+ // No Mastra or AI SDK imports here: this file is safe in Convex's default runtime
4
+ // and in the browser (types only).
5
+ import { defineTable } from "convex/server";
6
+ import { v } from "convex/values";
7
+ // --- Validators & tables -------------------------------------------------------------
8
+ export const actorRefValidator = v.union(v.object({ kind: v.literal("user"), userId: v.string() }), v.object({ kind: v.literal("agent"), apiKeyId: v.string() }));
9
+ export const toolCallState = v.union(v.literal("awaiting-approval"), // requiresApproval tool, waiting for the user
10
+ v.literal("approved"), // user approved; the runner executes it next
11
+ v.literal("rejected"), // user rejected; the model is told so
12
+ v.literal("running"), v.literal("done"), v.literal("error"));
13
+ export const messagePart = v.union(v.object({ type: v.literal("text"), text: v.string() }), v.object({
14
+ type: v.literal("tool-call"),
15
+ toolCallId: v.string(),
16
+ toolName: v.string(),
17
+ args: v.any(),
18
+ state: toolCallState,
19
+ result: v.optional(v.any()),
20
+ /** Stable system-core error code (FORBIDDEN, VALIDATION, …) or a short message. */
21
+ error: v.optional(v.string()),
22
+ }));
23
+ export const threadStatus = v.union(v.literal("idle"), v.literal("running"), v.literal("waiting-approval"));
24
+ export const messageStatus = v.union(v.literal("streaming"), v.literal("done"), v.literal("error"), v.literal("stopped"));
25
+ export const runStatus = v.union(v.literal("queued"), v.literal("running"), v.literal("waiting-approval"), v.literal("done"), v.literal("error"), v.literal("stopped"));
26
+ /** Spread into the app schema next to `systemTables`. */
27
+ export const aiTables = {
28
+ aiThreads: defineTable({
29
+ /** system-core user key of the owner. Only the owner can see a thread. */
30
+ ownerId: v.string(),
31
+ title: v.string(),
32
+ agentId: v.string(),
33
+ status: threadStatus,
34
+ archived: v.boolean(),
35
+ createdAt: v.number(),
36
+ updatedAt: v.number(),
37
+ }).index("by_owner", ["ownerId", "archived", "updatedAt"]),
38
+ aiMessages: defineTable({
39
+ threadId: v.id("aiThreads"),
40
+ role: v.union(v.literal("user"), v.literal("assistant")),
41
+ parts: v.array(messagePart),
42
+ status: messageStatus,
43
+ /** `operationId` is the run's, so support can find the logs of a failed answer. */
44
+ error: v.optional(v.object({ code: v.string(), message: v.string(), operationId: v.optional(v.string()) })),
45
+ runId: v.optional(v.id("aiRuns")),
46
+ createdAt: v.number(),
47
+ }).index("by_thread", ["threadId", "createdAt"]),
48
+ aiRuns: defineTable({
49
+ threadId: v.id("aiThreads"),
50
+ /** The assistant message this run writes into. */
51
+ messageId: v.id("aiMessages"),
52
+ /** Who the run acts for; re-authorized on every tool call (system-core authorizeJob). */
53
+ actor: actorRefValidator,
54
+ agentId: v.string(),
55
+ model: v.string(),
56
+ status: runStatus,
57
+ stopRequested: v.boolean(),
58
+ operationId: v.string(),
59
+ error: v.optional(v.object({ code: v.string(), message: v.string() })),
60
+ createdAt: v.number(),
61
+ updatedAt: v.number(),
62
+ })
63
+ .index("by_thread", ["threadId", "createdAt"])
64
+ .index("by_message", ["messageId"]),
65
+ };
66
+ // --- Function names (the app re-exports these) ---------------------------------------
67
+ /** Public functions in `functionsModule` (default convex/ai.ts). */
68
+ export const AI_PUBLIC_FUNCTIONS = [
69
+ "listAgents", // () → AgentView[]
70
+ "listThreads", // ({ cursor?, limit? }) → { items: ThreadView[], cursor, done }
71
+ "getThread", // ({ threadId }) → ThreadView (NOT_FOUND unless owner)
72
+ "listMessages", // ({ threadId }) → MessageView[] (live, ascending)
73
+ "createThread", // ({ agentId?, title? }) → { threadId }
74
+ "sendMessage", // ({ threadId, text }) → { messageId, runId }; schedules the runner
75
+ "stopRun", // ({ threadId }) → { stopped }
76
+ "respondToToolCall", // ({ messageId, toolCallId, approve }) → { runId }; resumes the run
77
+ "renameThread", // ({ threadId, title })
78
+ "archiveThread", // ({ threadId })
79
+ ];
80
+ /** Internal functions in `functionsModule`, called by the runner. */
81
+ export const AI_INTERNAL_FUNCTIONS = [
82
+ "_startRun", // ({ runId }) → RunContext | null (null: stopped or gone)
83
+ "_appendText", // ({ messageId, text }) append to the last text part (or start one)
84
+ "_addToolCall", // ({ messageId, toolCallId, toolName, args, state })
85
+ "_executeTool", // ({ runId, toolCallId }) → ToolExecutionResult; authorizes and runs the handler
86
+ "_setToolCallState", // ({ messageId, toolCallId, state, result?, error? })
87
+ "_finishRun", // ({ runId, status, error? }) sets run, message and thread status
88
+ "_isStopRequested", // ({ runId }) → boolean
89
+ ];
90
+ /** Node runner in `runnerModule` (default convex/aiRunner.ts, "use node"). */
91
+ export const AI_RUNNER_FUNCTION = "runAgent"; // internalAction ({ runId }) → void
92
+ // --- Audit actions ------------------------------------------------------------------------
93
+ export const AI_AUDIT = {
94
+ threadCreate: "ai.thread.create",
95
+ messageSend: "ai.message.send",
96
+ toolRun: "ai.tool.run",
97
+ toolApprove: "ai.tool.approve",
98
+ toolReject: "ai.tool.reject",
99
+ runStop: "ai.run.stop",
100
+ };
101
+ //# sourceMappingURL=contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/ai/contract.ts"],"names":[],"mappings":"AAAA,sFAAsF;AACtF,iFAAiF;AACjF,kFAAkF;AAClF,mCAAmC;AACnC,OAAO,EAAE,WAAW,EAAE,MAAM,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,EAAE,kBAAkB;IAChC,WAAW,EAAE,iBAAiB;IAC9B,OAAO,EAAE,aAAa;IACtB,WAAW,EAAE,iBAAiB;IAC9B,UAAU,EAAE,gBAAgB;IAC5B,OAAO,EAAE,aAAa;CACd,CAAA"}
@@ -0,0 +1,20 @@
1
+ import { z } from "zod";
2
+ export * from "./card.js";
3
+ export declare const AI_SHOW_CARD = "ai.show_card";
4
+ /** `ai.show_card`: no data access, no writes, so no approval; needs only `ai:chat:use`. */
5
+ export declare const showCardTool: import("./contract.js").AiTool<z.ZodObject<{
6
+ title: z.ZodOptional<z.ZodString>;
7
+ html: z.ZodString;
8
+ }, z.core.$strip>, {
9
+ shown: boolean;
10
+ bytes: number;
11
+ }>;
12
+ /** Tools for showing things to the user. Add to an agent: `tools: [...financeTools, ...displayTools]`. */
13
+ export declare const displayTools: readonly [import("./contract.js").AiTool<z.ZodObject<{
14
+ title: z.ZodOptional<z.ZodString>;
15
+ html: z.ZodString;
16
+ }, z.core.$strip>, {
17
+ shown: boolean;
18
+ bytes: number;
19
+ }>];
20
+ //# sourceMappingURL=display.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"display.d.ts","sourceRoot":"","sources":["../../src/ai/display.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAKvB,cAAc,WAAW,CAAA;AAEzB,eAAO,MAAM,YAAY,iBAAiB,CAAA;AAE1C,2FAA2F;AAC3F,eAAO,MAAM,YAAY;;;;;;EAgBvB,CAAA;AAEF,0GAA0G;AAC1G,eAAO,MAAM,YAAY;;;;;;GAA0B,CAAA"}
@@ -0,0 +1,35 @@
1
+ // Display tools of the AI module: the assistant shows the user a visual card
2
+ // (summary, comparison, table, simple chart) made of HTML/CSS/inline SVG.
3
+ //
4
+ // The HTML is model output and therefore UNTRUSTED (prompt injection can reach
5
+ // it through tool results). Apps must render it only with `cardDocument()` in an
6
+ // <iframe sandbox="allow-scripts"> (never allow-same-origin): opaque origin, no
7
+ // network, no navigation, no forms, no popups. Nothing leaves the frame except
8
+ // the height message the frame's own resize script posts.
9
+ import { z } from "zod";
10
+ import { AiPermissions } from "./manifest.js";
11
+ import { AI_CARD_HTML_MAX, AI_CARD_TITLE_MAX } from "./card.js";
12
+ import { defineAiTool } from "./tools.js";
13
+ export * from "./card.js";
14
+ export const AI_SHOW_CARD = "ai.show_card";
15
+ /** `ai.show_card`: no data access, no writes, so no approval; needs only `ai:chat:use`. */
16
+ export const showCardTool = defineAiTool({
17
+ id: AI_SHOW_CARD,
18
+ description: [
19
+ "Show the user a visual card rendered from HTML, inside the chat.",
20
+ "Use it when a visual answer is clearly better than text: a summary with key figures, a comparison, a small table, a status overview, a simple bar chart (HTML/CSS or inline SVG).",
21
+ "Rules: a self-contained HTML fragment (no <html>/<head>), inline <style> allowed; no external resources (images, fonts, scripts, links to URLs are blocked); images only as inline SVG or data: URLs.",
22
+ "Style with the CSS variables provided: var(--background), var(--foreground), var(--muted), var(--muted-foreground), var(--card), var(--border), var(--primary), var(--primary-foreground), var(--destructive), var(--radius). They follow the app's light/dark theme; do not hardcode colours.",
23
+ "The chat already draws the card frame (border, background, padding, title): do NOT add an outer border, background, shadow or padding of your own; start with the content. Use var(--destructive) only for errors or overdue items, var(--primary) for the one figure that matters.",
24
+ "Only show data you got from tools or the user. Keep the card compact. Write a short sentence in the chat too.",
25
+ ].join(" "),
26
+ input: z.object({
27
+ title: z.string().trim().min(1).max(AI_CARD_TITLE_MAX).optional(),
28
+ html: z.string().min(1).max(AI_CARD_HTML_MAX),
29
+ }),
30
+ permission: AiPermissions.use,
31
+ handler: async (_ctx, input) => ({ shown: true, bytes: input.html.length }),
32
+ });
33
+ /** Tools for showing things to the user. Add to an agent: `tools: [...financeTools, ...displayTools]`. */
34
+ export const displayTools = [showCardTool];
35
+ //# sourceMappingURL=display.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"display.js","sourceRoot":"","sources":["../../src/ai/display.ts"],"names":[],"mappings":"AA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,7 @@
1
+ export * from "./contract.js";
2
+ export * from "./manifest.js";
3
+ export { defineAiTool, defineAiAgent, executeAiTool } from "./tools.js";
4
+ export { defineAi, type AiSystem } from "./api.js";
5
+ export { AI_SHOW_CARD, showCardTool, displayTools } from "./display.js";
6
+ export * from "./card.js";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ai/index.ts"],"names":[],"mappings":"AAGA,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAC7B,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AACvE,OAAO,EAAE,QAAQ,EAAE,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAA;AAClD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AACvE,cAAc,WAAW,CAAA"}
@@ -0,0 +1,10 @@
1
+ // `@digimuza/system-core/ai`: reusable AI chat with permissioned tools.
2
+ // Convex default runtime + browser types. The Mastra runner is in
3
+ // `@digimuza/system-core/ai/runner` (Node only, "use node").
4
+ export * from "./contract.js";
5
+ export * from "./manifest.js";
6
+ export { defineAiTool, defineAiAgent, executeAiTool } from "./tools.js";
7
+ export { defineAi } from "./api.js";
8
+ export { AI_SHOW_CARD, showCardTool, displayTools } from "./display.js";
9
+ export * from "./card.js";
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ai/index.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,kEAAkE;AAClE,6DAA6D;AAC7D,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAC7B,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AACvE,OAAO,EAAE,QAAQ,EAAiB,MAAM,UAAU,CAAA;AAClD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AACvE,cAAc,WAAW,CAAA"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The AI assistant module. Business module: registers disabled, a Super
3
+ * enables it. `ai:chat:use` lets a person chat; what the assistant may *do* is
4
+ * decided per tool by that tool's own permission (finance:invoice:read, …).
5
+ */
6
+ export declare const ai: {
7
+ readonly id: "ai";
8
+ readonly kind: "business";
9
+ readonly description: "AI asistentas su įrankiais";
10
+ readonly permissions: readonly {
11
+ readonly id: "ai:chat:use";
12
+ readonly name: "Naudoti asistentą";
13
+ readonly description: "Kalbėtis su AI asistentu ir matyti savo pokalbius";
14
+ }[];
15
+ readonly initialSubroles: readonly {
16
+ readonly id: "ai:user";
17
+ readonly name: "Asistento naudotojas";
18
+ readonly description: "Gali kalbėtis su AI asistentu";
19
+ readonly permissions: readonly "ai:chat:use"[];
20
+ }[];
21
+ };
22
+ export declare const AiPermissions: {
23
+ readonly use: "ai:chat:use";
24
+ };
25
+ //# sourceMappingURL=manifest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../../src/ai/manifest.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,EAAE;;;;;;;;;;;;;;;CAMb,CAAA;AAEF,eAAO,MAAM,aAAa;kBAAU,aAAa;CAA6D,CAAA"}
@@ -0,0 +1,15 @@
1
+ import { defineModule } from "../modules/index.js";
2
+ /**
3
+ * The AI assistant module. Business module: registers disabled, a Super
4
+ * enables it. `ai:chat:use` lets a person chat; what the assistant may *do* is
5
+ * decided per tool by that tool's own permission (finance:invoice:read, …).
6
+ */
7
+ export const ai = defineModule({
8
+ id: "ai",
9
+ kind: "business",
10
+ description: "AI asistentas su įrankiais",
11
+ permissions: [{ id: "ai:chat:use", name: "Naudoti asistentą", description: "Kalbėtis su AI asistentu ir matyti savo pokalbius" }],
12
+ initialSubroles: [{ id: "ai:user", name: "Asistento naudotojas", description: "Gali kalbėtis su AI asistentu", permissions: ["ai:chat:use"] }],
13
+ });
14
+ export const AiPermissions = { use: "ai:chat:use" };
15
+ //# sourceMappingURL=manifest.js.map