@pikku/core 0.12.19 → 0.12.21

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 (146) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/dist/dev/hot-reload.js +20 -6
  3. package/dist/errors/error-handler.d.ts +1 -1
  4. package/dist/errors/error-handler.js +2 -2
  5. package/dist/function/function-runner.js +53 -3
  6. package/dist/function/functions.types.d.ts +3 -2
  7. package/dist/index.d.ts +3 -3
  8. package/dist/index.js +2 -2
  9. package/dist/middleware/index.d.ts +2 -2
  10. package/dist/middleware/index.js +2 -2
  11. package/dist/middleware/telemetry.d.ts +2 -2
  12. package/dist/middleware/telemetry.js +2 -2
  13. package/dist/middleware-runner.d.ts +15 -27
  14. package/dist/middleware-runner.js +25 -30
  15. package/dist/permissions.d.ts +15 -22
  16. package/dist/permissions.js +30 -25
  17. package/dist/pikku-request.js +1 -1
  18. package/dist/pikku-state.js +2 -3
  19. package/dist/services/ai-agent-runner-service.d.ts +1 -0
  20. package/dist/services/content-service.d.ts +66 -37
  21. package/dist/services/in-memory-queue-service.js +1 -1
  22. package/dist/services/in-memory-workflow-service.d.ts +15 -13
  23. package/dist/services/in-memory-workflow-service.js +31 -13
  24. package/dist/services/index.d.ts +1 -1
  25. package/dist/services/local-content.d.ts +10 -12
  26. package/dist/services/local-content.js +54 -38
  27. package/dist/services/user-session-service.d.ts +1 -1
  28. package/dist/testing/service-tests.js +24 -0
  29. package/dist/types/core.types.d.ts +4 -0
  30. package/dist/types/state.types.d.ts +2 -18
  31. package/dist/wirings/ai-agent/ai-agent-memory.d.ts +24 -5
  32. package/dist/wirings/ai-agent/ai-agent-memory.js +128 -23
  33. package/dist/wirings/ai-agent/ai-agent-model-config.d.ts +10 -1
  34. package/dist/wirings/ai-agent/ai-agent-model-config.js +15 -35
  35. package/dist/wirings/ai-agent/ai-agent-prepare.js +4 -1
  36. package/dist/wirings/ai-agent/ai-agent-runner.js +45 -31
  37. package/dist/wirings/ai-agent/ai-agent-stream.js +63 -34
  38. package/dist/wirings/ai-agent/ai-agent.types.d.ts +20 -0
  39. package/dist/wirings/channel/channel-handler.js +1 -1
  40. package/dist/wirings/channel/channel-store.d.ts +13 -0
  41. package/dist/wirings/channel/channel.types.d.ts +3 -0
  42. package/dist/wirings/channel/local/local-channel-runner.js +23 -5
  43. package/dist/wirings/channel/pikku-abstract-channel-handler.js +8 -0
  44. package/dist/wirings/channel/serverless/serverless-channel-runner.js +9 -0
  45. package/dist/wirings/cli/cli-runner.js +24 -5
  46. package/dist/wirings/cli/command-parser.js +19 -4
  47. package/dist/wirings/http/http-runner.js +72 -36
  48. package/dist/wirings/http/http.types.d.ts +1 -0
  49. package/dist/wirings/http/pikku-fetch-http-request.js +3 -1
  50. package/dist/wirings/http/web-request.js +32 -0
  51. package/dist/wirings/rpc/rpc-runner.d.ts +3 -2
  52. package/dist/wirings/rpc/rpc-runner.js +45 -11
  53. package/dist/wirings/workflow/graph/graph-node.d.ts +8 -0
  54. package/dist/wirings/workflow/graph/graph-node.js +4 -0
  55. package/dist/wirings/workflow/graph/graph-runner.js +12 -10
  56. package/dist/wirings/workflow/graph/workflow-graph.types.d.ts +4 -0
  57. package/dist/wirings/workflow/index.d.ts +1 -1
  58. package/dist/wirings/workflow/pikku-workflow-service.d.ts +81 -16
  59. package/dist/wirings/workflow/pikku-workflow-service.js +266 -45
  60. package/dist/wirings/workflow/workflow.types.d.ts +34 -0
  61. package/package.json +1 -1
  62. package/run-tests.sh +4 -1
  63. package/src/dev/hot-reload.test.ts +62 -11
  64. package/src/dev/hot-reload.ts +28 -7
  65. package/src/errors/error-handler.ts +8 -2
  66. package/src/errors/error.test.ts +2 -0
  67. package/src/function/function-runner.test.ts +500 -10
  68. package/src/function/function-runner.ts +68 -3
  69. package/src/function/functions.types.ts +3 -2
  70. package/src/index.ts +22 -3
  71. package/src/middleware/index.ts +11 -2
  72. package/src/middleware/telemetry.ts +2 -2
  73. package/src/middleware-runner.test.ts +16 -16
  74. package/src/middleware-runner.ts +42 -30
  75. package/src/permissions.test.ts +27 -24
  76. package/src/permissions.ts +41 -25
  77. package/src/pikku-request.test.ts +35 -0
  78. package/src/pikku-request.ts +1 -1
  79. package/src/pikku-state.ts +2 -3
  80. package/src/run-tests-script.test.ts +18 -0
  81. package/src/services/ai-agent-runner-service.ts +1 -0
  82. package/src/services/content-service.ts +79 -51
  83. package/src/services/in-memory-queue-service.ts +1 -1
  84. package/src/services/in-memory-session-store.ts +1 -2
  85. package/src/services/in-memory-workflow-service.test.ts +33 -11
  86. package/src/services/in-memory-workflow-service.ts +49 -13
  87. package/src/services/index.ts +10 -1
  88. package/src/services/local-content.test.ts +54 -0
  89. package/src/services/local-content.ts +80 -53
  90. package/src/services/typed-credential-service.ts +3 -3
  91. package/src/services/typed-secret-service.ts +3 -3
  92. package/src/services/typed-variables-service.ts +3 -3
  93. package/src/services/user-session-service.ts +4 -4
  94. package/src/testing/service-tests.ts +30 -0
  95. package/src/types/core.types.ts +6 -2
  96. package/src/types/state.types.ts +2 -13
  97. package/src/wirings/ai-agent/ai-agent-memory.test.ts +324 -0
  98. package/src/wirings/ai-agent/ai-agent-memory.ts +187 -36
  99. package/src/wirings/ai-agent/ai-agent-model-config.test.ts +12 -90
  100. package/src/wirings/ai-agent/ai-agent-model-config.ts +14 -38
  101. package/src/wirings/ai-agent/ai-agent-prepare.test.ts +292 -0
  102. package/src/wirings/ai-agent/ai-agent-prepare.ts +4 -1
  103. package/src/wirings/ai-agent/ai-agent-registry.test.ts +230 -3
  104. package/src/wirings/ai-agent/ai-agent-runner.test.ts +625 -6
  105. package/src/wirings/ai-agent/ai-agent-runner.ts +65 -50
  106. package/src/wirings/ai-agent/ai-agent-stream.test.ts +544 -5
  107. package/src/wirings/ai-agent/ai-agent-stream.ts +71 -69
  108. package/src/wirings/ai-agent/ai-agent.types.ts +24 -0
  109. package/src/wirings/channel/channel-handler.test.ts +272 -0
  110. package/src/wirings/channel/channel-handler.ts +1 -1
  111. package/src/wirings/channel/channel-middleware-runner.test.ts +163 -0
  112. package/src/wirings/channel/channel-store.ts +19 -0
  113. package/src/wirings/channel/channel.types.ts +4 -0
  114. package/src/wirings/channel/local/local-channel-runner.test.ts +63 -0
  115. package/src/wirings/channel/local/local-channel-runner.ts +41 -5
  116. package/src/wirings/channel/local/local-eventhub-service.ts +3 -3
  117. package/src/wirings/channel/pikku-abstract-channel-handler.ts +9 -2
  118. package/src/wirings/channel/serverless/serverless-channel-runner.ts +23 -5
  119. package/src/wirings/cli/channel/cli-raw-channel-runner.ts +7 -2
  120. package/src/wirings/cli/cli-runner.test.ts +255 -2
  121. package/src/wirings/cli/cli-runner.ts +31 -10
  122. package/src/wirings/cli/cli.types.ts +4 -8
  123. package/src/wirings/cli/command-parser.test.ts +83 -0
  124. package/src/wirings/cli/command-parser.ts +23 -4
  125. package/src/wirings/http/http-runner.test.ts +296 -1
  126. package/src/wirings/http/http-runner.ts +87 -57
  127. package/src/wirings/http/http.types.ts +11 -26
  128. package/src/wirings/http/pikku-fetch-http-request.ts +8 -4
  129. package/src/wirings/http/pikku-fetch-http-response.test.ts +41 -0
  130. package/src/wirings/http/web-request.test.ts +115 -0
  131. package/src/wirings/http/web-request.ts +43 -5
  132. package/src/wirings/mcp/mcp-runner.test.ts +367 -0
  133. package/src/wirings/queue/queue.types.ts +2 -5
  134. package/src/wirings/rpc/rpc-runner.test.ts +511 -0
  135. package/src/wirings/rpc/rpc-runner.ts +60 -21
  136. package/src/wirings/rpc/wire-addon.test.ts +57 -0
  137. package/src/wirings/workflow/graph/graph-node.ts +12 -0
  138. package/src/wirings/workflow/graph/graph-runner.test.ts +98 -0
  139. package/src/wirings/workflow/graph/graph-runner.ts +28 -10
  140. package/src/wirings/workflow/graph/workflow-graph.types.ts +4 -0
  141. package/src/wirings/workflow/index.ts +1 -0
  142. package/src/wirings/workflow/pikku-workflow-service.test.ts +19 -2
  143. package/src/wirings/workflow/pikku-workflow-service.ts +370 -71
  144. package/src/wirings/workflow/workflow-queue-workers.test.ts +131 -0
  145. package/src/wirings/workflow/workflow.types.ts +68 -5
  146. package/tsconfig.tsbuildinfo +1 -1
@@ -1,9 +1,124 @@
1
- import { describe, test } from 'node:test'
1
+ import { beforeEach, describe, test } from 'node:test'
2
2
  import assert from 'node:assert/strict'
3
3
 
4
- import { approveAIAgent } from './ai-agent-registry.js'
4
+ import { resetPikkuState, pikkuState } from '../../pikku-state.js'
5
+ import {
6
+ addAIAgent,
7
+ approveAIAgent,
8
+ getAIAgents,
9
+ getAIAgentsMeta,
10
+ } from './ai-agent-registry.js'
11
+
12
+ beforeEach(() => {
13
+ resetPikkuState()
14
+ })
15
+
16
+ describe('addAIAgent', () => {
17
+ test('skips registration when metadata is missing and warns', () => {
18
+ const warnings: string[] = []
19
+ const originalWarn = console.warn
20
+ console.warn = (message?: any) => {
21
+ warnings.push(String(message))
22
+ }
23
+
24
+ try {
25
+ addAIAgent('missing-agent', {
26
+ name: 'missing-agent',
27
+ description: 'desc',
28
+ goal: 'goal',
29
+ instructions: 'help',
30
+ model: 'model',
31
+ } as any)
32
+ } finally {
33
+ console.warn = originalWarn
34
+ }
35
+
36
+ assert.equal(getAIAgents().has('missing-agent'), false)
37
+ assert.match(warnings[0], /Skipping AI agent 'missing-agent'/)
38
+ })
39
+
40
+ test('adds agents when metadata exists and rejects duplicates', () => {
41
+ pikkuState(null, 'agent', 'agentsMeta').assistant = {
42
+ name: 'assistant',
43
+ description: 'desc',
44
+ goal: 'goal',
45
+ } as any
46
+
47
+ addAIAgent('assistant', {
48
+ name: 'assistant',
49
+ description: 'desc',
50
+ goal: 'goal',
51
+ instructions: 'help',
52
+ model: 'model',
53
+ } as any)
54
+
55
+ assert.equal(getAIAgents().get('assistant')?.name, 'assistant')
56
+ assert.throws(
57
+ () =>
58
+ addAIAgent('assistant', {
59
+ name: 'assistant',
60
+ description: 'desc',
61
+ goal: 'goal',
62
+ instructions: 'help',
63
+ model: 'model',
64
+ } as any),
65
+ {
66
+ message: 'AI agent already exists: assistant',
67
+ }
68
+ )
69
+ })
70
+ })
5
71
 
6
72
  describe('approveAIAgent', () => {
73
+ test('throws when run state service is missing, run is missing, or run is not suspended', async () => {
74
+ await assert.rejects(() => approveAIAgent(null as any, 'run-1', []), {
75
+ message: 'AIRunStateService not available',
76
+ })
77
+
78
+ await assert.rejects(
79
+ () =>
80
+ approveAIAgent(
81
+ {
82
+ getRun: async () => null,
83
+ updateRun: async () => {},
84
+ } as any,
85
+ 'run-1',
86
+ []
87
+ ),
88
+ {
89
+ message: 'Run not found: run-1',
90
+ }
91
+ )
92
+
93
+ await assert.rejects(
94
+ () =>
95
+ approveAIAgent(
96
+ {
97
+ getRun: async () => ({
98
+ runId: 'run-1',
99
+ agentName: 'agent',
100
+ threadId: 'thread-1',
101
+ resourceId: 'resource-1',
102
+ status: 'completed',
103
+ usage: {
104
+ inputTokens: 0,
105
+ outputTokens: 0,
106
+ model: 'test/test-model',
107
+ },
108
+ createdAt: new Date(),
109
+ updatedAt: new Date(),
110
+ }),
111
+ updateRun: async () => {},
112
+ } as any,
113
+ 'run-1',
114
+ []
115
+ ),
116
+ {
117
+ message: 'Run is not suspended: completed',
118
+ }
119
+ )
120
+ })
121
+
7
122
  test('rejects approval when run agent does not match expected agent', async () => {
8
123
  const aiRunState = {
9
124
  getRun: async () => ({
@@ -13,7 +128,7 @@ describe('approveAIAgent', () => {
13
128
  resourceId: 'resource-1',
14
129
  status: 'suspended',
15
130
  pendingApprovals: [],
16
- usage: { inputTokens: 0, outputTokens: 0, model: 'test-model' },
131
+ usage: { inputTokens: 0, outputTokens: 0, model: 'test/test-model' },
17
132
  createdAt: new Date(),
18
133
  updatedAt: new Date(),
19
134
  }),
@@ -34,4 +149,116 @@ describe('approveAIAgent', () => {
34
149
  }
35
150
  )
36
151
  })
152
+
153
+ test('marks run resumed when at least one approval is granted', async () => {
154
+ const updates: any[] = []
155
+ const result = await approveAIAgent(
156
+ {
157
+ getRun: async () => ({
158
+ runId: 'run-2',
159
+ agentName: 'assistant',
160
+ threadId: 'thread-1',
161
+ resourceId: 'resource-1',
162
+ status: 'suspended',
163
+ pendingApprovals: [
164
+ {
165
+ type: 'tool-call',
166
+ toolCallId: 'call-1',
167
+ toolName: 'a',
168
+ args: {},
169
+ },
170
+ {
171
+ type: 'tool-call',
172
+ toolCallId: 'call-2',
173
+ toolName: 'b',
174
+ args: {},
175
+ },
176
+ ],
177
+ usage: { inputTokens: 0, outputTokens: 0, model: 'test/test-model' },
178
+ createdAt: new Date(),
179
+ updatedAt: new Date(),
180
+ }),
181
+ updateRun: async (_runId: string, patch: any) => {
182
+ updates.push(patch)
183
+ },
184
+ } as any,
185
+ 'run-2',
186
+ [
187
+ { toolCallId: 'call-1', approved: true },
188
+ { toolCallId: 'call-3', approved: false },
189
+ ]
190
+ )
191
+
192
+ assert.deepEqual(updates, [
193
+ {
194
+ status: 'running',
195
+ pendingApprovals: [
196
+ { type: 'tool-call', toolCallId: 'call-2', toolName: 'b', args: {} },
197
+ ],
198
+ },
199
+ ])
200
+ assert.deepEqual(result, {
201
+ status: 'resumed',
202
+ runId: 'run-2',
203
+ approved: ['call-1'],
204
+ rejected: ['call-3'],
205
+ remainingApprovals: 1,
206
+ })
207
+ })
208
+
209
+ test('keeps run suspended when nothing is approved', async () => {
210
+ const updates: any[] = []
211
+ const result = await approveAIAgent(
212
+ {
213
+ getRun: async () => ({
214
+ runId: 'run-3',
215
+ agentName: 'assistant',
216
+ threadId: 'thread-1',
217
+ resourceId: 'resource-1',
218
+ status: 'suspended',
219
+ pendingApprovals: [
220
+ {
221
+ type: 'tool-call',
222
+ toolCallId: 'call-1',
223
+ toolName: 'a',
224
+ args: {},
225
+ },
226
+ ],
227
+ usage: { inputTokens: 0, outputTokens: 0, model: 'test/test-model' },
228
+ createdAt: new Date(),
229
+ updatedAt: new Date(),
230
+ }),
231
+ updateRun: async (_runId: string, patch: any) => {
232
+ updates.push(patch)
233
+ },
234
+ } as any,
235
+ 'run-3',
236
+ [{ toolCallId: 'call-1', approved: false }]
237
+ )
238
+
239
+ assert.deepEqual(updates, [
240
+ {
241
+ status: 'suspended',
242
+ pendingApprovals: undefined,
243
+ },
244
+ ])
245
+ assert.deepEqual(result, {
246
+ status: 'suspended',
247
+ runId: 'run-3',
248
+ approved: [],
249
+ rejected: ['call-1'],
250
+ remainingApprovals: 0,
251
+ })
252
+ })
253
+ })
254
+
255
+ describe('getAIAgentsMeta', () => {
256
+ test('returns the shared metadata registry', () => {
257
+ const meta = getAIAgentsMeta()
258
+ meta.assistant = { name: 'assistant' } as any
259
+ assert.equal(
260
+ pikkuState(null, 'agent', 'agentsMeta').assistant.name,
261
+ 'assistant'
262
+ )
263
+ })
37
264
  })