@pikku/core 0.12.2 → 0.12.3

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 (131) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/crypto-utils.d.ts +10 -0
  3. package/dist/crypto-utils.js +62 -0
  4. package/dist/function/function-runner.js +4 -4
  5. package/dist/function/functions.types.d.ts +26 -1
  6. package/dist/function/functions.types.js +16 -0
  7. package/dist/handle-error.d.ts +1 -1
  8. package/dist/handle-error.js +7 -3
  9. package/dist/index.d.ts +14 -2
  10. package/dist/index.js +1 -1
  11. package/dist/pikku-state.js +3 -1
  12. package/dist/schema.js +1 -1
  13. package/dist/services/in-memory-workflow-service.d.ts +8 -2
  14. package/dist/services/in-memory-workflow-service.js +28 -1
  15. package/dist/services/workflow-service.d.ts +8 -2
  16. package/dist/testing/index.d.ts +2 -0
  17. package/dist/testing/index.js +1 -0
  18. package/dist/testing/service-tests.d.ts +31 -0
  19. package/dist/testing/service-tests.js +598 -0
  20. package/dist/types/core.types.d.ts +9 -1
  21. package/dist/types/state.types.d.ts +4 -2
  22. package/dist/utils/hash.d.ts +2 -0
  23. package/dist/utils/hash.js +23 -0
  24. package/dist/wirings/ai-agent/agent-dynamic-workflow.d.ts +6 -0
  25. package/dist/wirings/ai-agent/agent-dynamic-workflow.js +348 -0
  26. package/dist/wirings/ai-agent/ai-agent-memory.d.ts +1 -1
  27. package/dist/wirings/ai-agent/ai-agent-memory.js +6 -5
  28. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +10 -7
  29. package/dist/wirings/ai-agent/ai-agent-prepare.js +127 -47
  30. package/dist/wirings/ai-agent/ai-agent-runner.d.ts +4 -0
  31. package/dist/wirings/ai-agent/ai-agent-runner.js +381 -27
  32. package/dist/wirings/ai-agent/ai-agent-stream.d.ts +7 -2
  33. package/dist/wirings/ai-agent/ai-agent-stream.js +109 -46
  34. package/dist/wirings/ai-agent/ai-agent.types.d.ts +22 -1
  35. package/dist/wirings/ai-agent/index.d.ts +1 -2
  36. package/dist/wirings/ai-agent/index.js +1 -2
  37. package/dist/wirings/channel/channel-handler.js +5 -1
  38. package/dist/wirings/channel/channel.types.d.ts +1 -7
  39. package/dist/wirings/channel/local/local-channel-handler.d.ts +1 -1
  40. package/dist/wirings/channel/local/local-channel-handler.js +5 -3
  41. package/dist/wirings/channel/local/local-channel-runner.js +14 -4
  42. package/dist/wirings/channel/serverless/serverless-channel-runner.js +4 -1
  43. package/dist/wirings/cli/cli-runner.js +10 -2
  44. package/dist/wirings/cli/cli.types.d.ts +1 -0
  45. package/dist/wirings/http/http-routes.js +0 -1
  46. package/dist/wirings/http/http-runner.d.ts +1 -1
  47. package/dist/wirings/http/http-runner.js +25 -3
  48. package/dist/wirings/http/http.types.d.ts +3 -11
  49. package/dist/wirings/http/index.d.ts +2 -1
  50. package/dist/wirings/http/index.js +1 -0
  51. package/dist/wirings/http/pikku-fetch-http-request.d.ts +1 -0
  52. package/dist/wirings/http/pikku-fetch-http-request.js +3 -0
  53. package/dist/wirings/http/pikku-fetch-http-response.d.ts +1 -0
  54. package/dist/wirings/http/pikku-fetch-http-response.js +4 -4
  55. package/dist/wirings/http/web-request.d.ts +12 -0
  56. package/dist/wirings/http/web-request.js +85 -0
  57. package/dist/wirings/mcp/mcp-endpoint-registry.js +1 -1
  58. package/dist/wirings/mcp/mcp-runner.js +13 -1
  59. package/dist/wirings/rpc/rpc-runner.d.ts +30 -6
  60. package/dist/wirings/rpc/rpc-runner.js +36 -14
  61. package/dist/wirings/rpc/rpc-types.d.ts +6 -1
  62. package/dist/wirings/rpc/wire-addon.d.ts +1 -0
  63. package/dist/wirings/rpc/wire-addon.js +1 -1
  64. package/dist/wirings/scheduler/scheduler-runner.js +1 -0
  65. package/dist/wirings/workflow/graph/graph-validation.d.ts +3 -0
  66. package/dist/wirings/workflow/graph/graph-validation.js +175 -0
  67. package/dist/wirings/workflow/graph/index.d.ts +1 -0
  68. package/dist/wirings/workflow/graph/index.js +1 -0
  69. package/dist/wirings/workflow/index.d.ts +1 -1
  70. package/dist/wirings/workflow/pikku-workflow-service.d.ts +8 -2
  71. package/dist/wirings/workflow/pikku-workflow-service.js +1 -2
  72. package/dist/wirings/workflow/workflow.types.d.ts +10 -1
  73. package/package.json +4 -2
  74. package/src/crypto-utils.test.ts +99 -1
  75. package/src/crypto-utils.ts +114 -0
  76. package/src/function/function-runner.ts +5 -5
  77. package/src/function/functions.types.ts +38 -1
  78. package/src/handle-error.test.ts +65 -2
  79. package/src/handle-error.ts +8 -4
  80. package/src/index.ts +14 -0
  81. package/src/pikku-state.ts +3 -1
  82. package/src/schema.ts +1 -1
  83. package/src/services/in-memory-workflow-service.ts +45 -2
  84. package/src/services/workflow-service.ts +12 -1
  85. package/src/testing/index.ts +2 -0
  86. package/src/testing/service-tests.ts +873 -0
  87. package/src/types/core.types.ts +9 -1
  88. package/src/types/state.types.ts +4 -2
  89. package/src/utils/hash.test.ts +68 -0
  90. package/src/utils/hash.ts +26 -0
  91. package/src/wirings/ai-agent/agent-dynamic-workflow.ts +469 -0
  92. package/src/wirings/ai-agent/ai-agent-memory.ts +7 -6
  93. package/src/wirings/ai-agent/ai-agent-prepare.ts +175 -61
  94. package/src/wirings/ai-agent/ai-agent-runner.test.ts +3 -1
  95. package/src/wirings/ai-agent/ai-agent-runner.ts +553 -31
  96. package/src/wirings/ai-agent/ai-agent-stream.test.ts +209 -2
  97. package/src/wirings/ai-agent/ai-agent-stream.ts +182 -89
  98. package/src/wirings/ai-agent/ai-agent.types.ts +23 -1
  99. package/src/wirings/ai-agent/index.ts +1 -5
  100. package/src/wirings/channel/channel-handler.ts +11 -2
  101. package/src/wirings/channel/channel.types.ts +1 -7
  102. package/src/wirings/channel/local/local-channel-handler.ts +5 -3
  103. package/src/wirings/channel/local/local-channel-runner.ts +17 -7
  104. package/src/wirings/channel/serverless/serverless-channel-runner.ts +4 -1
  105. package/src/wirings/cli/cli-runner.ts +12 -2
  106. package/src/wirings/cli/cli.types.ts +1 -0
  107. package/src/wirings/http/http-routes.ts +0 -1
  108. package/src/wirings/http/http-runner.ts +35 -10
  109. package/src/wirings/http/http.types.ts +3 -12
  110. package/src/wirings/http/index.ts +3 -0
  111. package/src/wirings/http/pikku-fetch-http-request.ts +4 -0
  112. package/src/wirings/http/pikku-fetch-http-response.ts +4 -4
  113. package/src/wirings/http/web-request.test.ts +236 -0
  114. package/src/wirings/http/web-request.ts +104 -0
  115. package/src/wirings/mcp/mcp-endpoint-registry.ts +1 -1
  116. package/src/wirings/mcp/mcp-runner.ts +30 -12
  117. package/src/wirings/rpc/rpc-runner.ts +45 -24
  118. package/src/wirings/rpc/rpc-types.ts +6 -1
  119. package/src/wirings/rpc/wire-addon.ts +2 -1
  120. package/src/wirings/scheduler/scheduler-runner.ts +1 -0
  121. package/src/wirings/workflow/graph/graph-validation.test.ts +170 -0
  122. package/src/wirings/workflow/graph/graph-validation.ts +237 -0
  123. package/src/wirings/workflow/graph/index.ts +5 -0
  124. package/src/wirings/workflow/index.ts +1 -0
  125. package/src/wirings/workflow/pikku-workflow-service.ts +14 -3
  126. package/src/wirings/workflow/workflow.types.ts +9 -1
  127. package/tsconfig.tsbuildinfo +1 -1
  128. package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +0 -5
  129. package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +0 -168
  130. package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +0 -363
  131. package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +0 -238
@@ -1,238 +0,0 @@
1
- import type {
2
- AIStreamChannel,
3
- AIStreamEvent,
4
- AIAgentInput,
5
- } from './ai-agent.types.js'
6
- import { randomUUID } from 'crypto'
7
-
8
- type AssistantUIChunk =
9
- | { type: 'start'; messageId: string }
10
- | { type: 'text-delta'; textDelta: string }
11
- | { type: 'reasoning-delta'; delta: string }
12
- | {
13
- type: 'tool-call-start'
14
- id: string
15
- toolCallId: string
16
- toolName: string
17
- }
18
- | {
19
- type: 'tool-call-delta'
20
- argsText: string
21
- }
22
- | { type: 'tool-call-end' }
23
- | {
24
- type: 'tool-result'
25
- toolCallId: string
26
- result: unknown
27
- }
28
- | {
29
- type: 'finish-step'
30
- finishReason: string
31
- usage?: { promptTokens: number; completionTokens: number }
32
- isContinued: boolean
33
- }
34
- | {
35
- type: 'finish'
36
- finishReason: string
37
- usage?: { promptTokens: number; completionTokens: number }
38
- }
39
- | { type: 'error'; errorText: string }
40
- | { type: string; data: unknown }
41
-
42
- export function createAssistantUIChannel(
43
- parent: AIStreamChannel
44
- ): AIStreamChannel {
45
- const messageId = randomUUID()
46
- let started = false
47
- let totalInputTokens = 0
48
- let totalOutputTokens = 0
49
- let pendingToolResults: AssistantUIChunk[] = []
50
- let hasToolCalls = false
51
- let hasPendingApproval = false
52
-
53
- const sendChunk = (chunk: AssistantUIChunk) => {
54
- parent.send(chunk as any)
55
- }
56
-
57
- const ensureStarted = () => {
58
- if (!started) {
59
- started = true
60
- sendChunk({ type: 'start', messageId })
61
- }
62
- }
63
-
64
- const flushToolResults = () => {
65
- for (const chunk of pendingToolResults) {
66
- sendChunk(chunk)
67
- }
68
- pendingToolResults = []
69
- }
70
-
71
- return {
72
- channelId: parent.channelId,
73
- openingData: parent.openingData,
74
- get state() {
75
- return parent.state
76
- },
77
- close: () => parent.close(),
78
- sendBinary: (data) => parent.sendBinary(data),
79
- send: (event: AIStreamEvent) => {
80
- switch (event.type) {
81
- case 'text-delta':
82
- ensureStarted()
83
- sendChunk({ type: 'text-delta', textDelta: event.text })
84
- break
85
-
86
- case 'reasoning-delta':
87
- ensureStarted()
88
- sendChunk({ type: 'reasoning-delta', delta: event.text })
89
- break
90
-
91
- case 'audio-delta':
92
- ensureStarted()
93
- sendChunk({ type: 'audio-delta', data: event } as any)
94
- break
95
-
96
- case 'audio-done':
97
- ensureStarted()
98
- sendChunk({ type: 'audio-done', data: event } as any)
99
- break
100
-
101
- case 'tool-call':
102
- ensureStarted()
103
- hasToolCalls = true
104
- sendChunk({
105
- type: 'tool-call-start',
106
- id: event.toolCallId,
107
- toolCallId: event.toolCallId,
108
- toolName: event.toolName,
109
- })
110
- sendChunk({
111
- type: 'tool-call-delta',
112
- argsText:
113
- typeof event.args === 'string'
114
- ? event.args
115
- : JSON.stringify(event.args),
116
- })
117
- sendChunk({ type: 'tool-call-end' })
118
- break
119
-
120
- case 'tool-result':
121
- ensureStarted()
122
- pendingToolResults.push({
123
- type: 'tool-result',
124
- toolCallId: event.toolCallId,
125
- result: event.result,
126
- })
127
- break
128
-
129
- case 'agent-call':
130
- case 'agent-result':
131
- break
132
-
133
- case 'usage': {
134
- ensureStarted()
135
- totalInputTokens += event.tokens.input
136
- totalOutputTokens += event.tokens.output
137
- const hasPending = pendingToolResults.length > 0
138
- sendChunk({
139
- type: 'finish-step',
140
- finishReason: hasToolCalls ? 'tool-calls' : 'stop',
141
- usage: {
142
- promptTokens: event.tokens.input,
143
- completionTokens: event.tokens.output,
144
- },
145
- isContinued: hasPending,
146
- })
147
- flushToolResults()
148
- hasToolCalls = false
149
- break
150
- }
151
-
152
- case 'error':
153
- ensureStarted()
154
- sendChunk({ type: 'error', errorText: event.message })
155
- break
156
-
157
- case 'approval-request':
158
- ensureStarted()
159
- hasPendingApproval = true
160
- sendChunk({ type: 'data-approval-request', data: event })
161
- break
162
-
163
- case 'suspended':
164
- ensureStarted()
165
- sendChunk({ type: 'data-suspended', data: event })
166
- break
167
-
168
- case 'done':
169
- ensureStarted()
170
- flushToolResults()
171
- sendChunk({
172
- type: 'finish',
173
- finishReason: hasPendingApproval ? 'tool-calls' : 'stop',
174
- usage: {
175
- promptTokens: totalInputTokens,
176
- completionTokens: totalOutputTokens,
177
- },
178
- })
179
- parent.send('[DONE]' as any)
180
- parent.close()
181
- break
182
- }
183
- },
184
- }
185
- }
186
-
187
- export function parseAssistantUIInput(
188
- input: Record<string, unknown>,
189
- defaults?: { resourceId?: string }
190
- ): AIAgentInput {
191
- const messages = input.messages as Array<Record<string, unknown>> | undefined
192
- if (!messages || !Array.isArray(messages) || messages.length === 0) {
193
- throw new Error(
194
- 'assistant-ui input must contain a non-empty messages array'
195
- )
196
- }
197
-
198
- const lastUserMessage = [...messages].reverse().find((m) => m.role === 'user')
199
- if (!lastUserMessage) {
200
- throw new Error('No user message found in assistant-ui input')
201
- }
202
-
203
- let messageText: string
204
- let attachments: AIAgentInput['attachments']
205
-
206
- const extractFromParts = (parts: Array<Record<string, unknown>>) => {
207
- const textParts = parts.filter((p) => p.type === 'text')
208
- messageText = textParts.map((p) => String(p.text)).join(' ') || ''
209
- const nonTextParts = parts.filter(
210
- (p) => p.type === 'image' || p.type === 'file'
211
- )
212
- if (nonTextParts.length > 0) {
213
- attachments = nonTextParts.map((p) => ({
214
- type: p.type as 'image' | 'file',
215
- data: p.data as string | undefined,
216
- url: p.url as string | undefined,
217
- mediaType: p.mediaType as string | undefined,
218
- filename: p.filename as string | undefined,
219
- }))
220
- }
221
- }
222
-
223
- if (typeof lastUserMessage.content === 'string') {
224
- messageText = lastUserMessage.content
225
- } else if (Array.isArray(lastUserMessage.parts)) {
226
- extractFromParts(lastUserMessage.parts as Array<Record<string, unknown>>)
227
- } else if (Array.isArray(lastUserMessage.content)) {
228
- extractFromParts(lastUserMessage.content as Array<Record<string, unknown>>)
229
- } else {
230
- messageText = ''
231
- }
232
-
233
- const threadId =
234
- (input.threadId as string) ?? (input.id as string) ?? randomUUID()
235
- const resourceId = defaults?.resourceId ?? 'default'
236
-
237
- return { message: messageText!, threadId, resourceId, attachments }
238
- }