@pikku/inspector 0.12.75 → 0.12.81

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 (219) hide show
  1. package/CHANGELOG.md +330 -0
  2. package/dist/add/add-analytics.d.ts +9 -0
  3. package/dist/add/add-analytics.js +178 -0
  4. package/dist/add/add-auth.js +0 -54
  5. package/dist/add/add-feature-flag.d.ts +2 -0
  6. package/dist/add/add-feature-flag.js +162 -0
  7. package/dist/add/add-wire-addon.js +1 -0
  8. package/dist/add/add-wire-remote-addon.js +1 -0
  9. package/dist/index.d.ts +3 -1
  10. package/dist/index.js +3 -1
  11. package/dist/inspector.js +4 -0
  12. package/dist/surface/collect-surface.d.ts +58 -0
  13. package/dist/surface/collect-surface.js +613 -0
  14. package/dist/types.d.ts +20 -11
  15. package/dist/utils/filter-inspector-state.js +14 -0
  16. package/dist/utils/post-process.js +15 -0
  17. package/dist/utils/read-module-specifiers.d.ts +8 -0
  18. package/dist/utils/read-module-specifiers.js +23 -0
  19. package/dist/utils/read-tsconfig-out-dir.d.ts +5 -0
  20. package/dist/utils/read-tsconfig-out-dir.js +12 -0
  21. package/dist/utils/resolve-deploy-target.d.ts +7 -0
  22. package/dist/utils/resolve-deploy-target.js +15 -7
  23. package/dist/utils/serialize-inspector-state.d.ts +5 -0
  24. package/dist/utils/serialize-inspector-state.js +8 -0
  25. package/dist/visit.js +4 -0
  26. package/package.json +16 -6
  27. package/run-tests.sh +21 -6
  28. package/src/add/add-addon-bans.ts +0 -85
  29. package/src/add/add-agent-scorer.test.ts +0 -197
  30. package/src/add/add-agent-scorer.ts +0 -83
  31. package/src/add/add-agent.test.ts +0 -87
  32. package/src/add/add-agent.ts +0 -638
  33. package/src/add/add-approval-description.ts +0 -76
  34. package/src/add/add-auth.test.ts +0 -791
  35. package/src/add/add-auth.ts +0 -424
  36. package/src/add/add-channel.ts +0 -778
  37. package/src/add/add-cli-renderers.test.ts +0 -74
  38. package/src/add/add-cli.ts +0 -1006
  39. package/src/add/add-credential.ts +0 -186
  40. package/src/add/add-feature.test.ts +0 -306
  41. package/src/add/add-feature.ts +0 -253
  42. package/src/add/add-file-extends-core-type.ts +0 -66
  43. package/src/add/add-file-with-config.ts +0 -45
  44. package/src/add/add-file-with-factory.test.ts +0 -134
  45. package/src/add/add-file-with-factory.ts +0 -288
  46. package/src/add/add-functions.test.ts +0 -743
  47. package/src/add/add-functions.ts +0 -1548
  48. package/src/add/add-gateway.ts +0 -143
  49. package/src/add/add-http-route-ref.test.ts +0 -149
  50. package/src/add/add-http-route.ts +0 -530
  51. package/src/add/add-http-routes.ts +0 -471
  52. package/src/add/add-keyed-wiring.ts +0 -180
  53. package/src/add/add-mcp-prompt.ts +0 -157
  54. package/src/add/add-mcp-resource.ts +0 -175
  55. package/src/add/add-middleware.test.ts +0 -196
  56. package/src/add/add-middleware.ts +0 -879
  57. package/src/add/add-permission-auth.test.ts +0 -59
  58. package/src/add/add-permission.ts +0 -311
  59. package/src/add/add-personas.test.ts +0 -406
  60. package/src/add/add-personas.ts +0 -287
  61. package/src/add/add-queue-worker.ts +0 -117
  62. package/src/add/add-rpc-invocations.test.ts +0 -287
  63. package/src/add/add-rpc-invocations.ts +0 -221
  64. package/src/add/add-scenario-step.test.ts +0 -509
  65. package/src/add/add-schedule.ts +0 -120
  66. package/src/add/add-scope.test.ts +0 -419
  67. package/src/add/add-scope.ts +0 -221
  68. package/src/add/add-secret.ts +0 -10
  69. package/src/add/add-subject-scenario-steps.test.ts +0 -161
  70. package/src/add/add-system-role.test.ts +0 -297
  71. package/src/add/add-system-role.ts +0 -162
  72. package/src/add/add-trigger.ts +0 -165
  73. package/src/add/add-variable.ts +0 -10
  74. package/src/add/add-wire-addon.test.ts +0 -185
  75. package/src/add/add-wire-addon.ts +0 -146
  76. package/src/add/add-wire-remote-addon.ts +0 -80
  77. package/src/add/add-workflow-edge-cases.test.ts +0 -201
  78. package/src/add/add-workflow-fanout.test.ts +0 -166
  79. package/src/add/add-workflow-graph-foreach.test.ts +0 -174
  80. package/src/add/add-workflow-graph-input.test.ts +0 -94
  81. package/src/add/add-workflow-graph-notes.test.ts +0 -74
  82. package/src/add/add-workflow-graph.ts +0 -644
  83. package/src/add/add-workflow-on-error.test.ts +0 -118
  84. package/src/add/add-workflow-return-switch.test.ts +0 -121
  85. package/src/add/add-workflow-silent-drops.test.ts +0 -261
  86. package/src/add/add-workflow-sleep-expression.test.ts +0 -96
  87. package/src/add/add-workflow.test.ts +0 -155
  88. package/src/add/add-workflow.ts +0 -482
  89. package/src/add/addon-bans.test.ts +0 -121
  90. package/src/add/addon-contracts.test.ts +0 -221
  91. package/src/add/allow-escape-hatches.test.ts +0 -144
  92. package/src/add/audit-meta.test.ts +0 -75
  93. package/src/add/classified-sink-check.test.ts +0 -347
  94. package/src/add/config-value-metadata.test.ts +0 -263
  95. package/src/add/dynamic-import-check.test.ts +0 -119
  96. package/src/add/expect-eventually-scenario-only.test.ts +0 -108
  97. package/src/add/function-body-span.test.ts +0 -96
  98. package/src/add/inline-wiring-function.test.ts +0 -104
  99. package/src/add/pii-check.test.ts +0 -258
  100. package/src/add/rpc-type-cast.test.ts +0 -123
  101. package/src/add/scenario-instrumentation.test.ts +0 -89
  102. package/src/add/scenario-instrumentation.ts +0 -42
  103. package/src/add/scenario-marker.test.ts +0 -89
  104. package/src/add/scenario-not-extractable.test.ts +0 -159
  105. package/src/add/scenario-services.test.ts +0 -103
  106. package/src/add/scenario-skip.test.ts +0 -83
  107. package/src/add/validate-workflow-graph-addons.test.ts +0 -102
  108. package/src/add/wire-name-literal.test.ts +0 -117
  109. package/src/error-codes-emitted.test.ts +0 -76
  110. package/src/error-codes.ts +0 -145
  111. package/src/index.ts +0 -66
  112. package/src/inspector.ts +0 -575
  113. package/src/schema-generator.ts +0 -1
  114. package/src/source-file-cache.ts +0 -73
  115. package/src/tsconfig-paths.test.ts +0 -90
  116. package/src/types-map.ts +0 -141
  117. package/src/types.ts +0 -775
  118. package/src/utils/annotate-http-route-auth.test.ts +0 -161
  119. package/src/utils/annotate-http-route-auth.ts +0 -53
  120. package/src/utils/check-classified-sinks.ts +0 -215
  121. package/src/utils/check-pii-output.ts +0 -120
  122. package/src/utils/compute-diagnostics.test.ts +0 -106
  123. package/src/utils/compute-required-schemas.ts +0 -48
  124. package/src/utils/contract-hashes.test.ts +0 -553
  125. package/src/utils/contract-hashes.ts +0 -387
  126. package/src/utils/custom-types-generator.test.ts +0 -124
  127. package/src/utils/custom-types-generator.ts +0 -150
  128. package/src/utils/detect-schema-vendor.test.ts +0 -123
  129. package/src/utils/detect-schema-vendor.ts +0 -120
  130. package/src/utils/does-type-extend-core-type.ts +0 -53
  131. package/src/utils/ensure-function-metadata.ts +0 -385
  132. package/src/utils/extract-function-name.ts +0 -612
  133. package/src/utils/extract-function-node.ts +0 -70
  134. package/src/utils/extract-node-value.test.ts +0 -117
  135. package/src/utils/extract-node-value.ts +0 -172
  136. package/src/utils/extract-secret-usage.test.ts +0 -81
  137. package/src/utils/extract-secret-usage.ts +0 -73
  138. package/src/utils/extract-services.ts +0 -70
  139. package/src/utils/extract-surface-usage.test.ts +0 -147
  140. package/src/utils/extract-surface-usage.ts +0 -121
  141. package/src/utils/filter-inspector-state.test.ts +0 -2232
  142. package/src/utils/filter-inspector-state.ts +0 -1298
  143. package/src/utils/filter-utils.test.ts +0 -843
  144. package/src/utils/filter-utils.ts +0 -161
  145. package/src/utils/find-root-dir.ts +0 -68
  146. package/src/utils/find-zod-transform.test.ts +0 -70
  147. package/src/utils/find-zod-transform.ts +0 -103
  148. package/src/utils/get-exported-variable-name.ts +0 -48
  149. package/src/utils/get-files-and-methods.ts +0 -157
  150. package/src/utils/get-property-value.test.ts +0 -141
  151. package/src/utils/get-property-value.ts +0 -331
  152. package/src/utils/hash.ts +0 -26
  153. package/src/utils/literal-properties.ts +0 -97
  154. package/src/utils/load-addon-functions-meta.test.ts +0 -157
  155. package/src/utils/load-addon-functions-meta.ts +0 -508
  156. package/src/utils/load-addon-scopes.test.ts +0 -138
  157. package/src/utils/middleware.test.ts +0 -204
  158. package/src/utils/middleware.ts +0 -303
  159. package/src/utils/nested-project-filter.test.ts +0 -53
  160. package/src/utils/nested-project-filter.ts +0 -28
  161. package/src/utils/permissions-shorthand.test.ts +0 -79
  162. package/src/utils/permissions.test.ts +0 -125
  163. package/src/utils/permissions.ts +0 -129
  164. package/src/utils/post-process.test.ts +0 -1094
  165. package/src/utils/post-process.ts +0 -1604
  166. package/src/utils/resolve-addon-package.ts +0 -106
  167. package/src/utils/resolve-core-type.test.ts +0 -142
  168. package/src/utils/resolve-core-type.ts +0 -35
  169. package/src/utils/resolve-deploy-target.test.ts +0 -135
  170. package/src/utils/resolve-deploy-target.ts +0 -65
  171. package/src/utils/resolve-function-meta.ts +0 -25
  172. package/src/utils/resolve-function-types.ts +0 -42
  173. package/src/utils/resolve-identifier.ts +0 -46
  174. package/src/utils/resolve-ref-contract.ts +0 -71
  175. package/src/utils/resolve-versions.test.ts +0 -499
  176. package/src/utils/resolve-versions.ts +0 -228
  177. package/src/utils/schema-cache.test.ts +0 -124
  178. package/src/utils/schema-generator.test.ts +0 -272
  179. package/src/utils/schema-generator.ts +0 -780
  180. package/src/utils/secret-alias-services.test.ts +0 -146
  181. package/src/utils/secret-alias-services.ts +0 -58
  182. package/src/utils/serialize-inspector-state.ts +0 -742
  183. package/src/utils/serialize-mcp-json.ts +0 -150
  184. package/src/utils/serialize-middleware-groups-meta.ts +0 -36
  185. package/src/utils/serialize-openapi-json.ts +0 -277
  186. package/src/utils/serialize-permissions-groups-meta.ts +0 -7
  187. package/src/utils/single-declaration.ts +0 -57
  188. package/src/utils/type-utils.ts +0 -145
  189. package/src/utils/validate-auth-sessionless.ts +0 -30
  190. package/src/utils/validate-exposed-functions-gated.test.ts +0 -230
  191. package/src/utils/validate-exposed-functions-gated.ts +0 -103
  192. package/src/utils/validate-tags-resolve-to-middleware.test.ts +0 -108
  193. package/src/utils/validate-tags-resolve-to-middleware.ts +0 -48
  194. package/src/utils/workflow/derive-workflow-plan.test.ts +0 -178
  195. package/src/utils/workflow/derive-workflow-plan.ts +0 -117
  196. package/src/utils/workflow/dsl/deserialize-dsl-workflow.ts +0 -1465
  197. package/src/utils/workflow/dsl/deserialize-fanout-body.test.ts +0 -71
  198. package/src/utils/workflow/dsl/deserialize-fanout-fidelity.test.ts +0 -99
  199. package/src/utils/workflow/dsl/deserialize-graph-next.test.ts +0 -161
  200. package/src/utils/workflow/dsl/deserialize-roundtrip-bindings.test.ts +0 -121
  201. package/src/utils/workflow/dsl/deserialize-roundtrip-values.test.ts +0 -116
  202. package/src/utils/workflow/dsl/deserialize-roundtrip.test.ts +0 -145
  203. package/src/utils/workflow/dsl/extract-approval-policy.test.ts +0 -71
  204. package/src/utils/workflow/dsl/extract-dsl-workflow.ts +0 -2254
  205. package/src/utils/workflow/dsl/index.ts +0 -11
  206. package/src/utils/workflow/dsl/patterns.ts +0 -440
  207. package/src/utils/workflow/dsl/validation.ts +0 -203
  208. package/src/utils/workflow/graph/convert-dsl-to-graph-skip.test.ts +0 -35
  209. package/src/utils/workflow/graph/convert-dsl-to-graph.ts +0 -543
  210. package/src/utils/workflow/graph/fanout-node-identity.test.ts +0 -105
  211. package/src/utils/workflow/graph/finalize-workflow-wires.ts +0 -310
  212. package/src/utils/workflow/graph/finalize-workflows.test.ts +0 -50
  213. package/src/utils/workflow/graph/finalize-workflows.ts +0 -130
  214. package/src/utils/workflow/graph/index.ts +0 -28
  215. package/src/utils/workflow/graph/serialize-workflow-graph.ts +0 -240
  216. package/src/utils/workflow/graph/workflow-graph.types.ts +0 -281
  217. package/src/visit.ts +0 -181
  218. package/tsconfig.json +0 -19
  219. package/tsconfig.tsbuildinfo +0 -1
@@ -1,638 +0,0 @@
1
- import * as ts from 'typescript'
2
- import {
3
- getPropertyValue,
4
- getCommonWireMetaData,
5
- } from '../utils/get-property-value.js'
6
- import { extractWireNames } from '../utils/post-process.js'
7
- import type { AddWiring, InspectorLogger, SchemaRef } from '../types.js'
8
- import {
9
- extractFunctionName,
10
- funcIdToTypeName,
11
- } from '../utils/extract-function-name.js'
12
- import {
13
- resolveMiddleware,
14
- resolveChannelMiddleware,
15
- resolveAgentMiddleware,
16
- } from '../utils/middleware.js'
17
- import { resolvePermissions } from '../utils/permissions.js'
18
- import { ErrorCode } from '../error-codes.js'
19
- import { detectSchemaVendorOrError } from '../utils/detect-schema-vendor.js'
20
-
21
- function resolveToolReferences(
22
- obj: ts.ObjectLiteralExpression,
23
- checker: ts.TypeChecker,
24
- agentName: string,
25
- logger: InspectorLogger
26
- ): string[] | null {
27
- const property = obj.properties.find(
28
- (p) =>
29
- ts.isPropertyAssignment(p) &&
30
- ts.isIdentifier(p.name) &&
31
- p.name.text === 'tools'
32
- )
33
-
34
- if (!property || !ts.isPropertyAssignment(property)) {
35
- return null
36
- }
37
-
38
- const initializer = property.initializer
39
- if (!ts.isArrayLiteralExpression(initializer)) {
40
- return null
41
- }
42
-
43
- const resolved: string[] = []
44
-
45
- for (const element of initializer.elements) {
46
- if (ts.isStringLiteral(element)) {
47
- logger.critical(
48
- ErrorCode.INVALID_VALUE,
49
- `AI agent '${agentName}' tools array contains a string literal '${element.text}'. ` +
50
- `Use a function reference instead (e.g., import the pikkuFunc variable).`
51
- )
52
- continue
53
- }
54
-
55
- if (ts.isCallExpression(element) && ts.isIdentifier(element.expression)) {
56
- const calleeName = element.expression.text
57
-
58
- if (calleeName === 'workflow') {
59
- const [firstArg] = element.arguments
60
- if (firstArg && ts.isStringLiteral(firstArg)) {
61
- resolved.push(`workflow:${firstArg.text}`)
62
- continue
63
- }
64
- }
65
-
66
- if (calleeName === 'ref') {
67
- const [firstArg] = element.arguments
68
- if (firstArg && ts.isStringLiteral(firstArg)) {
69
- resolved.push(firstArg.text)
70
- continue
71
- }
72
- }
73
- }
74
-
75
- if (ts.isIdentifier(element)) {
76
- const rpcName = resolveIdentifierToRpcName(element, checker)
77
- if (rpcName) {
78
- resolved.push(rpcName)
79
- continue
80
- }
81
-
82
- logger.critical(
83
- ErrorCode.INVALID_VALUE,
84
- `AI agent '${agentName}' tools array contains identifier '${element.text}' ` +
85
- `that could not be resolved to a pikkuFunc.`
86
- )
87
- }
88
- }
89
-
90
- return resolved.length > 0 ? resolved : null
91
- }
92
-
93
- function resolveAgentReferences(
94
- obj: ts.ObjectLiteralExpression,
95
- checker: ts.TypeChecker,
96
- agentName: string,
97
- logger: InspectorLogger
98
- ): string[] | null {
99
- const property = obj.properties.find(
100
- (p) =>
101
- ts.isPropertyAssignment(p) &&
102
- ts.isIdentifier(p.name) &&
103
- p.name.text === 'agents'
104
- )
105
-
106
- if (!property || !ts.isPropertyAssignment(property)) {
107
- return null
108
- }
109
-
110
- const initializer = property.initializer
111
- if (!ts.isArrayLiteralExpression(initializer)) {
112
- return null
113
- }
114
-
115
- const resolved: string[] = []
116
-
117
- for (const element of initializer.elements) {
118
- if (ts.isStringLiteral(element)) {
119
- logger.critical(
120
- ErrorCode.INVALID_VALUE,
121
- `AI agent '${agentName}' agents array contains a string literal '${element.text}'. ` +
122
- `Use an agent reference instead (e.g., import the pikkuAgent variable).`
123
- )
124
- continue
125
- }
126
-
127
- if (ts.isIdentifier(element)) {
128
- const name = resolveIdentifierToAgentName(element, checker)
129
- if (name) {
130
- resolved.push(name)
131
- continue
132
- }
133
-
134
- logger.critical(
135
- ErrorCode.INVALID_VALUE,
136
- `AI agent '${agentName}' agents array contains identifier '${element.text}' ` +
137
- `that could not be resolved to a pikkuAgent.`
138
- )
139
- }
140
- }
141
-
142
- return resolved.length > 0 ? resolved : null
143
- }
144
-
145
- export function resolveWorkflowReferences(
146
- obj: ts.ObjectLiteralExpression,
147
- checker: ts.TypeChecker,
148
- agentName: string,
149
- logger: InspectorLogger
150
- ): string[] | null {
151
- const property = obj.properties.find(
152
- (p) =>
153
- ts.isPropertyAssignment(p) &&
154
- ts.isIdentifier(p.name) &&
155
- p.name.text === 'workflows'
156
- )
157
-
158
- if (!property || !ts.isPropertyAssignment(property)) {
159
- return null
160
- }
161
-
162
- const initializer = property.initializer
163
- if (!ts.isArrayLiteralExpression(initializer)) {
164
- return null
165
- }
166
-
167
- const resolved: string[] = []
168
-
169
- for (const element of initializer.elements) {
170
- if (ts.isStringLiteral(element)) {
171
- logger.critical(
172
- ErrorCode.INVALID_VALUE,
173
- `AI agent '${agentName}' workflows array contains a string literal '${element.text}'. ` +
174
- `Use a workflow reference instead (e.g., ref('<workflowName>') or import the pikkuWorkflowGraph variable).`
175
- )
176
- continue
177
- }
178
-
179
- if (ts.isCallExpression(element) && ts.isIdentifier(element.expression)) {
180
- const calleeName = element.expression.text
181
- if (calleeName === 'ref' || calleeName === 'workflow') {
182
- const [firstArg] = element.arguments
183
- if (firstArg && ts.isStringLiteral(firstArg)) {
184
- resolved.push(firstArg.text)
185
- continue
186
- }
187
- }
188
- }
189
-
190
- if (ts.isIdentifier(element)) {
191
- const name = resolveIdentifierToWorkflowName(element, checker)
192
- if (name) {
193
- resolved.push(name)
194
- continue
195
- }
196
-
197
- logger.critical(
198
- ErrorCode.INVALID_VALUE,
199
- `AI agent '${agentName}' workflows array contains identifier '${element.text}' ` +
200
- `that could not be resolved to a pikkuWorkflowGraph.`
201
- )
202
- }
203
- }
204
-
205
- return resolved.length > 0 ? resolved : null
206
- }
207
-
208
- function resolveIdentifierToWorkflowName(
209
- identifier: ts.Identifier,
210
- checker: ts.TypeChecker
211
- ): string | null {
212
- const symbol = checker.getSymbolAtLocation(identifier)
213
- if (!symbol) return null
214
-
215
- let resolved = symbol
216
- if (resolved.flags & ts.SymbolFlags.Alias) {
217
- resolved = checker.getAliasedSymbol(resolved) ?? resolved
218
- }
219
-
220
- const decl = resolved.valueDeclaration ?? resolved.declarations?.[0]
221
- if (!decl) return null
222
-
223
- if (ts.isVariableDeclaration(decl) && decl.initializer) {
224
- if (
225
- ts.isCallExpression(decl.initializer) &&
226
- ts.isIdentifier(decl.initializer.expression) &&
227
- (decl.initializer.expression.text === 'pikkuWorkflowGraph' ||
228
- decl.initializer.expression.text === 'pikkuWorkflow' ||
229
- decl.initializer.expression.text === 'pikkuWorkflowComplex')
230
- ) {
231
- const firstArg = decl.initializer.arguments[0]
232
- if (firstArg && ts.isObjectLiteralExpression(firstArg)) {
233
- for (const prop of firstArg.properties) {
234
- if (
235
- ts.isPropertyAssignment(prop) &&
236
- ts.isIdentifier(prop.name) &&
237
- prop.name.text === 'name' &&
238
- ts.isStringLiteral(prop.initializer)
239
- ) {
240
- return prop.initializer.text
241
- }
242
- }
243
- }
244
- if (ts.isIdentifier(decl.name)) {
245
- return decl.name.text
246
- }
247
- }
248
- }
249
-
250
- return null
251
- }
252
-
253
- function resolveIdentifierToRpcName(
254
- identifier: ts.Identifier,
255
- checker: ts.TypeChecker
256
- ): string | null {
257
- const symbol = checker.getSymbolAtLocation(identifier)
258
- if (!symbol) return null
259
-
260
- let resolved = symbol
261
- if (resolved.flags & ts.SymbolFlags.Alias) {
262
- resolved = checker.getAliasedSymbol(resolved) ?? resolved
263
- }
264
-
265
- const decl = resolved.valueDeclaration ?? resolved.declarations?.[0]
266
- if (!decl) return null
267
-
268
- if (ts.isVariableDeclaration(decl) && decl.initializer) {
269
- if (
270
- ts.isCallExpression(decl.initializer) &&
271
- ts.isIdentifier(decl.initializer.expression)
272
- ) {
273
- const callName = decl.initializer.expression.text
274
- if (
275
- callName === 'pikkuFunc' ||
276
- callName === 'pikkuSessionlessFunc' ||
277
- callName === 'pikkuVoidFunc'
278
- ) {
279
- const firstArg = decl.initializer.arguments[0]
280
- if (firstArg && ts.isObjectLiteralExpression(firstArg)) {
281
- for (const prop of firstArg.properties) {
282
- if (
283
- ts.isPropertyAssignment(prop) &&
284
- ts.isIdentifier(prop.name) &&
285
- prop.name.text === 'override' &&
286
- ts.isStringLiteral(prop.initializer)
287
- ) {
288
- return prop.initializer.text
289
- }
290
- }
291
- }
292
-
293
- if (ts.isIdentifier(decl.name)) {
294
- return decl.name.text
295
- }
296
- }
297
- }
298
- }
299
-
300
- return null
301
- }
302
-
303
- function resolveIdentifierToAgentName(
304
- identifier: ts.Identifier,
305
- checker: ts.TypeChecker
306
- ): string | null {
307
- const symbol = checker.getSymbolAtLocation(identifier)
308
- if (!symbol) return null
309
-
310
- let resolved = symbol
311
- if (resolved.flags & ts.SymbolFlags.Alias) {
312
- resolved = checker.getAliasedSymbol(resolved) ?? resolved
313
- }
314
-
315
- const decl = resolved.valueDeclaration ?? resolved.declarations?.[0]
316
- if (!decl) return null
317
-
318
- if (ts.isVariableDeclaration(decl) && decl.initializer) {
319
- if (
320
- ts.isCallExpression(decl.initializer) &&
321
- ts.isIdentifier(decl.initializer.expression) &&
322
- decl.initializer.expression.text === 'pikkuAgent'
323
- ) {
324
- if (ts.isIdentifier(decl.name)) {
325
- return decl.name.text
326
- }
327
- }
328
- }
329
-
330
- return null
331
- }
332
-
333
- /**
334
- * The scorers an agent asks to be graded by, which are plain names rather than
335
- * references: a scorer is resolved out of the registry at grading time, not
336
- * imported by the agent, so nothing here has a declaration to chase.
337
- */
338
- function resolveScorerNames(
339
- obj: ts.ObjectLiteralExpression,
340
- agentName: string,
341
- logger: InspectorLogger
342
- ): string[] | null {
343
- const property = obj.properties.find(
344
- (p) =>
345
- ts.isPropertyAssignment(p) &&
346
- ts.isIdentifier(p.name) &&
347
- p.name.text === 'scorers'
348
- )
349
- if (!property || !ts.isPropertyAssignment(property)) return null
350
- if (!ts.isArrayLiteralExpression(property.initializer)) return null
351
-
352
- const names: string[] = []
353
- for (const element of property.initializer.elements) {
354
- if (!ts.isStringLiteral(element)) {
355
- logger.critical(
356
- ErrorCode.INVALID_VALUE,
357
- `AI agent '${agentName}' lists a scorer that is not an inline string: \`${element.getText()}\`. A scorer is named, not imported, so the name has to be readable from source.`
358
- )
359
- continue
360
- }
361
- names.push(element.text)
362
- }
363
- return names.length > 0 ? names : null
364
- }
365
-
366
- export const addAgent: AddWiring = (logger, node, checker, state, options) => {
367
- if (!ts.isCallExpression(node)) {
368
- return
369
- }
370
-
371
- const args = node.arguments
372
- const firstArg = args[0]
373
- const expression = node.expression
374
-
375
- if (!ts.isIdentifier(expression) || expression.text !== 'pikkuAgent') {
376
- return
377
- }
378
-
379
- if (!firstArg) {
380
- return
381
- }
382
-
383
- const { exportedName } = extractFunctionName(node, checker, state.rootDir)
384
-
385
- if (ts.isObjectLiteralExpression(firstArg)) {
386
- const obj = firstArg
387
-
388
- const nameValue = getPropertyValue(obj, 'name') as string | null
389
- const { disabled, tags, summary, description, errors } =
390
- getCommonWireMetaData(obj, 'AI agent', nameValue, logger, checker)
391
-
392
- if (disabled) return
393
-
394
- const modelValue = getPropertyValue(obj, 'model') as string | null
395
-
396
- const roleValue = getPropertyValue(obj, 'role') as string | null
397
- const personalityValue = getPropertyValue(obj, 'personality') as
398
- string | null
399
- const goalValue = getPropertyValue(obj, 'goal') as string | null
400
-
401
- const maxStepsValue = getPropertyValue(obj, 'maxSteps') as number | null
402
- const temperatureValue = getPropertyValue(obj, 'temperature') as
403
- number | null
404
- const toolChoiceValue = getPropertyValue(obj, 'toolChoice') as string | null
405
- const scorersValue = resolveScorerNames(obj, nameValue || '', logger)
406
- const toolsValue = resolveToolReferences(
407
- obj,
408
- checker,
409
- nameValue || '',
410
- logger
411
- )
412
-
413
- if (toolsValue) {
414
- for (const toolName of toolsValue) {
415
- if (toolName.startsWith('workflow:') || toolName.includes(':')) continue
416
- const funcFile = state.functions.files.get(toolName)
417
- if (funcFile && !state.rpc.internalFiles.has(toolName)) {
418
- state.rpc.internalFiles.set(toolName, funcFile)
419
- }
420
- }
421
- }
422
-
423
- const agentsValue = resolveAgentReferences(
424
- obj,
425
- checker,
426
- nameValue || '',
427
- logger
428
- )
429
-
430
- const workflowsValue = resolveWorkflowReferences(
431
- obj,
432
- checker,
433
- nameValue || '',
434
- logger
435
- )
436
-
437
- if (workflowsValue) {
438
- for (const workflowName of workflowsValue) {
439
- state.workflows.invokedWorkflows.add(workflowName)
440
- }
441
- }
442
-
443
- if (!nameValue) {
444
- logger.critical(
445
- ErrorCode.MISSING_NAME,
446
- "AI agent is missing the required 'name' property."
447
- )
448
- return
449
- }
450
-
451
- const agentKey = exportedName || nameValue
452
-
453
- if (!description) {
454
- logger.critical(
455
- ErrorCode.MISSING_DESCRIPTION,
456
- `AI agent '${nameValue}' is missing a description.`
457
- )
458
- return
459
- }
460
-
461
- const resolveSchemaRef = (
462
- identifier: ts.Identifier,
463
- context: string
464
- ): SchemaRef | null => {
465
- const symbol = checker.getSymbolAtLocation(identifier)
466
- if (!symbol) return null
467
-
468
- const decl = symbol.valueDeclaration || symbol.declarations?.[0]
469
- if (!decl) return null
470
-
471
- let sourceFile: string
472
-
473
- if (ts.isImportSpecifier(decl)) {
474
- const aliasedSymbol = checker.getAliasedSymbol(symbol)
475
- if (aliasedSymbol) {
476
- const aliasedDecl =
477
- aliasedSymbol.valueDeclaration || aliasedSymbol.declarations?.[0]
478
- if (aliasedDecl) {
479
- sourceFile = aliasedDecl.getSourceFile().fileName
480
- } else {
481
- return null
482
- }
483
- } else {
484
- return null
485
- }
486
- } else {
487
- sourceFile = decl.getSourceFile().fileName
488
- }
489
-
490
- const vendor = detectSchemaVendorOrError(
491
- identifier,
492
- checker,
493
- logger,
494
- context,
495
- sourceFile
496
- )
497
- if (!vendor) return null
498
-
499
- return {
500
- variableName: identifier.text,
501
- sourceFile,
502
- vendor,
503
- }
504
- }
505
-
506
- let inputSchema: string | null = null
507
- let outputSchema: string | null = null
508
- let workingMemorySchema: string | null = null
509
- const capitalizedName = funcIdToTypeName(agentKey)
510
-
511
- for (const prop of obj.properties) {
512
- if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
513
- const propName = prop.name.text
514
- if (propName === 'input' || propName === 'output') {
515
- if (ts.isIdentifier(prop.initializer)) {
516
- const context = `AI agent '${nameValue}' ${propName}`
517
- const ref = resolveSchemaRef(prop.initializer, context)
518
- if (ref) {
519
- const schemaName = `${capitalizedName}${propName.charAt(0).toUpperCase() + propName.slice(1)}`
520
- state.schemaLookup.set(schemaName, ref)
521
- state.functions.typesMap.addCustomType(schemaName, 'unknown', [])
522
- if (propName === 'input') {
523
- inputSchema = schemaName
524
- } else {
525
- outputSchema = schemaName
526
- }
527
- }
528
- } else if (ts.isCallExpression(prop.initializer)) {
529
- const schemaName = `${capitalizedName}${propName.charAt(0).toUpperCase() + propName.slice(1)}`
530
- logger.critical(
531
- ErrorCode.INLINE_SCHEMA,
532
- `Inline schemas are not supported for '${propName}' in AI agent '${nameValue}'.\n` +
533
- ` Extract to an exported variable:\n` +
534
- ` export const ${schemaName} = ${prop.initializer.getText()}\n` +
535
- ` Then use: ${propName}: ${schemaName}`
536
- )
537
- }
538
- } else if (
539
- propName === 'memory' &&
540
- ts.isObjectLiteralExpression(prop.initializer)
541
- ) {
542
- for (const memProp of prop.initializer.properties) {
543
- if (
544
- ts.isPropertyAssignment(memProp) &&
545
- ts.isIdentifier(memProp.name) &&
546
- memProp.name.text === 'workingMemory' &&
547
- ts.isIdentifier(memProp.initializer)
548
- ) {
549
- const context = `AI agent '${nameValue}' workingMemory`
550
- const ref = resolveSchemaRef(memProp.initializer, context)
551
- if (ref) {
552
- const schemaName = `${capitalizedName}WorkingMemory`
553
- state.schemaLookup.set(schemaName, ref)
554
- state.functions.typesMap.addCustomType(
555
- schemaName,
556
- 'unknown',
557
- []
558
- )
559
- workingMemorySchema = schemaName
560
- }
561
- }
562
- }
563
- }
564
- }
565
- }
566
-
567
- const middleware = resolveMiddleware(state, obj, tags, checker)
568
- const channelMiddleware = resolveChannelMiddleware(
569
- state,
570
- obj,
571
- tags,
572
- checker
573
- )
574
- const agentMiddleware = resolveAgentMiddleware(state, obj, checker)
575
- const permissions = resolvePermissions(state, obj, tags, checker)
576
-
577
- state.serviceAggregation.usedFunctions.add(agentKey)
578
- extractWireNames(middleware).forEach((name) =>
579
- state.serviceAggregation.usedMiddleware.add(name)
580
- )
581
- extractWireNames(permissions).forEach((name) =>
582
- state.serviceAggregation.usedPermissions.add(name)
583
- )
584
-
585
- if (exportedName) {
586
- state.agents.files.set(agentKey, {
587
- path: node.getSourceFile().fileName,
588
- exportedName,
589
- })
590
- }
591
-
592
- state.agents.agentsMeta[agentKey] = {
593
- name: nameValue,
594
- description,
595
- role: roleValue || undefined,
596
- personality: personalityValue || undefined,
597
- goal: goalValue || '',
598
- model: modelValue || '',
599
- summary,
600
- errors,
601
- sourceFile: node.getSourceFile().fileName,
602
- exportedName: exportedName || undefined,
603
- ...(maxStepsValue !== null && { maxSteps: maxStepsValue }),
604
- ...(temperatureValue !== null && { temperature: temperatureValue }),
605
- ...(toolChoiceValue !== null && {
606
- toolChoice: toolChoiceValue as 'auto' | 'required' | 'none',
607
- }),
608
- ...(toolsValue !== null && { tools: toolsValue }),
609
- ...(agentsValue !== null && { agents: agentsValue }),
610
- ...(workflowsValue !== null && { workflows: workflowsValue }),
611
- ...(scorersValue !== null && { scorers: scorersValue }),
612
- tags,
613
- inputSchema,
614
- outputSchema,
615
- workingMemorySchema,
616
- middleware,
617
- channelMiddleware,
618
- agentMiddleware,
619
- permissions,
620
- }
621
-
622
- // AI agent functions require platform services that aren't visible
623
- // through parameter destructuring
624
- const funcMeta = state.functions.meta[agentKey]
625
- if (funcMeta?.services) {
626
- for (const svc of [
627
- 'agentStorage',
628
- 'agentRunState',
629
- 'agentRunService',
630
- 'agentRunner',
631
- ]) {
632
- if (!funcMeta.services.services.includes(svc)) {
633
- funcMeta.services.services.push(svc)
634
- }
635
- }
636
- }
637
- }
638
- }
@@ -1,76 +0,0 @@
1
- import * as ts from 'typescript'
2
- import type { AddWiring } from '../types.js'
3
- import { extractFunctionName } from '../utils/extract-function-name.js'
4
- import {
5
- extractServicesFromFunction,
6
- extractUsedWires,
7
- } from '../utils/extract-services.js'
8
-
9
- /**
10
- * Inspect pikkuApprovalDescription() calls and extract metadata
11
- */
12
- export const addApprovalDescription: AddWiring = (
13
- logger,
14
- node,
15
- checker,
16
- state
17
- ) => {
18
- if (!ts.isCallExpression(node)) return
19
-
20
- const { expression, arguments: args } = node
21
-
22
- if (!ts.isIdentifier(expression)) return
23
- if (expression.text !== 'pikkuApprovalDescription') return
24
-
25
- const arg = args[0]
26
- if (!arg) return
27
-
28
- let actualHandler: ts.ArrowFunction | ts.FunctionExpression
29
-
30
- if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) {
31
- actualHandler = arg
32
- } else {
33
- logger.error(`• Handler for pikkuApprovalDescription is not a function.`)
34
- return
35
- }
36
-
37
- const services = extractServicesFromFunction(actualHandler)
38
- const wires = extractUsedWires(actualHandler, 1)
39
- let { pikkuFuncId, exportedName } = extractFunctionName(
40
- node,
41
- checker,
42
- state.rootDir
43
- )
44
-
45
- if (pikkuFuncId.startsWith('__temp_')) {
46
- if (
47
- ts.isVariableDeclaration(node.parent) &&
48
- ts.isIdentifier(node.parent.name)
49
- ) {
50
- pikkuFuncId = node.parent.name.text
51
- } else if (
52
- ts.isPropertyAssignment(node.parent) &&
53
- ts.isIdentifier(node.parent.name)
54
- ) {
55
- pikkuFuncId = node.parent.name.text
56
- } else {
57
- logger.error(
58
- `• pikkuApprovalDescription() must be assigned to a variable or object property. ` +
59
- `Extract it to a const: const myApproval = pikkuApprovalDescription(...)`
60
- )
61
- return
62
- }
63
- }
64
-
65
- state.functions.approvalDescriptions[pikkuFuncId] = {
66
- services,
67
- wires: wires.wires.length > 0 || !wires.optimized ? wires : undefined,
68
- sourceFile: node.getSourceFile().fileName,
69
- position: node.getStart(),
70
- exportedName,
71
- }
72
-
73
- logger.debug(
74
- `• Found approval description '${pikkuFuncId}' with services: ${services.services.join(', ')}`
75
- )
76
- }