@pikku/core 0.12.2 → 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 (282) hide show
  1. package/CHANGELOG.md +24 -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 +22 -3
  14. package/dist/services/in-memory-workflow-service.js +77 -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/lcov.info +18891 -0
  74. package/package.json +4 -2
  75. package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +0 -5
  76. package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +0 -168
  77. package/src/crypto-utils.test.ts +0 -116
  78. package/src/crypto-utils.ts +0 -99
  79. package/src/errors/error-handler.ts +0 -62
  80. package/src/errors/error.test.ts +0 -195
  81. package/src/errors/errors.ts +0 -374
  82. package/src/errors/index.ts +0 -2
  83. package/src/factory-functions.test.ts +0 -109
  84. package/src/function/function-runner.test.ts +0 -536
  85. package/src/function/function-runner.ts +0 -365
  86. package/src/function/functions.types.ts +0 -267
  87. package/src/function/index.ts +0 -5
  88. package/src/handle-error.test.ts +0 -361
  89. package/src/handle-error.ts +0 -65
  90. package/src/index.ts +0 -104
  91. package/src/internal.ts +0 -6
  92. package/src/middleware/auth-apikey.test.ts +0 -363
  93. package/src/middleware/auth-apikey.ts +0 -47
  94. package/src/middleware/auth-bearer.test.ts +0 -450
  95. package/src/middleware/auth-bearer.ts +0 -72
  96. package/src/middleware/auth-cookie.test.ts +0 -528
  97. package/src/middleware/auth-cookie.ts +0 -80
  98. package/src/middleware/cors.test.ts +0 -424
  99. package/src/middleware/cors.ts +0 -104
  100. package/src/middleware/index.ts +0 -5
  101. package/src/middleware/remote-auth.test.ts +0 -488
  102. package/src/middleware/remote-auth.ts +0 -68
  103. package/src/middleware/timeout.ts +0 -15
  104. package/src/middleware-runner.test.ts +0 -418
  105. package/src/middleware-runner.ts +0 -234
  106. package/src/permissions.test.ts +0 -434
  107. package/src/permissions.ts +0 -319
  108. package/src/pikku-request.ts +0 -23
  109. package/src/pikku-response.ts +0 -5
  110. package/src/pikku-state.test.ts +0 -224
  111. package/src/pikku-state.ts +0 -214
  112. package/src/run-tests-script.test.ts +0 -49
  113. package/src/schema.test.ts +0 -249
  114. package/src/schema.ts +0 -151
  115. package/src/services/ai-agent-runner-service.ts +0 -41
  116. package/src/services/ai-run-state-service.ts +0 -20
  117. package/src/services/ai-storage-service.ts +0 -39
  118. package/src/services/content-service.ts +0 -76
  119. package/src/services/deployment-service.ts +0 -22
  120. package/src/services/gateway-service.ts +0 -20
  121. package/src/services/gopass-secrets.ts +0 -98
  122. package/src/services/in-memory-ai-run-state-service.ts +0 -73
  123. package/src/services/in-memory-trigger-service.ts +0 -64
  124. package/src/services/in-memory-workflow-service.test.ts +0 -351
  125. package/src/services/in-memory-workflow-service.ts +0 -395
  126. package/src/services/index.ts +0 -56
  127. package/src/services/jwt-service.ts +0 -30
  128. package/src/services/local-content.ts +0 -120
  129. package/src/services/local-gateway-service.ts +0 -62
  130. package/src/services/local-secrets.test.ts +0 -80
  131. package/src/services/local-secrets.ts +0 -94
  132. package/src/services/local-variables.test.ts +0 -61
  133. package/src/services/local-variables.ts +0 -39
  134. package/src/services/logger-console.test.ts +0 -118
  135. package/src/services/logger-console.ts +0 -98
  136. package/src/services/logger.ts +0 -57
  137. package/src/services/scheduler-service.ts +0 -86
  138. package/src/services/schema-service.ts +0 -33
  139. package/src/services/scoped-secret-service.test.ts +0 -74
  140. package/src/services/scoped-secret-service.ts +0 -41
  141. package/src/services/secret-service.ts +0 -38
  142. package/src/services/trigger-service.ts +0 -17
  143. package/src/services/typed-secret-service.test.ts +0 -93
  144. package/src/services/typed-secret-service.ts +0 -70
  145. package/src/services/typed-variables-service.test.ts +0 -73
  146. package/src/services/typed-variables-service.ts +0 -81
  147. package/src/services/user-session-service.test.ts +0 -113
  148. package/src/services/user-session-service.ts +0 -86
  149. package/src/services/variables-service.ts +0 -11
  150. package/src/services/workflow-service.ts +0 -84
  151. package/src/time-utils.test.ts +0 -56
  152. package/src/time-utils.ts +0 -107
  153. package/src/types/core.types.ts +0 -470
  154. package/src/types/state.types.ts +0 -186
  155. package/src/utils.test.ts +0 -350
  156. package/src/utils.ts +0 -129
  157. package/src/version.test.ts +0 -80
  158. package/src/version.ts +0 -25
  159. package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +0 -363
  160. package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +0 -238
  161. package/src/wirings/ai-agent/ai-agent-helpers.test.ts +0 -152
  162. package/src/wirings/ai-agent/ai-agent-helpers.ts +0 -76
  163. package/src/wirings/ai-agent/ai-agent-memory.ts +0 -309
  164. package/src/wirings/ai-agent/ai-agent-model-config.test.ts +0 -115
  165. package/src/wirings/ai-agent/ai-agent-model-config.ts +0 -43
  166. package/src/wirings/ai-agent/ai-agent-prepare.ts +0 -522
  167. package/src/wirings/ai-agent/ai-agent-registry.test.ts +0 -37
  168. package/src/wirings/ai-agent/ai-agent-registry.ts +0 -82
  169. package/src/wirings/ai-agent/ai-agent-runner.test.ts +0 -317
  170. package/src/wirings/ai-agent/ai-agent-runner.ts +0 -251
  171. package/src/wirings/ai-agent/ai-agent-stream.test.ts +0 -652
  172. package/src/wirings/ai-agent/ai-agent-stream.ts +0 -1060
  173. package/src/wirings/ai-agent/ai-agent.types.ts +0 -360
  174. package/src/wirings/ai-agent/index.ts +0 -41
  175. package/src/wirings/channel/channel-common.ts +0 -90
  176. package/src/wirings/channel/channel-handler.ts +0 -241
  177. package/src/wirings/channel/channel-middleware-runner.ts +0 -120
  178. package/src/wirings/channel/channel-runner.ts +0 -166
  179. package/src/wirings/channel/channel-store.ts +0 -29
  180. package/src/wirings/channel/channel.types.ts +0 -178
  181. package/src/wirings/channel/define-channel-routes.ts +0 -25
  182. package/src/wirings/channel/eventhub-service.ts +0 -34
  183. package/src/wirings/channel/eventhub-store.ts +0 -13
  184. package/src/wirings/channel/index.ts +0 -24
  185. package/src/wirings/channel/local/index.ts +0 -3
  186. package/src/wirings/channel/local/local-channel-handler.ts +0 -79
  187. package/src/wirings/channel/local/local-channel-runner.test.ts +0 -215
  188. package/src/wirings/channel/local/local-channel-runner.ts +0 -200
  189. package/src/wirings/channel/local/local-eventhub-service.test.ts +0 -95
  190. package/src/wirings/channel/local/local-eventhub-service.ts +0 -101
  191. package/src/wirings/channel/log-channels.ts +0 -20
  192. package/src/wirings/channel/pikku-abstract-channel-handler.test.ts +0 -54
  193. package/src/wirings/channel/pikku-abstract-channel-handler.ts +0 -45
  194. package/src/wirings/channel/serverless/index.ts +0 -5
  195. package/src/wirings/channel/serverless/serverless-channel-runner.ts +0 -286
  196. package/src/wirings/cli/channel/cli-channel-runner.ts +0 -136
  197. package/src/wirings/cli/channel/index.ts +0 -1
  198. package/src/wirings/cli/cli-runner.test.ts +0 -403
  199. package/src/wirings/cli/cli-runner.ts +0 -519
  200. package/src/wirings/cli/cli.types.ts +0 -357
  201. package/src/wirings/cli/command-parser.test.ts +0 -445
  202. package/src/wirings/cli/command-parser.ts +0 -504
  203. package/src/wirings/cli/define-cli-commands.ts +0 -24
  204. package/src/wirings/cli/index.ts +0 -19
  205. package/src/wirings/gateway/gateway-runner.test.ts +0 -474
  206. package/src/wirings/gateway/gateway-runner.ts +0 -411
  207. package/src/wirings/gateway/gateway.types.ts +0 -149
  208. package/src/wirings/gateway/index.ts +0 -13
  209. package/src/wirings/http/http-routes.test.ts +0 -322
  210. package/src/wirings/http/http-routes.ts +0 -198
  211. package/src/wirings/http/http-runner.test.ts +0 -144
  212. package/src/wirings/http/http-runner.ts +0 -563
  213. package/src/wirings/http/http.types.ts +0 -378
  214. package/src/wirings/http/index.ts +0 -25
  215. package/src/wirings/http/log-http-routes.ts +0 -22
  216. package/src/wirings/http/pikku-fetch-http-request.test.ts +0 -237
  217. package/src/wirings/http/pikku-fetch-http-request.ts +0 -166
  218. package/src/wirings/http/pikku-fetch-http-response.test.ts +0 -82
  219. package/src/wirings/http/pikku-fetch-http-response.ts +0 -147
  220. package/src/wirings/http/routers/http-router.ts +0 -13
  221. package/src/wirings/http/routers/path-to-regex.test.ts +0 -319
  222. package/src/wirings/http/routers/path-to-regex.ts +0 -126
  223. package/src/wirings/mcp/index.ts +0 -27
  224. package/src/wirings/mcp/mcp-endpoint-registry.test.ts +0 -389
  225. package/src/wirings/mcp/mcp-endpoint-registry.ts +0 -159
  226. package/src/wirings/mcp/mcp-runner.ts +0 -305
  227. package/src/wirings/mcp/mcp.types.ts +0 -216
  228. package/src/wirings/node/index.ts +0 -2
  229. package/src/wirings/node/node.types.ts +0 -23
  230. package/src/wirings/oauth2/index.ts +0 -3
  231. package/src/wirings/oauth2/oauth2-client.test.ts +0 -929
  232. package/src/wirings/oauth2/oauth2-client.ts +0 -335
  233. package/src/wirings/oauth2/oauth2.types.ts +0 -69
  234. package/src/wirings/oauth2/wire-oauth2-credential.ts +0 -23
  235. package/src/wirings/queue/index.ts +0 -31
  236. package/src/wirings/queue/queue-runner.test.ts +0 -710
  237. package/src/wirings/queue/queue-runner.ts +0 -192
  238. package/src/wirings/queue/queue.types.ts +0 -189
  239. package/src/wirings/queue/register-queue-helper.ts +0 -60
  240. package/src/wirings/queue/validate-worker-config.test.ts +0 -108
  241. package/src/wirings/queue/validate-worker-config.ts +0 -116
  242. package/src/wirings/rpc/index.ts +0 -4
  243. package/src/wirings/rpc/rpc-runner.ts +0 -439
  244. package/src/wirings/rpc/rpc-types.ts +0 -51
  245. package/src/wirings/rpc/wire-addon.ts +0 -20
  246. package/src/wirings/scheduler/index.ts +0 -11
  247. package/src/wirings/scheduler/log-schedulers.ts +0 -20
  248. package/src/wirings/scheduler/scheduler-runner.test.ts +0 -660
  249. package/src/wirings/scheduler/scheduler-runner.ts +0 -132
  250. package/src/wirings/scheduler/scheduler.types.ts +0 -53
  251. package/src/wirings/secret/index.ts +0 -9
  252. package/src/wirings/secret/secret.types.ts +0 -32
  253. package/src/wirings/secret/validate-secret-definitions.test.ts +0 -140
  254. package/src/wirings/secret/validate-secret-definitions.ts +0 -82
  255. package/src/wirings/trigger/index.ts +0 -10
  256. package/src/wirings/trigger/pikku-trigger-service.ts +0 -112
  257. package/src/wirings/trigger/trigger-runner.test.ts +0 -79
  258. package/src/wirings/trigger/trigger-runner.ts +0 -135
  259. package/src/wirings/trigger/trigger.types.ts +0 -178
  260. package/src/wirings/variable/index.ts +0 -8
  261. package/src/wirings/variable/validate-variable-definitions.test.ts +0 -91
  262. package/src/wirings/variable/validate-variable-definitions.ts +0 -69
  263. package/src/wirings/variable/variable.types.ts +0 -22
  264. package/src/wirings/workflow/dsl/index.ts +0 -31
  265. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +0 -320
  266. package/src/wirings/workflow/dsl/workflow-runner.ts +0 -28
  267. package/src/wirings/workflow/graph/graph-node.ts +0 -222
  268. package/src/wirings/workflow/graph/graph-runner.test.ts +0 -487
  269. package/src/wirings/workflow/graph/graph-runner.ts +0 -816
  270. package/src/wirings/workflow/graph/index.ts +0 -14
  271. package/src/wirings/workflow/graph/template.test.ts +0 -49
  272. package/src/wirings/workflow/graph/template.ts +0 -42
  273. package/src/wirings/workflow/graph/wire-workflow-graph.ts +0 -29
  274. package/src/wirings/workflow/graph/workflow-graph.types.ts +0 -104
  275. package/src/wirings/workflow/index.ts +0 -81
  276. package/src/wirings/workflow/pikku-workflow-service.test.ts +0 -200
  277. package/src/wirings/workflow/pikku-workflow-service.ts +0 -1165
  278. package/src/wirings/workflow/workflow-helpers.test.ts +0 -129
  279. package/src/wirings/workflow/workflow-helpers.ts +0 -79
  280. package/src/wirings/workflow/workflow.types.ts +0 -266
  281. package/tsconfig.json +0 -14
  282. package/tsconfig.tsbuildinfo +0 -1
@@ -1,504 +0,0 @@
1
- import type {
2
- CLIMeta,
3
- CLIProgramMeta,
4
- CLICommandMeta,
5
- CLIPositional,
6
- CLIOption,
7
- } from './cli.types.js'
8
-
9
- /**
10
- * Result of parsing CLI arguments
11
- */
12
- export interface ParsedCommand {
13
- program: string
14
- commandPath: string[]
15
- positionals: Record<string, any>
16
- options: Record<string, any>
17
- errors: string[]
18
- }
19
-
20
- /**
21
- * Parses raw CLI arguments into structured data for a specific program
22
- */
23
- export function parseCLIArguments(
24
- args: string[],
25
- programName: string,
26
- allMeta: CLIMeta
27
- ): ParsedCommand {
28
- const result: ParsedCommand = {
29
- program: programName,
30
- commandPath: [],
31
- positionals: {},
32
- options: {},
33
- errors: [],
34
- }
35
-
36
- const meta = allMeta.programs[programName]
37
- if (!meta) {
38
- result.errors.push(`Program not found: ${programName}`)
39
- return result
40
- }
41
-
42
- let currentIndex = 0
43
- let currentMeta = meta
44
-
45
- // Parse command path (non-flag arguments at the beginning)
46
- while (currentIndex < args.length && !args[currentIndex].startsWith('-')) {
47
- const arg = args[currentIndex]
48
-
49
- // Check if this is a known subcommand
50
- if (currentMeta.commands && currentMeta.commands[arg]) {
51
- result.commandPath.push(arg)
52
- currentMeta = {
53
- program: currentMeta.program,
54
- commands: currentMeta.commands[arg].subcommands || {},
55
- options: {
56
- ...currentMeta.options,
57
- ...currentMeta.commands[arg].options,
58
- },
59
- defaultRenderName:
60
- currentMeta.commands[arg].renderName || currentMeta.defaultRenderName,
61
- }
62
- currentIndex++
63
- } else {
64
- // Not a subcommand, must be a positional argument
65
- break
66
- }
67
- }
68
-
69
- // If no command was parsed, check for a default command
70
- // Only use default if user didn't provide any non-flag arguments
71
- const hasNonFlagArgs = args.some(
72
- (arg, idx) => idx >= currentIndex && !arg.startsWith('-')
73
- )
74
- if (result.commandPath.length === 0 && meta.commands && !hasNonFlagArgs) {
75
- for (const [name, cmd] of Object.entries(meta.commands)) {
76
- if (cmd.isDefault) {
77
- result.commandPath.push(name)
78
- currentMeta = {
79
- program: currentMeta.program,
80
- commands: cmd.subcommands || {},
81
- options: {
82
- ...currentMeta.options,
83
- ...cmd.options,
84
- },
85
- defaultRenderName: cmd.renderName || currentMeta.defaultRenderName,
86
- }
87
- break
88
- }
89
- }
90
- }
91
-
92
- // Get the final command metadata
93
- const commandMeta = getCommandMeta(meta, result.commandPath)
94
- if (!commandMeta) {
95
- // If we have no command path but there are non-flag args, the first arg is likely an unknown command
96
- if (
97
- result.commandPath.length === 0 &&
98
- hasNonFlagArgs &&
99
- args.length > 0 &&
100
- !args[0].startsWith('-')
101
- ) {
102
- result.errors.push(`Unknown command: ${args[0]}`)
103
- } else {
104
- result.errors.push(`Unknown command: ${result.commandPath.join(' ')}`)
105
- }
106
- return result
107
- }
108
-
109
- // Collect all available options (global + inherited)
110
- const availableOptions = collectAvailableOptions(meta, result.commandPath)
111
-
112
- // Parse remaining arguments as positionals and options
113
- const positionalArgs: string[] = []
114
- const optionArgs: Record<string, any> = {}
115
-
116
- while (currentIndex < args.length) {
117
- const arg = args[currentIndex]
118
-
119
- if (arg.startsWith('--')) {
120
- // Long option
121
- const equalIndex = arg.indexOf('=')
122
- if (equalIndex > 0) {
123
- // --option=value format
124
- const key = arg.slice(2, equalIndex)
125
- const optionDef = availableOptions[key]
126
-
127
- // Unknown options are allowed for forward compatibility
128
- const value = arg.slice(equalIndex + 1)
129
- optionArgs[key] = parseOptionValue(value, optionDef)
130
- } else {
131
- // --option value format
132
- const key = arg.slice(2)
133
- const optionDef = availableOptions[key]
134
-
135
- // Unknown options are allowed for forward compatibility
136
- if (optionDef && optionDef.array) {
137
- // Array option - collect all following non-flag values
138
- currentIndex++
139
- const values: any[] = []
140
- while (
141
- currentIndex < args.length &&
142
- !args[currentIndex].startsWith('-')
143
- ) {
144
- values.push(parseOptionValue(args[currentIndex], optionDef))
145
- currentIndex++
146
- }
147
- currentIndex-- // Back up one since we'll increment at loop end
148
- optionArgs[key] = values
149
- } else if (
150
- currentIndex + 1 < args.length &&
151
- !args[currentIndex + 1].startsWith('-')
152
- ) {
153
- // Next arg is the value
154
- currentIndex++
155
- optionArgs[key] = parseOptionValue(args[currentIndex], optionDef)
156
- } else {
157
- // Boolean flag
158
- optionArgs[key] = true
159
- }
160
- }
161
- } else if (arg.startsWith('-') && arg.length > 1) {
162
- // Short option(s)
163
- for (let i = 1; i < arg.length; i++) {
164
- const shortFlag = arg[i]
165
-
166
- // Find the corresponding long option
167
- const longOption = findLongOption(shortFlag, availableOptions)
168
- if (longOption) {
169
- // Check if this is the last character and there's a value
170
- if (
171
- i === arg.length - 1 &&
172
- currentIndex + 1 < args.length &&
173
- !args[currentIndex + 1].startsWith('-')
174
- ) {
175
- currentIndex++
176
- optionArgs[longOption] = parseOptionValue(
177
- args[currentIndex],
178
- availableOptions[longOption]
179
- )
180
- } else {
181
- // Boolean flag
182
- optionArgs[longOption] = true
183
- }
184
- } else {
185
- result.errors.push(`Unknown option: -${shortFlag}`)
186
- }
187
- }
188
- } else {
189
- // Positional argument
190
- positionalArgs.push(arg)
191
- }
192
-
193
- currentIndex++
194
- }
195
-
196
- // Map positional arguments to named parameters
197
- mapPositionalArguments(commandMeta.positionals, positionalArgs, result)
198
-
199
- // Apply option defaults and validation
200
- applyOptionDefaults(availableOptions, optionArgs, result)
201
-
202
- result.options = optionArgs
203
-
204
- return result
205
- }
206
-
207
- /**
208
- * Gets the command metadata for a given path
209
- */
210
- function getCommandMeta(
211
- meta: CLIProgramMeta,
212
- path: string[]
213
- ): CLICommandMeta | null {
214
- if (path.length === 0) {
215
- return null
216
- }
217
-
218
- let current = meta.commands[path[0]]
219
- if (!current) {
220
- return null
221
- }
222
-
223
- for (let i = 1; i < path.length; i++) {
224
- if (!current.subcommands || !current.subcommands[path[i]]) {
225
- return null
226
- }
227
- current = current.subcommands[path[i]]
228
- }
229
-
230
- return current
231
- }
232
-
233
- /**
234
- * Collects all available options through the inheritance chain
235
- */
236
- function collectAvailableOptions(
237
- meta: CLIProgramMeta,
238
- path: string[]
239
- ): Record<string, CLIOption> {
240
- let options: Record<string, CLIOption> = { ...meta.options }
241
-
242
- if (path.length === 0) {
243
- return options
244
- }
245
-
246
- // Walk through the command path, merging options
247
- let current = meta.commands[path[0]]
248
- if (current) {
249
- options = { ...options, ...current.options }
250
-
251
- for (let i = 1; i < path.length; i++) {
252
- if (current.subcommands && current.subcommands[path[i]]) {
253
- current = current.subcommands[path[i]]
254
- options = { ...options, ...current.options }
255
- }
256
- }
257
- }
258
-
259
- return options
260
- }
261
-
262
- /**
263
- * Finds the long option name for a short flag
264
- */
265
- function findLongOption(
266
- shortFlag: string,
267
- options: Record<string, CLIOption>
268
- ): string | null {
269
- for (const [name, option] of Object.entries(options)) {
270
- if (option.short === shortFlag) {
271
- return name
272
- }
273
- }
274
- return null
275
- }
276
-
277
- /**
278
- * Parses an option value based on its definition
279
- */
280
- function parseOptionValue(value: string, optionDef?: CLIOption): any {
281
- if (!optionDef) {
282
- // No definition, try to infer type
283
- if (value === 'true') return true
284
- if (value === 'false') return false
285
- if (/^\d+$/.test(value)) return parseInt(value, 10)
286
- if (/^\d*\.\d+$/.test(value)) return parseFloat(value)
287
- return value
288
- }
289
-
290
- // Use default type from option definition
291
- const defaultValue = optionDef.default
292
- if (typeof defaultValue === 'boolean') {
293
- return value === 'true' || value === '1' || value === 'yes'
294
- }
295
- if (typeof defaultValue === 'number') {
296
- return parseFloat(value)
297
- }
298
-
299
- // Check choices
300
- if (optionDef.choices && !optionDef.choices.includes(value as any)) {
301
- // Invalid choice, but return anyway (validation will catch it)
302
- return value
303
- }
304
-
305
- return value
306
- }
307
-
308
- /**
309
- * Maps positional arguments to named parameters
310
- */
311
- function mapPositionalArguments(
312
- positionalDefs: CLIPositional[],
313
- args: string[],
314
- result: ParsedCommand
315
- ) {
316
- let argIndex = 0
317
-
318
- for (const def of positionalDefs) {
319
- if (def.variadic) {
320
- // Collect all remaining arguments
321
- if (argIndex < args.length) {
322
- result.positionals[def.name] = args.slice(argIndex)
323
- argIndex = args.length
324
- } else if (def.required) {
325
- result.errors.push(`Missing required argument: ${def.name}`)
326
- } else {
327
- result.positionals[def.name] = []
328
- }
329
- } else {
330
- // Single argument
331
- if (argIndex < args.length) {
332
- result.positionals[def.name] = args[argIndex]
333
- argIndex++
334
- } else if (def.required) {
335
- result.errors.push(`Missing required argument: ${def.name}`)
336
- }
337
- }
338
- }
339
-
340
- // Check for extra positional arguments
341
- if (argIndex < args.length) {
342
- result.errors.push(
343
- `Unexpected arguments: ${args.slice(argIndex).join(' ')}`
344
- )
345
- }
346
- }
347
-
348
- /**
349
- * Applies option defaults and validates choices
350
- */
351
- function applyOptionDefaults(
352
- optionDefs: Record<string, CLIOption>,
353
- options: Record<string, any>,
354
- result: ParsedCommand
355
- ) {
356
- for (const [name, def] of Object.entries(optionDefs)) {
357
- // Apply default if not provided
358
- if (!(name in options) && def.default !== undefined) {
359
- options[name] = def.default
360
- }
361
-
362
- // Check required
363
- if (def.required && !(name in options)) {
364
- result.errors.push(`Missing required option: --${name}`)
365
- }
366
-
367
- // Validate choices
368
- if (def.choices && name in options) {
369
- const value = options[name]
370
- if (Array.isArray(value)) {
371
- for (const v of value) {
372
- if (!def.choices.includes(v)) {
373
- result.errors.push(
374
- `Invalid value for --${name}: ${v}. Valid choices: ${def.choices.join(', ')}`
375
- )
376
- }
377
- }
378
- } else if (!def.choices.includes(value)) {
379
- result.errors.push(
380
- `Invalid value for --${name}: ${value}. Valid choices: ${def.choices.join(', ')}`
381
- )
382
- }
383
- }
384
- }
385
- }
386
-
387
- /**
388
- * Generates help text for a command in a specific program
389
- */
390
- export function generateCommandHelp(
391
- programName: string,
392
- allMeta: CLIMeta,
393
- commandPath: string[] = []
394
- ): string {
395
- const lines: string[] = []
396
- const meta = allMeta.programs[programName]
397
-
398
- if (!meta) {
399
- return `Program not found: ${programName}`
400
- }
401
-
402
- if (commandPath.length === 0) {
403
- // Root help for the program
404
- lines.push(`Usage: ${programName} <command> [options]`)
405
- lines.push('')
406
- lines.push('Commands:')
407
-
408
- for (const [name, cmd] of Object.entries(meta.commands)) {
409
- const desc = cmd.description || ''
410
- const defaultMarker = cmd.isDefault ? ' (default)' : ''
411
- lines.push(` ${name.padEnd(20)} ${desc}${defaultMarker}`)
412
- }
413
-
414
- if (Object.keys(meta.options).length > 0) {
415
- lines.push('')
416
- lines.push('Options:')
417
- formatOptions(meta.options, lines)
418
- }
419
- } else {
420
- // Command-specific help
421
- const commandMeta = getCommandMeta(meta, commandPath)
422
- if (!commandMeta) {
423
- return `Unknown command: ${commandPath.join(' ')}`
424
- }
425
-
426
- // Usage line
427
- let usage = `${programName} ${commandPath.join(' ')}`
428
- if (commandMeta.parameters) {
429
- usage += ' ' + commandMeta.parameters
430
- }
431
- lines.push(`Usage: ${usage} [options]`)
432
-
433
- if (commandMeta.description) {
434
- lines.push('')
435
- lines.push(commandMeta.description)
436
- }
437
-
438
- // Positional arguments
439
- if (commandMeta.positionals.length > 0) {
440
- lines.push('')
441
- lines.push('Arguments:')
442
- for (const pos of commandMeta.positionals) {
443
- const marker = pos.required ? '<required>' : '[optional]'
444
- const variadic = pos.variadic ? '...' : ''
445
- lines.push(` ${pos.name}${variadic} ${marker}`)
446
- }
447
- }
448
-
449
- // Subcommands
450
- if (
451
- commandMeta.subcommands &&
452
- Object.keys(commandMeta.subcommands).length > 0
453
- ) {
454
- lines.push('')
455
- lines.push('Subcommands:')
456
- for (const [name, sub] of Object.entries(commandMeta.subcommands)) {
457
- const desc = sub.description || ''
458
- lines.push(` ${name.padEnd(20)} ${desc}`)
459
- }
460
- }
461
-
462
- // Options
463
- const availableOptions = collectAvailableOptions(meta, commandPath)
464
- if (Object.keys(availableOptions).length > 0) {
465
- lines.push('')
466
- lines.push('Options:')
467
- formatOptions(availableOptions, lines)
468
- }
469
- }
470
-
471
- return lines.join('\n')
472
- }
473
-
474
- /**
475
- * Formats options for help text
476
- */
477
- function formatOptions(options: Record<string, CLIOption>, lines: string[]) {
478
- for (const [name, opt] of Object.entries(options)) {
479
- let line = ' '
480
- if (opt.short) {
481
- line += `-${opt.short}, `
482
- } else {
483
- line += ' '
484
- }
485
- line += `--${name}`
486
-
487
- if (opt.default !== undefined && typeof opt.default !== 'boolean') {
488
- line += ` <value>`
489
- }
490
-
491
- line = line.padEnd(30)
492
- line += opt.description || ''
493
-
494
- if (opt.default !== undefined) {
495
- line += ` (default: ${JSON.stringify(opt.default)})`
496
- }
497
-
498
- if (opt.choices) {
499
- line += ` [choices: ${opt.choices.join(', ')}]`
500
- }
501
-
502
- lines.push(line)
503
- }
504
- }
@@ -1,24 +0,0 @@
1
- import type { CoreCLICommandConfig } from './cli.types.js'
2
-
3
- /**
4
- * Type-safe helper for defining CLI commands that can be composed.
5
- * Returns the commands record as-is (identity function) for use with `wireCLI`.
6
- *
7
- * @example
8
- * ```typescript
9
- * export const chatCommands = defineCLICommands({
10
- * chat: pikkuCLICommand({ func: chatFunc }),
11
- * 'chat:history': pikkuCLICommand({ func: historyFunc }),
12
- * })
13
- *
14
- * wireCLI({
15
- * program: 'my-app',
16
- * commands: { ...chatCommands, other: pikkuCLICommand({ func: otherFunc }) }
17
- * })
18
- * ```
19
- */
20
- export function defineCLICommands<
21
- T extends Record<string, CoreCLICommandConfig<any, any, any, any>>,
22
- >(commands: T): T {
23
- return commands
24
- }
@@ -1,19 +0,0 @@
1
- export {
2
- wireCLI,
3
- runCLICommand,
4
- pikkuCLIRender,
5
- executeCLI,
6
- CLIError,
7
- } from './cli-runner.js'
8
-
9
- export { parseCLIArguments, generateCommandHelp } from './command-parser.js'
10
-
11
- export { defineCLICommands } from './define-cli-commands.js'
12
- export type {
13
- CLIMeta,
14
- CLICommandMeta,
15
- CLIProgramMeta,
16
- CoreCLI,
17
- CoreCLICommandConfig,
18
- CorePikkuCLIRender,
19
- } from './cli.types.js'