deepline 0.0.1 → 0.1.1

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 (100) hide show
  1. package/README.md +324 -0
  2. package/dist/cli/index.js +6750 -503
  3. package/dist/cli/index.js.map +1 -1
  4. package/dist/cli/index.mjs +6735 -512
  5. package/dist/cli/index.mjs.map +1 -1
  6. package/dist/index.d.mts +2349 -32
  7. package/dist/index.d.ts +2349 -32
  8. package/dist/index.js +1631 -82
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.mjs +1617 -83
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/repo/apps/play-runner-workers/src/coordinator-entry.ts +3256 -0
  13. package/dist/repo/apps/play-runner-workers/src/dedup-do.ts +710 -0
  14. package/dist/repo/apps/play-runner-workers/src/entry.ts +5070 -0
  15. package/dist/repo/apps/play-runner-workers/src/runtime/README.md +21 -0
  16. package/dist/repo/apps/play-runner-workers/src/runtime/batching.ts +177 -0
  17. package/dist/repo/apps/play-runner-workers/src/runtime/execution-plan.ts +52 -0
  18. package/dist/repo/apps/play-runner-workers/src/runtime/tool-batch.ts +100 -0
  19. package/dist/repo/apps/play-runner-workers/src/runtime/tool-result.ts +184 -0
  20. package/dist/repo/sdk/src/cli/commands/auth.ts +482 -0
  21. package/dist/repo/sdk/src/cli/commands/billing.ts +188 -0
  22. package/dist/repo/sdk/src/cli/commands/csv.ts +123 -0
  23. package/dist/repo/sdk/src/cli/commands/db.ts +119 -0
  24. package/dist/repo/sdk/src/cli/commands/feedback.ts +40 -0
  25. package/dist/repo/sdk/src/cli/commands/org.ts +117 -0
  26. package/dist/repo/sdk/src/cli/commands/play.ts +3200 -0
  27. package/dist/repo/sdk/src/cli/commands/tools.ts +687 -0
  28. package/dist/repo/sdk/src/cli/dataset-stats.ts +341 -0
  29. package/dist/repo/sdk/src/cli/index.ts +138 -0
  30. package/dist/repo/sdk/src/cli/progress.ts +135 -0
  31. package/dist/repo/sdk/src/cli/trace.ts +61 -0
  32. package/dist/repo/sdk/src/cli/utils.ts +145 -0
  33. package/dist/repo/sdk/src/client.ts +1188 -0
  34. package/dist/repo/sdk/src/compat.ts +77 -0
  35. package/dist/repo/sdk/src/config.ts +285 -0
  36. package/dist/repo/sdk/src/errors.ts +125 -0
  37. package/dist/repo/sdk/src/http.ts +391 -0
  38. package/dist/repo/sdk/src/index.ts +139 -0
  39. package/dist/repo/sdk/src/play.ts +1330 -0
  40. package/dist/repo/sdk/src/plays/bundle-play-file.ts +133 -0
  41. package/dist/repo/sdk/src/plays/harness-stub.ts +210 -0
  42. package/dist/repo/sdk/src/plays/local-file-discovery.ts +326 -0
  43. package/dist/repo/sdk/src/tool-output.ts +489 -0
  44. package/dist/repo/sdk/src/types.ts +669 -0
  45. package/dist/repo/sdk/src/version.ts +2 -0
  46. package/dist/repo/sdk/src/worker-play-entry.ts +286 -0
  47. package/dist/repo/shared_libs/observability/node-tracing.ts +129 -0
  48. package/dist/repo/shared_libs/observability/tracing.ts +98 -0
  49. package/dist/repo/shared_libs/play-runtime/backend.ts +139 -0
  50. package/dist/repo/shared_libs/play-runtime/batch-runtime.ts +182 -0
  51. package/dist/repo/shared_libs/play-runtime/batching-types.ts +91 -0
  52. package/dist/repo/shared_libs/play-runtime/context.ts +3999 -0
  53. package/dist/repo/shared_libs/play-runtime/coordinator-headers.ts +78 -0
  54. package/dist/repo/shared_libs/play-runtime/ctx-contract.ts +250 -0
  55. package/dist/repo/shared_libs/play-runtime/ctx-types.ts +713 -0
  56. package/dist/repo/shared_libs/play-runtime/dataset-id.ts +10 -0
  57. package/dist/repo/shared_libs/play-runtime/db-session-crypto.ts +304 -0
  58. package/dist/repo/shared_libs/play-runtime/db-session.ts +462 -0
  59. package/dist/repo/shared_libs/play-runtime/dedup-backend.ts +0 -0
  60. package/dist/repo/shared_libs/play-runtime/default-batch-strategies.ts +124 -0
  61. package/dist/repo/shared_libs/play-runtime/execution-plan.ts +262 -0
  62. package/dist/repo/shared_libs/play-runtime/live-events.ts +214 -0
  63. package/dist/repo/shared_libs/play-runtime/live-state-contract.ts +50 -0
  64. package/dist/repo/shared_libs/play-runtime/map-execution-frame.ts +114 -0
  65. package/dist/repo/shared_libs/play-runtime/map-row-identity.ts +158 -0
  66. package/dist/repo/shared_libs/play-runtime/profiles.ts +90 -0
  67. package/dist/repo/shared_libs/play-runtime/progress-emitter.ts +172 -0
  68. package/dist/repo/shared_libs/play-runtime/protocol.ts +121 -0
  69. package/dist/repo/shared_libs/play-runtime/public-play-contract.ts +42 -0
  70. package/dist/repo/shared_libs/play-runtime/result-normalization.ts +33 -0
  71. package/dist/repo/shared_libs/play-runtime/runtime-actions.ts +208 -0
  72. package/dist/repo/shared_libs/play-runtime/runtime-api.ts +1873 -0
  73. package/dist/repo/shared_libs/play-runtime/runtime-constraints.ts +2 -0
  74. package/dist/repo/shared_libs/play-runtime/runtime-pg-driver-neon-serverless.ts +201 -0
  75. package/dist/repo/shared_libs/play-runtime/runtime-pg-driver-pg.ts +48 -0
  76. package/dist/repo/shared_libs/play-runtime/runtime-pg-driver.ts +84 -0
  77. package/dist/repo/shared_libs/play-runtime/scheduler-backend.ts +174 -0
  78. package/dist/repo/shared_libs/play-runtime/static-pipeline-types.ts +147 -0
  79. package/dist/repo/shared_libs/play-runtime/suspension.ts +68 -0
  80. package/dist/repo/shared_libs/play-runtime/tool-batch-executor.ts +146 -0
  81. package/dist/repo/shared_libs/play-runtime/tool-result.ts +387 -0
  82. package/dist/repo/shared_libs/play-runtime/tracing.ts +31 -0
  83. package/dist/repo/shared_libs/play-runtime/waterfall-replay.ts +75 -0
  84. package/dist/repo/shared_libs/play-runtime/worker-api-types.ts +140 -0
  85. package/dist/repo/shared_libs/plays/artifact-transport.ts +14 -0
  86. package/dist/repo/shared_libs/plays/artifact-types.ts +49 -0
  87. package/dist/repo/shared_libs/plays/bundling/index.ts +1346 -0
  88. package/dist/repo/shared_libs/plays/compiler-manifest.ts +186 -0
  89. package/dist/repo/shared_libs/plays/contracts.ts +51 -0
  90. package/dist/repo/shared_libs/plays/dataset.ts +308 -0
  91. package/dist/repo/shared_libs/plays/definition.ts +264 -0
  92. package/dist/repo/shared_libs/plays/file-refs.ts +11 -0
  93. package/dist/repo/shared_libs/plays/rate-limit-scheduler.ts +206 -0
  94. package/dist/repo/shared_libs/plays/resolve-static-pipeline.ts +164 -0
  95. package/dist/repo/shared_libs/plays/row-identity.ts +302 -0
  96. package/dist/repo/shared_libs/plays/runtime-validation.ts +415 -0
  97. package/dist/repo/shared_libs/plays/static-pipeline.ts +560 -0
  98. package/dist/repo/shared_libs/temporal/constants.ts +39 -0
  99. package/dist/repo/shared_libs/temporal/preview-config.ts +153 -0
  100. package/package.json +14 -12
package/README.md ADDED
@@ -0,0 +1,324 @@
1
+ # Deepline V2 SDK + CLI
2
+
3
+ TypeScript SDK and CLI for Deepline plays, tools, and enrichment.
4
+
5
+ ## Dev workflow
6
+
7
+ ### Prerequisites
8
+
9
+ ```bash
10
+ bun run dev # starts Next.js + Convex + Temporal + worker
11
+ ```
12
+
13
+ Wait for health: `curl http://localhost:3000/api/v2/health`
14
+
15
+ ### Auth (one-time)
16
+
17
+ ```bash
18
+ bun sdk/bin/deepline-dev.ts auth register
19
+ ```
20
+
21
+ Opens browser for approval. Stores SDK CLI auth in `~/.local/deepline/<host-slug>/.env`.
22
+
23
+ Check status: `bun sdk/bin/deepline-dev.ts auth status --json`
24
+
25
+ ### Making changes
26
+
27
+ Edit any file in `sdk/src/` and re-run. No build step needed — `deepline-dev.ts` runs source directly via Bun.
28
+
29
+ ```bash
30
+ # Edit sdk/src/cli/commands/play.ts
31
+ # Then immediately:
32
+ bun sdk/bin/deepline-dev.ts play run --file my-play.play.ts --watch
33
+ ```
34
+
35
+ The root `package.json` has an alias: `bun run deepline` does the same thing.
36
+
37
+ ### When you need to build
38
+
39
+ Only two cases require `npm run sdk:build`:
40
+
41
+ 1. **`npx --package=./sdk -- deepline`** — the path used by integration tests
42
+ 2. **Testing the published artifact** — verifying `dist/cli/index.mjs` works
43
+
44
+ ```bash
45
+ cd sdk && bun run build # or: npm run sdk:build from root
46
+ ```
47
+
48
+ ## Env files
49
+
50
+ | File | Set by | Contains |
51
+ | ------------------------------------ | ------------------- | ----------------------------------------- |
52
+ | `~/.local/deepline/<host-slug>/.env` | SDK `auth register` | `DEEPLINE_ORIGIN_URL`, `DEEPLINE_API_KEY` |
53
+
54
+ `deepline-dev.ts` hard-sets `DEEPLINE_ORIGIN_URL=http://localhost:3000` so you never need to configure it manually for local dev.
55
+
56
+ Config resolution order: explicit options > env vars > checkout `.env.worktree` > SDK v2 `.env` > prod fallback.
57
+
58
+ ## CLI commands
59
+
60
+ ```bash
61
+ bun sdk/bin/deepline-dev.ts health
62
+ bun sdk/bin/deepline-dev.ts auth register|status
63
+ bun sdk/bin/deepline-dev.ts tools list|get|call
64
+ bun sdk/bin/deepline-dev.ts play run --file <file.play.ts> [--input '{}'] [--csv file.csv] [--watch]
65
+ bun sdk/bin/deepline-dev.ts play run --name <name> [--input '{}'] [--csv file.csv] [--watch]
66
+ bun sdk/bin/deepline-dev.ts play get <file.play.ts|name> [--json]
67
+ bun sdk/bin/deepline-dev.ts play versions --name <name> [--json]
68
+ bun sdk/bin/deepline-dev.ts play tail --run-id <run-id> [--json]
69
+ bun sdk/bin/deepline-dev.ts play tail --name <name> [--json]
70
+ bun sdk/bin/deepline-dev.ts play runs --name <name> [--json]
71
+ bun sdk/bin/deepline-dev.ts play publish <play-file.ts|name> [--latest|--revision-id <id>] [--json]
72
+ ```
73
+
74
+ ## Public play entrypoints
75
+
76
+ These are the public ways to run plays. They intentionally map to the external
77
+ surfaces we exercise in CI:
78
+
79
+ 1. CLI
80
+ - Repo-local Bun/source CLI: `bun sdk/bin/deepline-dev.ts play run ...`
81
+ - Repo script alias: `bun run deepline -- play run ...`
82
+ - Installed CLI: `deepline play run ...`
83
+ 2. Programmatic SDK convenience client
84
+ - `client.runPlay(...)`
85
+ 3. High-level SDK handles
86
+ - `ctx.play(name).run(...)`
87
+ - `ctx.play(name).runSync(...)`
88
+ - `ctx.runPlay(name, input)`
89
+ 4. Raw HTTP API
90
+ - `POST /api/v2/plays/run`
91
+ - `GET /api/v2/plays/run/:workflowId`
92
+ 5. In-play composition
93
+ - `ctx.runPlay(...)`
94
+
95
+ Internal / advanced only:
96
+
97
+ - `registerPlayArtifact()`
98
+ - `stagePlayFiles()`
99
+ - `startPlayRun()`
100
+ - `getPlayStatus()`
101
+
102
+ Those low-level primitives are still available for callers that explicitly need
103
+ to own the package-submit-poll flow, but they are not the primary public SDK
104
+ surface.
105
+
106
+ ## SDK usage (programmatic)
107
+
108
+ ```typescript
109
+ import { Deepline, definePlay } from 'deepline';
110
+
111
+ const ctx = await Deepline.connect();
112
+
113
+ // Tools
114
+ const tools = await ctx.tools.list();
115
+ const result = await ctx.tools.execute({
116
+ tool: 'test_company_search',
117
+ input: {
118
+ domain: 'stripe.com',
119
+ },
120
+ });
121
+ const company = result.value;
122
+
123
+ // Named play (already live)
124
+ const job = await ctx.play('my-play').run({ domain: 'example.com' });
125
+ const status = await job.status();
126
+ const output = await job.get(); // polls until complete
127
+
128
+ // Define + run a play
129
+ const myPlay = definePlay('my-play', async (ctx, input: { domain: string }) => {
130
+ const company = await ctx.tools.execute({
131
+ id: 'company_search',
132
+ tool: 'test_company_search',
133
+ input: { domain: input.domain },
134
+ description: 'Look up company details by domain.',
135
+ });
136
+ return { company: company.result };
137
+ });
138
+
139
+ const detail = await myPlay.get();
140
+ const result = await myPlay.runSync({ domain: 'example.com' });
141
+ ```
142
+
143
+ ## Testing
144
+
145
+ ```bash
146
+ # Unit tests (no server needed)
147
+ bun run vitest run tests/lib/plays tests/api/plays-get-route.test.ts
148
+
149
+ # SDK + CLI integration (needs running dev server)
150
+ bash scripts/test-sdk-play-cli.sh
151
+
152
+ # V2 plays e2e — all free test tools, real Temporal (needs running dev server)
153
+ bun tests/v2-plays/runner.ts --top 10
154
+ bun tests/v2-plays/runner.ts --shard 1/4
155
+ bun tests/v2-plays/runner.ts --file tests/v2-plays/plays/07-waterfall-basic.play.ts
156
+ ```
157
+
158
+ ## Repro: rate limit handling (end-to-end)
159
+
160
+ Full sequence from zero to hitting + surviving rate limits via v2 plays.
161
+
162
+ ### 1. Install + auth
163
+
164
+ ```bash
165
+ # Production
166
+ bash <(curl -sS https://code.deepline.com/api/v2/sdk/install)
167
+
168
+ # Local dev (no install needed)
169
+ bun run dev # start server
170
+ bun sdk/bin/deepline-dev.ts auth register
171
+ ```
172
+
173
+ ### 2. Write a play that hammers the rate-limited test tool
174
+
175
+ ```bash
176
+ cat > rate-limit-repro.play.ts << 'TS'
177
+ import { definePlay } from 'deepline';
178
+
179
+ // test_rate_limit has a 5 req/s shared window — this play fires 20 calls
180
+ // to force 429s and prove the runtime retries + backs off correctly.
181
+ export default definePlay('rate-limit-repro', async (ctx) => {
182
+ const items = Array.from({ length: 20 }, (_, i) => ({
183
+ key: `probe-${i}`,
184
+ row_number: i + 1,
185
+ }));
186
+
187
+ const results = await ctx.map('rate_limit_probes', items, { key: 'key' })
188
+ .step("result", (row, ctx) =>
189
+ ctx.tools.execute({
190
+ id: 'rate_limit_probe',
191
+ tool: 'test_rate_limit',
192
+ input: {
193
+ key: 'redis-shared-scenario',
194
+ row_number: row.row_number,
195
+ },
196
+ description: 'Exercise the shared rate-limit retry path.',
197
+ }))
198
+ .run();
199
+
200
+ return { total: results.length, results };
201
+ });
202
+ TS
203
+ ```
204
+
205
+ ### 3. Run it
206
+
207
+ ```bash
208
+ # Dev
209
+ bun sdk/bin/deepline-dev.ts play run --file rate-limit-repro.play.ts --watch
210
+
211
+ # Production
212
+ deepline play run --file rate-limit-repro.play.ts --watch
213
+ ```
214
+
215
+ ### 4. Fallback variant (multi-step with rate limits)
216
+
217
+ Use `steps()` and `when()` to express row-level fallback sequences. Skipped
218
+ conditional steps yield `null`.
219
+
220
+ ```bash
221
+ cat > rate-limit-waterfall.play.ts << 'TS'
222
+ import { definePlay, steps, when } from 'deepline';
223
+
224
+ export default definePlay('rate-limit-waterfall', async (ctx) => {
225
+ const leads = Array.from({ length: 24 }, (_, i) => ({
226
+ lead_id: `lead-${String(i + 1).padStart(3, '0')}`,
227
+ row_number: i + 1,
228
+ }));
229
+
230
+ const probeFallback = steps<{ lead_id: string; row_number: number }>()
231
+ .step("primary_probe", (row, ctx) =>
232
+ ctx.tool({
233
+ id: 'rate_limit_probe',
234
+ tool: 'test_rate_limit',
235
+ input: {
236
+ key: 'redis-shared-scenario',
237
+ lead_id: row.lead_id,
238
+ row_number: row.row_number,
239
+ },
240
+ description: 'Exercise primary rate-limit behavior.',
241
+ }))
242
+ .step("backup_probe", when(
243
+ (row) => !row.primary_probe,
244
+ (row, ctx) =>
245
+ ctx.tool({
246
+ id: 'rate_limit_probe_backup',
247
+ tool: 'test_rate_limit',
248
+ input: {
249
+ key: 'redis-shared-scenario',
250
+ lead_id: row.lead_id,
251
+ row_number: row.row_number,
252
+ },
253
+ description: 'Exercise fallback rate-limit behavior.',
254
+ }),
255
+ ))
256
+ .return((row) => row.primary_probe ?? row.backup_probe ?? null);
257
+
258
+ // Fan out over leads — each gets a row-level fallback sequence
259
+ const results = await ctx.map('leads', leads, { key: 'lead_id' })
260
+ .step("probe", probeFallback)
261
+ .run();
262
+
263
+ return results;
264
+ });
265
+ TS
266
+ ```
267
+
268
+ ```bash
269
+ bun sdk/bin/deepline-dev.ts play run --file rate-limit-waterfall.play.ts --watch
270
+ ```
271
+
272
+ ### What to expect
273
+
274
+ - `test_rate_limit` has a Redis-backed rate limit: **5 req/s, 3 max concurrency**.
275
+ - The SDK HTTP client auto-retries 429s with exponential backoff (1s → 2s → 4s, capped at 30s, up to 4 attempts).
276
+ - The play rate limit scheduler (`PlayRateLimitScheduler`) paces tool calls using `queue_hint` metadata from rate limit definitions.
277
+ - All 20/24 rows should complete — no terminal rate limit errors in the output.
278
+ - The waterfall variant proves that short-circuiting (skipping later steps when earlier steps match) works correctly under rate pressure.
279
+
280
+ ### V1 equivalent
281
+
282
+ The v1 repro scripts use `deepline enrich` (Python CLI) with `--with` JSON specs. The v2 plays replace this with typed TypeScript — same backend rate limit infrastructure, better DX.
283
+
284
+ ```bash
285
+ # V1 (still works)
286
+ scripts/repro-rate-limit-enrich.sh redis_429
287
+ scripts/repro-rate-limit-enrich.sh redis_429_waterfall
288
+ ```
289
+
290
+ ## Transpiling native plays
291
+
292
+ Convert V1 native `PlayDocument` definitions to V2 `definePlay()` files:
293
+
294
+ ```bash
295
+ bun scripts/transpile-native-plays.ts # all -> src/lib/plays/generated/v2/
296
+ bun scripts/transpile-native-plays.ts --play test_play # single play
297
+ bun scripts/transpile-native-plays.ts --stdout # print to stdout
298
+ ```
299
+
300
+ ## File structure
301
+
302
+ ```
303
+ sdk/
304
+ bin/deepline-dev.ts # Dev entry (bun, no build, points at localhost)
305
+ src/
306
+ index.ts # Public exports
307
+ config.ts # Config + env resolution
308
+ http.ts # Fetch client with retries
309
+ client.ts # DeeplineClient (tools, plays API)
310
+ play.ts # definePlay, Deepline.connect, job handles
311
+ types.ts # TypeScript interfaces
312
+ errors.ts # DeeplineError, AuthError, RateLimitError
313
+ cli/
314
+ index.ts # CLI router
315
+ commands/
316
+ auth.ts # auth register/status
317
+ play.ts # play run/get/tail/runs/publish
318
+ tools.ts # tools list/get/call
319
+ enrich.ts # CSV enrichment (delegates to Python CLI)
320
+ plays/
321
+ bundle-play-file.ts # esbuild bundler for .play.ts files
322
+ local-file-discovery.ts # ctx.csv() file resolution + packaging
323
+ dist/ # Build output (gitignored)
324
+ ```