@pikku/core 0.12.3 → 0.12.4

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 (218) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/services/in-memory-workflow-service.d.ts +15 -2
  3. package/dist/services/in-memory-workflow-service.js +49 -0
  4. package/lcov.info +18891 -0
  5. package/package.json +1 -1
  6. package/src/crypto-utils.test.ts +0 -214
  7. package/src/crypto-utils.ts +0 -213
  8. package/src/errors/error-handler.ts +0 -62
  9. package/src/errors/error.test.ts +0 -195
  10. package/src/errors/errors.ts +0 -374
  11. package/src/errors/index.ts +0 -2
  12. package/src/factory-functions.test.ts +0 -109
  13. package/src/function/function-runner.test.ts +0 -536
  14. package/src/function/function-runner.ts +0 -365
  15. package/src/function/functions.types.ts +0 -304
  16. package/src/function/index.ts +0 -5
  17. package/src/handle-error.test.ts +0 -424
  18. package/src/handle-error.ts +0 -69
  19. package/src/index.ts +0 -118
  20. package/src/internal.ts +0 -6
  21. package/src/middleware/auth-apikey.test.ts +0 -363
  22. package/src/middleware/auth-apikey.ts +0 -47
  23. package/src/middleware/auth-bearer.test.ts +0 -450
  24. package/src/middleware/auth-bearer.ts +0 -72
  25. package/src/middleware/auth-cookie.test.ts +0 -528
  26. package/src/middleware/auth-cookie.ts +0 -80
  27. package/src/middleware/cors.test.ts +0 -424
  28. package/src/middleware/cors.ts +0 -104
  29. package/src/middleware/index.ts +0 -5
  30. package/src/middleware/remote-auth.test.ts +0 -488
  31. package/src/middleware/remote-auth.ts +0 -68
  32. package/src/middleware/timeout.ts +0 -15
  33. package/src/middleware-runner.test.ts +0 -418
  34. package/src/middleware-runner.ts +0 -234
  35. package/src/permissions.test.ts +0 -434
  36. package/src/permissions.ts +0 -319
  37. package/src/pikku-request.ts +0 -23
  38. package/src/pikku-response.ts +0 -5
  39. package/src/pikku-state.test.ts +0 -224
  40. package/src/pikku-state.ts +0 -216
  41. package/src/run-tests-script.test.ts +0 -49
  42. package/src/schema.test.ts +0 -249
  43. package/src/schema.ts +0 -151
  44. package/src/services/ai-agent-runner-service.ts +0 -41
  45. package/src/services/ai-run-state-service.ts +0 -20
  46. package/src/services/ai-storage-service.ts +0 -39
  47. package/src/services/content-service.ts +0 -76
  48. package/src/services/deployment-service.ts +0 -22
  49. package/src/services/gateway-service.ts +0 -20
  50. package/src/services/gopass-secrets.ts +0 -98
  51. package/src/services/in-memory-ai-run-state-service.ts +0 -73
  52. package/src/services/in-memory-trigger-service.ts +0 -64
  53. package/src/services/in-memory-workflow-service.test.ts +0 -351
  54. package/src/services/in-memory-workflow-service.ts +0 -438
  55. package/src/services/index.ts +0 -56
  56. package/src/services/jwt-service.ts +0 -30
  57. package/src/services/local-content.ts +0 -120
  58. package/src/services/local-gateway-service.ts +0 -62
  59. package/src/services/local-secrets.test.ts +0 -80
  60. package/src/services/local-secrets.ts +0 -94
  61. package/src/services/local-variables.test.ts +0 -61
  62. package/src/services/local-variables.ts +0 -39
  63. package/src/services/logger-console.test.ts +0 -118
  64. package/src/services/logger-console.ts +0 -98
  65. package/src/services/logger.ts +0 -57
  66. package/src/services/scheduler-service.ts +0 -86
  67. package/src/services/schema-service.ts +0 -33
  68. package/src/services/scoped-secret-service.test.ts +0 -74
  69. package/src/services/scoped-secret-service.ts +0 -41
  70. package/src/services/secret-service.ts +0 -38
  71. package/src/services/trigger-service.ts +0 -17
  72. package/src/services/typed-secret-service.test.ts +0 -93
  73. package/src/services/typed-secret-service.ts +0 -70
  74. package/src/services/typed-variables-service.test.ts +0 -73
  75. package/src/services/typed-variables-service.ts +0 -81
  76. package/src/services/user-session-service.test.ts +0 -113
  77. package/src/services/user-session-service.ts +0 -86
  78. package/src/services/variables-service.ts +0 -11
  79. package/src/services/workflow-service.ts +0 -95
  80. package/src/testing/index.ts +0 -2
  81. package/src/testing/service-tests.ts +0 -873
  82. package/src/time-utils.test.ts +0 -56
  83. package/src/time-utils.ts +0 -107
  84. package/src/types/core.types.ts +0 -478
  85. package/src/types/state.types.ts +0 -188
  86. package/src/utils/hash.test.ts +0 -68
  87. package/src/utils/hash.ts +0 -26
  88. package/src/utils.test.ts +0 -350
  89. package/src/utils.ts +0 -129
  90. package/src/version.test.ts +0 -80
  91. package/src/version.ts +0 -25
  92. package/src/wirings/ai-agent/agent-dynamic-workflow.ts +0 -469
  93. package/src/wirings/ai-agent/ai-agent-helpers.test.ts +0 -152
  94. package/src/wirings/ai-agent/ai-agent-helpers.ts +0 -76
  95. package/src/wirings/ai-agent/ai-agent-memory.ts +0 -310
  96. package/src/wirings/ai-agent/ai-agent-model-config.test.ts +0 -115
  97. package/src/wirings/ai-agent/ai-agent-model-config.ts +0 -43
  98. package/src/wirings/ai-agent/ai-agent-prepare.ts +0 -636
  99. package/src/wirings/ai-agent/ai-agent-registry.test.ts +0 -37
  100. package/src/wirings/ai-agent/ai-agent-registry.ts +0 -82
  101. package/src/wirings/ai-agent/ai-agent-runner.test.ts +0 -319
  102. package/src/wirings/ai-agent/ai-agent-runner.ts +0 -773
  103. package/src/wirings/ai-agent/ai-agent-stream.test.ts +0 -859
  104. package/src/wirings/ai-agent/ai-agent-stream.ts +0 -1153
  105. package/src/wirings/ai-agent/ai-agent.types.ts +0 -382
  106. package/src/wirings/ai-agent/index.ts +0 -37
  107. package/src/wirings/channel/channel-common.ts +0 -90
  108. package/src/wirings/channel/channel-handler.ts +0 -250
  109. package/src/wirings/channel/channel-middleware-runner.ts +0 -120
  110. package/src/wirings/channel/channel-runner.ts +0 -166
  111. package/src/wirings/channel/channel-store.ts +0 -29
  112. package/src/wirings/channel/channel.types.ts +0 -172
  113. package/src/wirings/channel/define-channel-routes.ts +0 -25
  114. package/src/wirings/channel/eventhub-service.ts +0 -34
  115. package/src/wirings/channel/eventhub-store.ts +0 -13
  116. package/src/wirings/channel/index.ts +0 -24
  117. package/src/wirings/channel/local/index.ts +0 -3
  118. package/src/wirings/channel/local/local-channel-handler.ts +0 -81
  119. package/src/wirings/channel/local/local-channel-runner.test.ts +0 -215
  120. package/src/wirings/channel/local/local-channel-runner.ts +0 -210
  121. package/src/wirings/channel/local/local-eventhub-service.test.ts +0 -95
  122. package/src/wirings/channel/local/local-eventhub-service.ts +0 -101
  123. package/src/wirings/channel/log-channels.ts +0 -20
  124. package/src/wirings/channel/pikku-abstract-channel-handler.test.ts +0 -54
  125. package/src/wirings/channel/pikku-abstract-channel-handler.ts +0 -45
  126. package/src/wirings/channel/serverless/index.ts +0 -5
  127. package/src/wirings/channel/serverless/serverless-channel-runner.ts +0 -289
  128. package/src/wirings/cli/channel/cli-channel-runner.ts +0 -136
  129. package/src/wirings/cli/channel/index.ts +0 -1
  130. package/src/wirings/cli/cli-runner.test.ts +0 -403
  131. package/src/wirings/cli/cli-runner.ts +0 -529
  132. package/src/wirings/cli/cli.types.ts +0 -358
  133. package/src/wirings/cli/command-parser.test.ts +0 -445
  134. package/src/wirings/cli/command-parser.ts +0 -504
  135. package/src/wirings/cli/define-cli-commands.ts +0 -24
  136. package/src/wirings/cli/index.ts +0 -19
  137. package/src/wirings/gateway/gateway-runner.test.ts +0 -474
  138. package/src/wirings/gateway/gateway-runner.ts +0 -411
  139. package/src/wirings/gateway/gateway.types.ts +0 -149
  140. package/src/wirings/gateway/index.ts +0 -13
  141. package/src/wirings/http/http-routes.test.ts +0 -322
  142. package/src/wirings/http/http-routes.ts +0 -197
  143. package/src/wirings/http/http-runner.test.ts +0 -144
  144. package/src/wirings/http/http-runner.ts +0 -588
  145. package/src/wirings/http/http.types.ts +0 -369
  146. package/src/wirings/http/index.ts +0 -28
  147. package/src/wirings/http/log-http-routes.ts +0 -22
  148. package/src/wirings/http/pikku-fetch-http-request.test.ts +0 -237
  149. package/src/wirings/http/pikku-fetch-http-request.ts +0 -170
  150. package/src/wirings/http/pikku-fetch-http-response.test.ts +0 -82
  151. package/src/wirings/http/pikku-fetch-http-response.ts +0 -147
  152. package/src/wirings/http/routers/http-router.ts +0 -13
  153. package/src/wirings/http/routers/path-to-regex.test.ts +0 -319
  154. package/src/wirings/http/routers/path-to-regex.ts +0 -126
  155. package/src/wirings/http/web-request.test.ts +0 -236
  156. package/src/wirings/http/web-request.ts +0 -104
  157. package/src/wirings/mcp/index.ts +0 -27
  158. package/src/wirings/mcp/mcp-endpoint-registry.test.ts +0 -389
  159. package/src/wirings/mcp/mcp-endpoint-registry.ts +0 -159
  160. package/src/wirings/mcp/mcp-runner.ts +0 -323
  161. package/src/wirings/mcp/mcp.types.ts +0 -216
  162. package/src/wirings/node/index.ts +0 -2
  163. package/src/wirings/node/node.types.ts +0 -23
  164. package/src/wirings/oauth2/index.ts +0 -3
  165. package/src/wirings/oauth2/oauth2-client.test.ts +0 -929
  166. package/src/wirings/oauth2/oauth2-client.ts +0 -335
  167. package/src/wirings/oauth2/oauth2.types.ts +0 -69
  168. package/src/wirings/oauth2/wire-oauth2-credential.ts +0 -23
  169. package/src/wirings/queue/index.ts +0 -31
  170. package/src/wirings/queue/queue-runner.test.ts +0 -710
  171. package/src/wirings/queue/queue-runner.ts +0 -192
  172. package/src/wirings/queue/queue.types.ts +0 -189
  173. package/src/wirings/queue/register-queue-helper.ts +0 -60
  174. package/src/wirings/queue/validate-worker-config.test.ts +0 -108
  175. package/src/wirings/queue/validate-worker-config.ts +0 -116
  176. package/src/wirings/rpc/index.ts +0 -4
  177. package/src/wirings/rpc/rpc-runner.ts +0 -460
  178. package/src/wirings/rpc/rpc-types.ts +0 -56
  179. package/src/wirings/rpc/wire-addon.ts +0 -21
  180. package/src/wirings/scheduler/index.ts +0 -11
  181. package/src/wirings/scheduler/log-schedulers.ts +0 -20
  182. package/src/wirings/scheduler/scheduler-runner.test.ts +0 -660
  183. package/src/wirings/scheduler/scheduler-runner.ts +0 -133
  184. package/src/wirings/scheduler/scheduler.types.ts +0 -53
  185. package/src/wirings/secret/index.ts +0 -9
  186. package/src/wirings/secret/secret.types.ts +0 -32
  187. package/src/wirings/secret/validate-secret-definitions.test.ts +0 -140
  188. package/src/wirings/secret/validate-secret-definitions.ts +0 -82
  189. package/src/wirings/trigger/index.ts +0 -10
  190. package/src/wirings/trigger/pikku-trigger-service.ts +0 -112
  191. package/src/wirings/trigger/trigger-runner.test.ts +0 -79
  192. package/src/wirings/trigger/trigger-runner.ts +0 -135
  193. package/src/wirings/trigger/trigger.types.ts +0 -178
  194. package/src/wirings/variable/index.ts +0 -8
  195. package/src/wirings/variable/validate-variable-definitions.test.ts +0 -91
  196. package/src/wirings/variable/validate-variable-definitions.ts +0 -69
  197. package/src/wirings/variable/variable.types.ts +0 -22
  198. package/src/wirings/workflow/dsl/index.ts +0 -31
  199. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +0 -320
  200. package/src/wirings/workflow/dsl/workflow-runner.ts +0 -28
  201. package/src/wirings/workflow/graph/graph-node.ts +0 -222
  202. package/src/wirings/workflow/graph/graph-runner.test.ts +0 -487
  203. package/src/wirings/workflow/graph/graph-runner.ts +0 -816
  204. package/src/wirings/workflow/graph/graph-validation.test.ts +0 -170
  205. package/src/wirings/workflow/graph/graph-validation.ts +0 -237
  206. package/src/wirings/workflow/graph/index.ts +0 -19
  207. package/src/wirings/workflow/graph/template.test.ts +0 -49
  208. package/src/wirings/workflow/graph/template.ts +0 -42
  209. package/src/wirings/workflow/graph/wire-workflow-graph.ts +0 -29
  210. package/src/wirings/workflow/graph/workflow-graph.types.ts +0 -104
  211. package/src/wirings/workflow/index.ts +0 -82
  212. package/src/wirings/workflow/pikku-workflow-service.test.ts +0 -200
  213. package/src/wirings/workflow/pikku-workflow-service.ts +0 -1176
  214. package/src/wirings/workflow/workflow-helpers.test.ts +0 -129
  215. package/src/wirings/workflow/workflow-helpers.ts +0 -79
  216. package/src/wirings/workflow/workflow.types.ts +0 -274
  217. package/tsconfig.json +0 -14
  218. package/tsconfig.tsbuildinfo +0 -1
@@ -1,82 +0,0 @@
1
- import type { CoreAIAgent, AgentRunState } from './ai-agent.types.js'
2
- import { pikkuState } from '../../pikku-state.js'
3
- import type { AIRunStateService } from '../../services/ai-run-state-service.js'
4
- import { PikkuMissingMetaError } from '../../errors/errors.js'
5
-
6
- export const addAIAgent = (
7
- agentName: string,
8
- agent: CoreAIAgent<any, any>,
9
- packageName: string | null = null
10
- ) => {
11
- const agentsMeta = pikkuState(packageName, 'agent', 'agentsMeta')
12
- const agentMeta = agentsMeta[agentName]
13
- if (!agentMeta) {
14
- throw new PikkuMissingMetaError(
15
- `Missing generated metadata for AI agent '${agentName}'`
16
- )
17
- }
18
- const agents = pikkuState(packageName, 'agent', 'agents')
19
- if (agents.has(agentName)) {
20
- throw new Error(`AI agent already exists: ${agentName}`)
21
- }
22
- agents.set(agentName, agent)
23
- }
24
-
25
- export async function approveAIAgent(
26
- aiRunState: AIRunStateService,
27
- runId: string,
28
- approvals: { toolCallId: string; approved: boolean }[],
29
- expectedAgentName?: string
30
- ): Promise<{
31
- status: 'resumed' | 'suspended'
32
- runId: string
33
- approved: string[]
34
- rejected: string[]
35
- remainingApprovals: number
36
- }> {
37
- if (!aiRunState) throw new Error('AIRunStateService not available')
38
-
39
- const run = await aiRunState.getRun(runId)
40
- if (!run) throw new Error('Run not found: ' + runId)
41
- if (expectedAgentName && run.agentName !== expectedAgentName) {
42
- throw new Error(
43
- `Run ${runId} belongs to agent '${run.agentName}', not '${expectedAgentName}'`
44
- )
45
- }
46
- if (run.status !== 'suspended')
47
- throw new Error('Run is not suspended: ' + run.status)
48
-
49
- const approvedIds = new Set(
50
- approvals.filter((a) => a.approved).map((a) => a.toolCallId)
51
- )
52
- const rejectedIds = new Set(
53
- approvals.filter((a) => !a.approved).map((a) => a.toolCallId)
54
- )
55
-
56
- const remaining = (run.pendingApprovals ?? []).filter(
57
- (p: NonNullable<AgentRunState['pendingApprovals']>[number]) =>
58
- !approvedIds.has(p.toolCallId) && !rejectedIds.has(p.toolCallId)
59
- )
60
-
61
- const hasApproved = approvedIds.size > 0
62
- await aiRunState.updateRun(runId, {
63
- status: hasApproved ? 'running' : 'suspended',
64
- pendingApprovals: remaining.length > 0 ? remaining : undefined,
65
- })
66
-
67
- return {
68
- status: hasApproved ? 'resumed' : 'suspended',
69
- runId,
70
- approved: [...approvedIds],
71
- rejected: [...rejectedIds],
72
- remainingApprovals: remaining.length,
73
- }
74
- }
75
-
76
- export const getAIAgents = () => {
77
- return pikkuState(null, 'agent', 'agents')
78
- }
79
-
80
- export const getAIAgentsMeta = () => {
81
- return pikkuState(null, 'agent', 'agentsMeta')
82
- }
@@ -1,319 +0,0 @@
1
- import { beforeEach, describe, test } from 'node:test'
2
- import assert from 'node:assert/strict'
3
-
4
- import { resetPikkuState, pikkuState } from '../../pikku-state.js'
5
- import { runAIAgent } from './ai-agent-runner.js'
6
- import type { CoreAIAgent, PikkuAIMiddlewareHooks } from './ai-agent.types.js'
7
- import type { AIAgentStepResult } from '../../services/ai-agent-runner-service.js'
8
-
9
- beforeEach(() => {
10
- resetPikkuState()
11
- })
12
-
13
- const addTestAgent = (agentName: string) => {
14
- const agent: CoreAIAgent = {
15
- name: agentName,
16
- description: 'test agent',
17
- instructions: 'be helpful',
18
- model: 'test-model',
19
- }
20
-
21
- pikkuState(null, 'agent', 'agentsMeta')[agentName] = {
22
- ...agent,
23
- inputSchema: null,
24
- outputSchema: null,
25
- workingMemorySchema: null,
26
- }
27
- pikkuState(null, 'agent', 'agents').set(agentName, agent)
28
- pikkuState(null, 'models', 'config', {
29
- models: { 'test-model': 'test/test-model' },
30
- })
31
- }
32
-
33
- const makeStepResult = (
34
- overrides?: Partial<AIAgentStepResult>
35
- ): AIAgentStepResult => ({
36
- text: '',
37
- toolCalls: [],
38
- toolResults: [],
39
- usage: { inputTokens: 0, outputTokens: 0 },
40
- finishReason: 'stop',
41
- ...overrides,
42
- })
43
-
44
- describe('runAIAgent', () => {
45
- test('marks run as failed when runner throws', async () => {
46
- addTestAgent('failing-agent')
47
-
48
- const updates: unknown[] = []
49
- const expectedError = new Error('runner failed')
50
-
51
- const mockServices = {
52
- logger: {
53
- info: () => {},
54
- warn: () => {},
55
- error: () => {},
56
- debug: () => {},
57
- },
58
- aiAgentRunner: {
59
- run: async () => {
60
- throw expectedError
61
- },
62
- },
63
- aiRunState: {
64
- createRun: async () => 'run-1',
65
- updateRun: async (_runId: string, patch: unknown) => {
66
- updates.push(patch)
67
- },
68
- },
69
- } as any
70
-
71
- pikkuState(null, 'package', 'singletonServices', mockServices)
72
-
73
- await assert.rejects(
74
- () =>
75
- runAIAgent(
76
- 'failing-agent',
77
- {
78
- message: 'hello',
79
- threadId: 'thread-1',
80
- resourceId: 'resource-1',
81
- },
82
- {}
83
- ),
84
- expectedError
85
- )
86
-
87
- assert.deepEqual(updates, [
88
- { status: 'failed', errorMessage: 'runner failed' },
89
- ])
90
- })
91
-
92
- test('loops through multiple steps and accumulates usage', async () => {
93
- addTestAgent('multi-step-agent')
94
-
95
- let callCount = 0
96
- const mockServices = {
97
- logger: {
98
- info: () => {},
99
- warn: () => {},
100
- error: () => {},
101
- debug: () => {},
102
- },
103
- aiAgentRunner: {
104
- run: async (): Promise<AIAgentStepResult> => {
105
- callCount++
106
- if (callCount === 1) {
107
- return makeStepResult({
108
- text: '',
109
- toolCalls: [
110
- { toolCallId: 'tc-1', toolName: 'myTool', args: { q: 'test' } },
111
- ],
112
- toolResults: [
113
- {
114
- toolCallId: 'tc-1',
115
- toolName: 'myTool',
116
- result: 'tool output',
117
- },
118
- ],
119
- usage: { inputTokens: 100, outputTokens: 50 },
120
- finishReason: 'tool-calls',
121
- })
122
- }
123
- return makeStepResult({
124
- text: 'Final answer',
125
- usage: { inputTokens: 80, outputTokens: 30 },
126
- finishReason: 'stop',
127
- })
128
- },
129
- },
130
- aiRunState: {
131
- createRun: async () => 'run-multi',
132
- updateRun: async () => {},
133
- },
134
- } as any
135
-
136
- pikkuState(null, 'package', 'singletonServices', mockServices)
137
-
138
- const result = await runAIAgent(
139
- 'multi-step-agent',
140
- {
141
- message: 'hello',
142
- threadId: 'thread-multi',
143
- resourceId: 'resource-multi',
144
- },
145
- {}
146
- )
147
-
148
- assert.equal(callCount, 2)
149
- assert.equal(result.text, 'Final answer')
150
- assert.equal(result.usage.inputTokens, 180)
151
- assert.equal(result.usage.outputTokens, 80)
152
- assert.equal(result.steps.length, 2)
153
- })
154
-
155
- test('afterStep hook is called for each step', async () => {
156
- addTestAgent('afterstep-agent')
157
-
158
- const afterStepCalls: unknown[] = []
159
- const middleware: PikkuAIMiddlewareHooks = {
160
- afterStep: async (_services, ctx) => {
161
- afterStepCalls.push(ctx)
162
- },
163
- }
164
- const agent = pikkuState(null, 'agent', 'agents').get('afterstep-agent')!
165
- agent.aiMiddleware = [middleware] as any
166
- pikkuState(null, 'agent', 'agents').set('afterstep-agent', agent)
167
-
168
- let callCount = 0
169
- const mockServices = {
170
- logger: {
171
- info: () => {},
172
- warn: () => {},
173
- error: () => {},
174
- debug: () => {},
175
- },
176
- aiAgentRunner: {
177
- run: async (): Promise<AIAgentStepResult> => {
178
- callCount++
179
- if (callCount === 1) {
180
- return makeStepResult({
181
- text: 'thinking...',
182
- toolCalls: [
183
- { toolCallId: 'tc-1', toolName: 'myTool', args: { q: 'test' } },
184
- ],
185
- toolResults: [
186
- { toolCallId: 'tc-1', toolName: 'myTool', result: 'output' },
187
- ],
188
- usage: { inputTokens: 100, outputTokens: 50 },
189
- finishReason: 'tool-calls',
190
- })
191
- }
192
- return makeStepResult({
193
- text: 'done',
194
- usage: { inputTokens: 80, outputTokens: 30 },
195
- finishReason: 'stop',
196
- })
197
- },
198
- },
199
- aiRunState: {
200
- createRun: async () => 'run-afterstep',
201
- updateRun: async () => {},
202
- },
203
- } as any
204
-
205
- pikkuState(null, 'package', 'singletonServices', mockServices)
206
-
207
- await runAIAgent(
208
- 'afterstep-agent',
209
- { message: 'hi', threadId: 't1', resourceId: 'r1' },
210
- {}
211
- )
212
-
213
- assert.equal(afterStepCalls.length, 2)
214
- const first = afterStepCalls[0] as any
215
- assert.equal(first.stepNumber, 0)
216
- assert.equal(first.finishReason, 'tool-calls')
217
- assert.equal(first.toolCalls.length, 1)
218
- const second = afterStepCalls[1] as any
219
- assert.equal(second.stepNumber, 1)
220
- assert.equal(second.finishReason, 'stop')
221
- assert.equal(second.text, 'done')
222
- })
223
-
224
- test('onError hook is called when runner throws', async () => {
225
- addTestAgent('onerror-agent')
226
-
227
- const onErrorCalls: unknown[] = []
228
- const middleware: PikkuAIMiddlewareHooks = {
229
- onError: async (_services, ctx) => {
230
- onErrorCalls.push(ctx)
231
- },
232
- }
233
- const agent = pikkuState(null, 'agent', 'agents').get('onerror-agent')!
234
- agent.aiMiddleware = [middleware] as any
235
- pikkuState(null, 'agent', 'agents').set('onerror-agent', agent)
236
-
237
- const expectedError = new Error('boom')
238
- const mockServices = {
239
- logger: {
240
- info: () => {},
241
- warn: () => {},
242
- error: () => {},
243
- debug: () => {},
244
- },
245
- aiAgentRunner: {
246
- run: async () => {
247
- throw expectedError
248
- },
249
- },
250
- aiRunState: {
251
- createRun: async () => 'run-err',
252
- updateRun: async () => {},
253
- },
254
- } as any
255
-
256
- pikkuState(null, 'package', 'singletonServices', mockServices)
257
-
258
- await assert.rejects(
259
- () =>
260
- runAIAgent(
261
- 'onerror-agent',
262
- { message: 'hi', threadId: 't2', resourceId: 'r2' },
263
- {}
264
- ),
265
- expectedError
266
- )
267
-
268
- assert.equal(onErrorCalls.length, 1)
269
- const call = onErrorCalls[0] as any
270
- assert.equal(call.error.message, 'boom')
271
- assert.equal(call.stepNumber, -1)
272
- })
273
-
274
- test('onError hook failure does not suppress original error', async () => {
275
- addTestAgent('onerror-throws-agent')
276
-
277
- const middleware: PikkuAIMiddlewareHooks = {
278
- onError: async () => {
279
- throw new Error('hook error')
280
- },
281
- }
282
- const agent = pikkuState(null, 'agent', 'agents').get(
283
- 'onerror-throws-agent'
284
- )!
285
- agent.aiMiddleware = [middleware] as any
286
- pikkuState(null, 'agent', 'agents').set('onerror-throws-agent', agent)
287
-
288
- const expectedError = new Error('original error')
289
- const mockServices = {
290
- logger: {
291
- info: () => {},
292
- warn: () => {},
293
- error: () => {},
294
- debug: () => {},
295
- },
296
- aiAgentRunner: {
297
- run: async () => {
298
- throw expectedError
299
- },
300
- },
301
- aiRunState: {
302
- createRun: async () => 'run-safe',
303
- updateRun: async () => {},
304
- },
305
- } as any
306
-
307
- pikkuState(null, 'package', 'singletonServices', mockServices)
308
-
309
- await assert.rejects(
310
- () =>
311
- runAIAgent(
312
- 'onerror-throws-agent',
313
- { message: 'hi', threadId: 't3', resourceId: 'r3' },
314
- {}
315
- ),
316
- expectedError
317
- )
318
- })
319
- })