terse-cli 0.9.19 → 0.9.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/README.md +2 -2
  2. package/dist/cliUi.d.ts +3 -1
  3. package/dist/cliUi.d.ts.map +1 -1
  4. package/dist/cliUi.js +3 -2
  5. package/dist/cliUi.js.map +1 -1
  6. package/dist/commands/actor.d.ts.map +1 -1
  7. package/dist/commands/actor.js +32 -5
  8. package/dist/commands/actor.js.map +1 -1
  9. package/dist/commands/actorGenerate.d.ts.map +1 -1
  10. package/dist/commands/actorGenerate.js +22 -86
  11. package/dist/commands/actorGenerate.js.map +1 -1
  12. package/dist/commands/attach.d.ts +0 -7
  13. package/dist/commands/attach.d.ts.map +1 -1
  14. package/dist/commands/attach.js +220 -189
  15. package/dist/commands/attach.js.map +1 -1
  16. package/dist/commands/deploy.d.ts.map +1 -1
  17. package/dist/commands/deploy.js +15 -3
  18. package/dist/commands/deploy.js.map +1 -1
  19. package/dist/commands/generate.d.ts +0 -4
  20. package/dist/commands/generate.d.ts.map +1 -1
  21. package/dist/commands/generate.js +161 -127
  22. package/dist/commands/generate.js.map +1 -1
  23. package/dist/commands/history.d.ts +0 -15
  24. package/dist/commands/history.d.ts.map +1 -1
  25. package/dist/commands/history.js +139 -105
  26. package/dist/commands/history.js.map +1 -1
  27. package/dist/commands/install.d.ts.map +1 -1
  28. package/dist/commands/install.js +6 -4
  29. package/dist/commands/install.js.map +1 -1
  30. package/dist/commands/integrate.d.ts +0 -48
  31. package/dist/commands/integrate.d.ts.map +1 -1
  32. package/dist/commands/integrate.js +897 -699
  33. package/dist/commands/integrate.js.map +1 -1
  34. package/dist/commands/integrateUse.d.ts +0 -12
  35. package/dist/commands/integrateUse.d.ts.map +1 -1
  36. package/dist/commands/integrateUse.js +132 -98
  37. package/dist/commands/integrateUse.js.map +1 -1
  38. package/dist/commands/listen.d.ts +0 -7
  39. package/dist/commands/listen.d.ts.map +1 -1
  40. package/dist/commands/listen.js +98 -81
  41. package/dist/commands/listen.js.map +1 -1
  42. package/dist/commands/memory.d.ts +0 -21
  43. package/dist/commands/memory.d.ts.map +1 -1
  44. package/dist/commands/memory.js +147 -128
  45. package/dist/commands/memory.js.map +1 -1
  46. package/dist/commands/replay.d.ts +0 -2
  47. package/dist/commands/replay.d.ts.map +1 -1
  48. package/dist/commands/replay.js +37 -28
  49. package/dist/commands/replay.js.map +1 -1
  50. package/dist/commands/resume.d.ts +0 -4
  51. package/dist/commands/resume.d.ts.map +1 -1
  52. package/dist/commands/resume.js +34 -28
  53. package/dist/commands/resume.js.map +1 -1
  54. package/dist/commands/run.d.ts +0 -2
  55. package/dist/commands/run.d.ts.map +1 -1
  56. package/dist/commands/run.js +78 -70
  57. package/dist/commands/run.js.map +1 -1
  58. package/dist/commands/state.d.ts +0 -18
  59. package/dist/commands/state.d.ts.map +1 -1
  60. package/dist/commands/state.js +129 -108
  61. package/dist/commands/state.js.map +1 -1
  62. package/dist/commands/test.d.ts +0 -28
  63. package/dist/commands/test.d.ts.map +1 -1
  64. package/dist/commands/test.js +509 -403
  65. package/dist/commands/test.js.map +1 -1
  66. package/dist/commands/toolRun.d.ts +0 -6
  67. package/dist/commands/toolRun.d.ts.map +1 -1
  68. package/dist/commands/toolRun.js +149 -134
  69. package/dist/commands/toolRun.js.map +1 -1
  70. package/dist/index.js +1 -1
  71. package/dist/index.js.map +1 -1
  72. package/dist/integrationApi.d.ts +0 -15
  73. package/dist/integrationApi.d.ts.map +1 -1
  74. package/dist/integrationApi.js +75 -49
  75. package/dist/integrationApi.js.map +1 -1
  76. package/dist/loadJob.d.ts +0 -11
  77. package/dist/loadJob.d.ts.map +1 -1
  78. package/dist/loadJob.js +54 -46
  79. package/dist/loadJob.js.map +1 -1
  80. package/dist/providers/LanguageProvider.d.ts +0 -29
  81. package/dist/providers/LanguageProvider.d.ts.map +1 -1
  82. package/dist/providers/LanguageProvider.js +3 -0
  83. package/dist/providers/LanguageProvider.js.map +1 -1
  84. package/dist/providers/typescript/TypeScriptProvider.d.ts +0 -24
  85. package/dist/providers/typescript/TypeScriptProvider.d.ts.map +1 -1
  86. package/dist/providers/typescript/TypeScriptProvider.js +113 -113
  87. package/dist/providers/typescript/TypeScriptProvider.js.map +1 -1
  88. package/dist/providers/typescript/runtimes/JobRuntime.d.ts +0 -26
  89. package/dist/providers/typescript/runtimes/JobRuntime.d.ts.map +1 -1
  90. package/dist/providers/typescript/runtimes/JobRuntime.js +51 -18
  91. package/dist/providers/typescript/runtimes/JobRuntime.js.map +1 -1
  92. package/dist/providers/typescript/runtimes/directJobRuntime.d.ts +0 -2
  93. package/dist/providers/typescript/runtimes/directJobRuntime.d.ts.map +1 -1
  94. package/dist/providers/typescript/runtimes/directJobRuntime.js +60 -49
  95. package/dist/providers/typescript/runtimes/directJobRuntime.js.map +1 -1
  96. package/dist/providers/typescript/runtimes/durableJobRuntime.d.ts +0 -2
  97. package/dist/providers/typescript/runtimes/durableJobRuntime.d.ts.map +1 -1
  98. package/dist/providers/typescript/runtimes/durableJobRuntime.js +168 -146
  99. package/dist/providers/typescript/runtimes/durableJobRuntime.js.map +1 -1
  100. package/dist/providers/typescript/runtimes/index.d.ts +0 -3
  101. package/dist/providers/typescript/runtimes/index.d.ts.map +1 -1
  102. package/dist/providers/typescript/runtimes/index.js +5 -2
  103. package/dist/providers/typescript/runtimes/index.js.map +1 -1
  104. package/dist/providers/typescript/runtimes/session.d.ts +0 -4
  105. package/dist/providers/typescript/runtimes/session.d.ts.map +1 -1
  106. package/dist/providers/typescript/runtimes/session.js +78 -64
  107. package/dist/providers/typescript/runtimes/session.js.map +1 -1
  108. package/dist/providers/typescript/terseWorkflow.d.ts +0 -7
  109. package/dist/providers/typescript/terseWorkflow.d.ts.map +1 -1
  110. package/dist/providers/typescript/terseWorkflow.js +12 -7
  111. package/dist/providers/typescript/terseWorkflow.js.map +1 -1
  112. package/dist/runLocalTestJob.d.ts +0 -19
  113. package/dist/runLocalTestJob.d.ts.map +1 -1
  114. package/dist/runLocalTestJob.js +55 -29
  115. package/dist/runLocalTestJob.js.map +1 -1
  116. package/dist/setup.js +1 -1
  117. package/dist/setup.js.map +1 -1
  118. package/dist/sourceArchive.d.ts +1 -1
  119. package/dist/sourceArchive.d.ts.map +1 -1
  120. package/dist/sourceArchive.js +1 -0
  121. package/dist/sourceArchive.js.map +1 -1
  122. package/package.json +3 -4
  123. package/templates/typescript/init/package.json.hbs +1 -2
  124. package/templates/typescript/terse-transport.d.ts +25 -0
  125. package/templates/typescript/terse-transport.js +117 -0
  126. package/templates/typescript/tsconfig.json +13 -0
@@ -0,0 +1,117 @@
1
+ // Generated by terse actor generate; regenerate instead of editing.
2
+ // Terse connection defaults for this generated actor client.
3
+ import { ActorSessionRejectedError, SocketProxy as RuntimeSocketProxy, createActorSessionTransport, createActorTransport, createActorStub as createRuntimeActorStub } from "./runtime/index.js"
4
+
5
+ export * from "./runtime/index.js"
6
+
7
+ const DEFAULT_ACTOR_URL = "http://127.0.0.1:7100/v1/projects/local/actors"
8
+
9
+ let connection
10
+
11
+ const terseTransport = {
12
+ invoke(actorName, actorId, method, args) {
13
+ return currentTransport().invoke(actorName, actorId, method, args)
14
+ }
15
+ }
16
+
17
+ /** Use TERSE_ACTOR_URL and TERSE_API_KEY, with local development defaults. */
18
+ export function createActorStub(actorName, actorId, methods, transport = terseTransport) {
19
+ return createRuntimeActorStub(actorName, actorId, methods, transport)
20
+ }
21
+
22
+ /** Issue browser WebSocket grants through the selected Terse project. */
23
+ export class SocketProxy {
24
+ #actors
25
+ #options
26
+ #dependencies
27
+
28
+ constructor(actors, options = {}, dependencies = {}) {
29
+ this.#actors = actors
30
+ this.#options = options
31
+ this.#dependencies = dependencies
32
+ }
33
+
34
+ async handle(authorization) {
35
+ const { endpoint, apiKey } = connectionSettings()
36
+ const setupTimeoutMs = this.#options.setupTimeoutMs ?? configuredSetupTimeoutMs()
37
+ if (endpoint.kind === "runtime") {
38
+ const options = { projectId: endpoint.projectId, controlPlaneUrl: endpoint.origin, apiKey: apiKey || undefined, setupTimeoutMs }
39
+ return new RuntimeSocketProxy(this.#actors, options, this.#dependencies).handle(authorization)
40
+ }
41
+ const { actorName, actorId, metadata } = authorization
42
+ if (!Object.hasOwn(this.#actors, actorName)) throw new Error(`Unknown actor name: ${actorName}`)
43
+ const response = await (this.#dependencies.fetch ?? globalThis.fetch)(endpoint.actorUrl, {
44
+ method: "POST",
45
+ headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", Accept: "application/json" },
46
+ body: JSON.stringify({ actorName, actorId, metadata }),
47
+ signal: AbortSignal.timeout(setupTimeoutMs ?? 10_000),
48
+ redirect: "error"
49
+ })
50
+ if (!response.ok) {
51
+ await response.body?.cancel()
52
+ throw new Error(`Actor connection grant could not be issued (HTTP ${response.status})`)
53
+ }
54
+ return response.json()
55
+ }
56
+ }
57
+
58
+ function currentTransport() {
59
+ const { endpoint, apiKey } = connectionSettings()
60
+ const key = JSON.stringify([endpoint.actorUrl, apiKey])
61
+ if (connection?.key === key) return connection.transport
62
+ connection?.transport.dispose?.()
63
+ const transport =
64
+ endpoint.kind === "runtime"
65
+ ? createActorTransport({ projectId: endpoint.projectId, controlPlaneUrl: endpoint.origin, apiKey: apiKey || undefined })
66
+ : createActorSessionTransport({ projectId: endpoint.projectId, getSession: () => requestSession(endpoint, apiKey) })
67
+ connection = { key, transport }
68
+ return transport
69
+ }
70
+
71
+ async function requestSession(endpoint, apiKey) {
72
+ const response = await globalThis.fetch(`${endpoint.actorUrl}/access`, {
73
+ method: "POST",
74
+ headers: { Authorization: `Bearer ${apiKey}`, Accept: "application/json" },
75
+ redirect: "error",
76
+ signal: AbortSignal.timeout(10_000)
77
+ })
78
+ if (!response.ok) {
79
+ await response.body?.cancel()
80
+ const message = `Actor authorization failed (HTTP ${response.status})`
81
+ if (response.status === 401 || response.status === 403) throw new ActorSessionRejectedError(message)
82
+ throw new Error(message)
83
+ }
84
+ return response.json()
85
+ }
86
+
87
+ function connectionSettings() {
88
+ const environment = globalThis.process?.env ?? {}
89
+ const endpoint = parseActorEndpoint(environment.TERSE_ACTOR_URL?.trim() || DEFAULT_ACTOR_URL)
90
+ const apiKey = environment.TERSE_API_KEY?.trim() || ""
91
+ const local = endpoint.kind === "runtime" && ["localhost", "127.0.0.1", "[::1]"].includes(new URL(endpoint.origin).hostname)
92
+ if (!apiKey && !local) throw new Error("Missing TERSE_API_KEY. Set it in the .env of the project calling the actor.")
93
+ return { endpoint, apiKey }
94
+ }
95
+
96
+ /** The URL path selects the Terse API or the actor runtime explicitly. */
97
+ function parseActorEndpoint(value) {
98
+ const invalid = () => new Error("TERSE_ACTOR_URL must be an HTTP(S) URL ending in /<projectId>/actors or /v1/projects/<projectId>/actors, without credentials, query, or fragment")
99
+ const url = URL.canParse(value) ? new URL(value) : undefined
100
+ const match = url && /^\/(?:(sdk|v1)\/projects\/)?([^/]+)\/actors\/?$/.exec(url.pathname)
101
+ if (!match || !["http:", "https:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) throw invalid()
102
+ const projectId = decodeURIComponent(match[2])
103
+ if (!/^[A-Za-z0-9._-]{1,64}$/.test(projectId) || projectId === "." || projectId === "..") throw invalid()
104
+ const kind = match[1] === "v1" ? "runtime" : "terse"
105
+ const project = encodeURIComponent(projectId)
106
+ return { kind, origin: url.origin, projectId, actorUrl: `${url.origin}${kind === "runtime" ? `/v1/projects/${project}` : `/${project}`}/actors` }
107
+ }
108
+
109
+ function configuredSetupTimeoutMs() {
110
+ const configured = globalThis.process?.env?.TERSE_ACTOR_SETUP_TIMEOUT_MS?.trim()
111
+ if (!configured) return undefined
112
+ const value = Number(configured)
113
+ if (!Number.isSafeInteger(value) || value < 1_000 || value > 600_000) {
114
+ throw new Error("TERSE_ACTOR_SETUP_TIMEOUT_MS must be an integer between 1000 and 600000 milliseconds")
115
+ }
116
+ return value
117
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ // These templates import ./runtime/, which exists only beside generated output.
3
+ // actorGenerate.test.ts typechecks them there with skipLibCheck disabled.
4
+ "compilerOptions": {
5
+ "target": "ES2022",
6
+ "module": "ESNext",
7
+ "moduleResolution": "bundler",
8
+ "strict": true,
9
+ "noEmit": true,
10
+ "skipLibCheck": true
11
+ },
12
+ "include": ["*.d.ts"]
13
+ }