atabey 0.1.1-beta → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -79
- package/dist/atabey/src/cli/commands/coverage.js +17 -12
- package/dist/atabey/src/cli/commands/coverage.js.map +1 -1
- package/dist/atabey/src/cli/commands/lint.d.ts +2 -1
- package/dist/atabey/src/cli/commands/lint.js +20 -1
- package/dist/atabey/src/cli/commands/lint.js.map +1 -1
- package/dist/atabey/src/cli/commands/mcp.js +56 -23
- package/dist/atabey/src/cli/commands/mcp.js.map +1 -1
- package/dist/atabey/src/cli/commands/orchestrate.d.ts +6 -6
- package/dist/atabey/src/cli/index.js +1 -1
- package/dist/shared/src/agent-identity.d.ts +1 -34
- package/dist/shared/src/agent-identity.js +1 -76
- package/dist/shared/src/agent-identity.js.map +1 -1
- package/dist/shared/src/api-catalog.d.ts +1 -57
- package/dist/shared/src/api-catalog.js +1 -253
- package/dist/shared/src/api-catalog.js.map +1 -1
- package/dist/shared/src/audit-chain.d.ts +1 -80
- package/dist/shared/src/audit-chain.js +1 -261
- package/dist/shared/src/audit-chain.js.map +1 -1
- package/dist/shared/src/audit.d.ts +1 -121
- package/dist/shared/src/audit.js +1 -321
- package/dist/shared/src/audit.js.map +1 -1
- package/dist/shared/src/constants.d.ts +1 -150
- package/dist/shared/src/constants.js +1 -211
- package/dist/shared/src/constants.js.map +1 -1
- package/dist/shared/src/database.d.ts +1 -6
- package/dist/shared/src/database.js +1 -15
- package/dist/shared/src/database.js.map +1 -1
- package/dist/shared/src/errors.d.ts +1 -46
- package/dist/shared/src/errors.js +1 -68
- package/dist/shared/src/errors.js.map +1 -1
- package/dist/shared/src/file-lock.d.ts +1 -93
- package/dist/shared/src/file-lock.js +1 -239
- package/dist/shared/src/file-lock.js.map +1 -1
- package/dist/shared/src/fs.d.ts +1 -9
- package/dist/shared/src/fs.js +1 -53
- package/dist/shared/src/fs.js.map +1 -1
- package/dist/shared/src/index.d.ts +2 -31
- package/dist/shared/src/index.js +2 -31
- package/dist/shared/src/index.js.map +1 -1
- package/dist/shared/src/job-queue.d.ts +1 -76
- package/dist/shared/src/job-queue.js +1 -199
- package/dist/shared/src/job-queue.js.map +1 -1
- package/dist/shared/src/lock.d.ts +1 -82
- package/dist/shared/src/lock.js +1 -158
- package/dist/shared/src/lock.js.map +1 -1
- package/dist/shared/src/logger.d.ts +1 -31
- package/dist/shared/src/logger.js +1 -152
- package/dist/shared/src/logger.js.map +1 -1
- package/dist/shared/src/memory-prune.d.ts +1 -18
- package/dist/shared/src/memory-prune.js +1 -47
- package/dist/shared/src/memory-prune.js.map +1 -1
- package/dist/shared/src/migrations.d.ts +1 -69
- package/dist/shared/src/migrations.js +1 -393
- package/dist/shared/src/migrations.js.map +1 -1
- package/dist/shared/src/modules/agent/agent-identity.d.ts +34 -0
- package/dist/shared/src/modules/agent/agent-identity.js +77 -0
- package/dist/shared/src/modules/agent/agent-identity.js.map +1 -0
- package/dist/shared/src/modules/agent/api-catalog.d.ts +57 -0
- package/dist/shared/src/modules/agent/api-catalog.js +254 -0
- package/dist/shared/src/modules/agent/api-catalog.js.map +1 -0
- package/dist/shared/src/modules/agent/index.d.ts +3 -0
- package/dist/shared/src/modules/agent/index.js +4 -0
- package/dist/shared/src/modules/agent/index.js.map +1 -0
- package/dist/shared/src/modules/agent/tokenizer.d.ts +54 -0
- package/dist/shared/src/modules/agent/tokenizer.js +272 -0
- package/dist/shared/src/modules/agent/tokenizer.js.map +1 -0
- package/dist/shared/src/modules/concurrency/file-lock.d.ts +93 -0
- package/dist/shared/src/modules/concurrency/file-lock.js +240 -0
- package/dist/shared/src/modules/concurrency/file-lock.js.map +1 -0
- package/dist/shared/src/modules/concurrency/index.d.ts +6 -0
- package/dist/shared/src/modules/concurrency/index.js +7 -0
- package/dist/shared/src/modules/concurrency/index.js.map +1 -0
- package/dist/shared/src/modules/concurrency/job-queue.d.ts +76 -0
- package/dist/shared/src/modules/concurrency/job-queue.js +200 -0
- package/dist/shared/src/modules/concurrency/job-queue.js.map +1 -0
- package/dist/shared/src/modules/concurrency/lock.d.ts +82 -0
- package/dist/shared/src/modules/concurrency/lock.js +159 -0
- package/dist/shared/src/modules/concurrency/lock.js.map +1 -0
- package/dist/shared/src/modules/concurrency/resilience.d.ts +84 -0
- package/dist/shared/src/modules/concurrency/resilience.js +217 -0
- package/dist/shared/src/modules/concurrency/resilience.js.map +1 -0
- package/dist/shared/src/modules/concurrency/worker-pool.d.ts +52 -0
- package/dist/shared/src/modules/concurrency/worker-pool.js +229 -0
- package/dist/shared/src/modules/concurrency/worker-pool.js.map +1 -0
- package/dist/shared/src/modules/concurrency/worker-thread.d.ts +21 -0
- package/dist/shared/src/modules/concurrency/worker-thread.js +67 -0
- package/dist/shared/src/modules/concurrency/worker-thread.js.map +1 -0
- package/dist/shared/src/modules/index.d.ts +14 -0
- package/dist/shared/src/modules/index.js +15 -0
- package/dist/shared/src/modules/index.js.map +1 -0
- package/dist/shared/src/modules/observability/audit-chain.d.ts +80 -0
- package/dist/shared/src/modules/observability/audit-chain.js +262 -0
- package/dist/shared/src/modules/observability/audit-chain.js.map +1 -0
- package/dist/shared/src/modules/observability/audit.d.ts +121 -0
- package/dist/shared/src/modules/observability/audit.js +322 -0
- package/dist/shared/src/modules/observability/audit.js.map +1 -0
- package/dist/shared/src/modules/observability/index.d.ts +4 -0
- package/dist/shared/src/modules/observability/index.js +5 -0
- package/dist/shared/src/modules/observability/index.js.map +1 -0
- package/dist/shared/src/modules/observability/logger.d.ts +31 -0
- package/dist/shared/src/modules/observability/logger.js +153 -0
- package/dist/shared/src/modules/observability/logger.js.map +1 -0
- package/dist/shared/src/modules/observability/otel.d.ts +39 -0
- package/dist/shared/src/modules/observability/otel.js +93 -0
- package/dist/shared/src/modules/observability/otel.js.map +1 -0
- package/dist/shared/src/modules/primitives/constants.d.ts +150 -0
- package/dist/shared/src/modules/primitives/constants.js +212 -0
- package/dist/shared/src/modules/primitives/constants.js.map +1 -0
- package/dist/shared/src/modules/primitives/errors.d.ts +46 -0
- package/dist/shared/src/modules/primitives/errors.js +69 -0
- package/dist/shared/src/modules/primitives/errors.js.map +1 -0
- package/dist/shared/src/modules/primitives/fs.d.ts +9 -0
- package/dist/shared/src/modules/primitives/fs.js +54 -0
- package/dist/shared/src/modules/primitives/fs.js.map +1 -0
- package/dist/shared/src/modules/primitives/index.d.ts +7 -0
- package/dist/shared/src/modules/primitives/index.js +8 -0
- package/dist/shared/src/modules/primitives/index.js.map +1 -0
- package/dist/shared/src/modules/primitives/outbound-http.d.ts +32 -0
- package/dist/shared/src/modules/primitives/outbound-http.js +88 -0
- package/dist/shared/src/modules/primitives/outbound-http.js.map +1 -0
- package/dist/shared/src/modules/primitives/string.d.ts +1 -0
- package/dist/shared/src/modules/primitives/string.js +30 -0
- package/dist/shared/src/modules/primitives/string.js.map +1 -0
- package/dist/shared/src/modules/primitives/time.d.ts +5 -0
- package/dist/shared/src/modules/primitives/time.js +35 -0
- package/dist/shared/src/modules/primitives/time.js.map +1 -0
- package/dist/shared/src/modules/primitives/types.d.ts +27 -0
- package/dist/shared/src/modules/primitives/types.js +17 -0
- package/dist/shared/src/modules/primitives/types.js.map +1 -0
- package/dist/shared/src/modules/sandbox/index.d.ts +3 -0
- package/dist/shared/src/modules/sandbox/index.js +4 -0
- package/dist/shared/src/modules/sandbox/index.js.map +1 -0
- package/dist/shared/src/modules/sandbox/sandbox-fs.d.ts +28 -0
- package/dist/shared/src/modules/sandbox/sandbox-fs.js +143 -0
- package/dist/shared/src/modules/sandbox/sandbox-fs.js.map +1 -0
- package/dist/shared/src/modules/sandbox/sandbox-runtime.d.ts +78 -0
- package/dist/shared/src/modules/sandbox/sandbox-runtime.js +282 -0
- package/dist/shared/src/modules/sandbox/sandbox-runtime.js.map +1 -0
- package/dist/shared/src/modules/sandbox/sandbox.d.ts +54 -0
- package/dist/shared/src/modules/sandbox/sandbox.js +179 -0
- package/dist/shared/src/modules/sandbox/sandbox.js.map +1 -0
- package/dist/shared/src/modules/security/index.d.ts +5 -0
- package/dist/shared/src/modules/security/index.js +6 -0
- package/dist/shared/src/modules/security/index.js.map +1 -0
- package/dist/shared/src/modules/security/pii.d.ts +70 -0
- package/dist/shared/src/modules/security/pii.js +276 -0
- package/dist/shared/src/modules/security/pii.js.map +1 -0
- package/dist/shared/src/modules/security/policy-gate.d.ts +42 -0
- package/dist/shared/src/modules/security/policy-gate.js +172 -0
- package/dist/shared/src/modules/security/policy-gate.js.map +1 -0
- package/dist/shared/src/modules/security/retention.d.ts +68 -0
- package/dist/shared/src/modules/security/retention.js +218 -0
- package/dist/shared/src/modules/security/retention.js.map +1 -0
- package/dist/shared/src/modules/security/secrets.d.ts +99 -0
- package/dist/shared/src/modules/security/secrets.js +371 -0
- package/dist/shared/src/modules/security/secrets.js.map +1 -0
- package/dist/shared/src/modules/security/tls.d.ts +87 -0
- package/dist/shared/src/modules/security/tls.js +154 -0
- package/dist/shared/src/modules/security/tls.js.map +1 -0
- package/dist/shared/src/modules/storage/database.d.ts +6 -0
- package/dist/shared/src/modules/storage/database.js +16 -0
- package/dist/shared/src/modules/storage/database.js.map +1 -0
- package/dist/shared/src/modules/storage/index.d.ts +4 -0
- package/dist/shared/src/modules/storage/index.js +5 -0
- package/dist/shared/src/modules/storage/index.js.map +1 -0
- package/dist/shared/src/modules/storage/memory-prune.d.ts +18 -0
- package/dist/shared/src/modules/storage/memory-prune.js +48 -0
- package/dist/shared/src/modules/storage/memory-prune.js.map +1 -0
- package/dist/shared/src/modules/storage/migrations.d.ts +69 -0
- package/dist/shared/src/modules/storage/migrations.js +394 -0
- package/dist/shared/src/modules/storage/migrations.js.map +1 -0
- package/dist/shared/src/modules/storage/storage.d.ts +341 -0
- package/dist/shared/src/modules/storage/storage.js +1004 -0
- package/dist/shared/src/modules/storage/storage.js.map +1 -0
- package/dist/shared/src/otel.d.ts +1 -39
- package/dist/shared/src/otel.js +1 -92
- package/dist/shared/src/otel.js.map +1 -1
- package/dist/shared/src/outbound-http.d.ts +1 -32
- package/dist/shared/src/outbound-http.js +1 -87
- package/dist/shared/src/outbound-http.js.map +1 -1
- package/dist/shared/src/pii.d.ts +1 -70
- package/dist/shared/src/pii.js +1 -275
- package/dist/shared/src/pii.js.map +1 -1
- package/dist/shared/src/policy-gate.d.ts +1 -42
- package/dist/shared/src/policy-gate.js +1 -171
- package/dist/shared/src/policy-gate.js.map +1 -1
- package/dist/shared/src/resilience.d.ts +1 -84
- package/dist/shared/src/resilience.js +1 -216
- package/dist/shared/src/resilience.js.map +1 -1
- package/dist/shared/src/retention.d.ts +1 -68
- package/dist/shared/src/retention.js +1 -217
- package/dist/shared/src/retention.js.map +1 -1
- package/dist/shared/src/sandbox-fs.d.ts +1 -28
- package/dist/shared/src/sandbox-fs.js +1 -142
- package/dist/shared/src/sandbox-fs.js.map +1 -1
- package/dist/shared/src/sandbox-runtime.d.ts +1 -78
- package/dist/shared/src/sandbox-runtime.js +1 -281
- package/dist/shared/src/sandbox-runtime.js.map +1 -1
- package/dist/shared/src/sandbox.d.ts +1 -54
- package/dist/shared/src/sandbox.js +1 -178
- package/dist/shared/src/sandbox.js.map +1 -1
- package/dist/shared/src/secrets.d.ts +1 -99
- package/dist/shared/src/secrets.js +1 -370
- package/dist/shared/src/secrets.js.map +1 -1
- package/dist/shared/src/storage.d.ts +1 -341
- package/dist/shared/src/storage.js +1 -1003
- package/dist/shared/src/storage.js.map +1 -1
- package/dist/shared/src/string.d.ts +1 -1
- package/dist/shared/src/string.js +1 -29
- package/dist/shared/src/string.js.map +1 -1
- package/dist/shared/src/time.d.ts +1 -5
- package/dist/shared/src/time.js +1 -34
- package/dist/shared/src/time.js.map +1 -1
- package/dist/shared/src/tls.d.ts +1 -87
- package/dist/shared/src/tls.js +1 -153
- package/dist/shared/src/tls.js.map +1 -1
- package/dist/shared/src/tokenizer.d.ts +1 -54
- package/dist/shared/src/tokenizer.js +1 -271
- package/dist/shared/src/tokenizer.js.map +1 -1
- package/dist/shared/src/types.d.ts +1 -27
- package/dist/shared/src/types.js +1 -16
- package/dist/shared/src/types.js.map +1 -1
- package/dist/shared/src/worker-pool.d.ts +1 -52
- package/dist/shared/src/worker-pool.js +1 -228
- package/dist/shared/src/worker-pool.js.map +1 -1
- package/dist/shared/src/worker-thread.d.ts +1 -21
- package/dist/shared/src/worker-thread.js +1 -66
- package/dist/shared/src/worker-thread.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,272 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* Token Counter & FinOps Pricing Catalog (Phase 1.5)
|
|
3
|
-
*
|
|
4
|
-
* Uses the REAL `cl100k_base` BPE tokenizer via `js-tiktoken` (lite build +
|
|
5
|
-
* cl100k ranks ≈ ±2% vs provider tokenizers). If the BPE table cannot be
|
|
6
|
-
* loaded, it falls back to a cl100k-style pre-tokenization heuristic.
|
|
7
|
-
*
|
|
8
|
-
* Model-specific tokenizers (o200k_base, Claude, Gemini) are not applied yet;
|
|
9
|
-
* `_model` is accepted for API compatibility. Use counts for FinOps awareness
|
|
10
|
-
* and budget guardrails.
|
|
11
|
-
*/
|
|
12
|
-
import { Tiktoken } from "js-tiktoken/lite";
|
|
13
|
-
import cl100kRanks from "js-tiktoken/ranks/cl100k_base";
|
|
14
|
-
/**
|
|
15
|
-
* Up-to-date model pricing catalog (USD per 1M tokens).
|
|
16
|
-
*/
|
|
17
|
-
export const MODEL_PRICING_CATALOG = {
|
|
18
|
-
"claude-sonnet-4": {
|
|
19
|
-
provider: "anthropic",
|
|
20
|
-
model: "claude-sonnet-4",
|
|
21
|
-
inputPer1MUsd: 3.0,
|
|
22
|
-
outputPer1MUsd: 15.0,
|
|
23
|
-
tokenizer: "claude_bpe",
|
|
24
|
-
},
|
|
25
|
-
"claude-opus-4": {
|
|
26
|
-
provider: "anthropic",
|
|
27
|
-
model: "claude-opus-4",
|
|
28
|
-
inputPer1MUsd: 15.0,
|
|
29
|
-
outputPer1MUsd: 75.0,
|
|
30
|
-
tokenizer: "claude_bpe",
|
|
31
|
-
},
|
|
32
|
-
"claude-3-7-sonnet": {
|
|
33
|
-
provider: "anthropic",
|
|
34
|
-
model: "claude-3-7-sonnet",
|
|
35
|
-
inputPer1MUsd: 3.0,
|
|
36
|
-
outputPer1MUsd: 15.0,
|
|
37
|
-
tokenizer: "claude_bpe",
|
|
38
|
-
},
|
|
39
|
-
"claude-3-5-sonnet": {
|
|
40
|
-
provider: "anthropic",
|
|
41
|
-
model: "claude-3-5-sonnet",
|
|
42
|
-
inputPer1MUsd: 3.0,
|
|
43
|
-
outputPer1MUsd: 15.0,
|
|
44
|
-
tokenizer: "claude_bpe",
|
|
45
|
-
},
|
|
46
|
-
"claude-3-5-haiku": {
|
|
47
|
-
provider: "anthropic",
|
|
48
|
-
model: "claude-3-5-haiku",
|
|
49
|
-
inputPer1MUsd: 0.8,
|
|
50
|
-
outputPer1MUsd: 4.0,
|
|
51
|
-
tokenizer: "claude_bpe",
|
|
52
|
-
},
|
|
53
|
-
"gpt-4o": {
|
|
54
|
-
provider: "openai",
|
|
55
|
-
model: "gpt-4o",
|
|
56
|
-
inputPer1MUsd: 2.5,
|
|
57
|
-
outputPer1MUsd: 10.0,
|
|
58
|
-
tokenizer: "o200k_base",
|
|
59
|
-
},
|
|
60
|
-
"gpt-4o-mini": {
|
|
61
|
-
provider: "openai",
|
|
62
|
-
model: "gpt-4o-mini",
|
|
63
|
-
inputPer1MUsd: 0.15,
|
|
64
|
-
outputPer1MUsd: 0.6,
|
|
65
|
-
tokenizer: "o200k_base",
|
|
66
|
-
},
|
|
67
|
-
"gpt-4.1": {
|
|
68
|
-
provider: "openai",
|
|
69
|
-
model: "gpt-4.1",
|
|
70
|
-
inputPer1MUsd: 2.0,
|
|
71
|
-
outputPer1MUsd: 8.0,
|
|
72
|
-
tokenizer: "o200k_base",
|
|
73
|
-
},
|
|
74
|
-
"o3-mini": {
|
|
75
|
-
provider: "openai",
|
|
76
|
-
model: "o3-mini",
|
|
77
|
-
inputPer1MUsd: 1.1,
|
|
78
|
-
outputPer1MUsd: 4.4,
|
|
79
|
-
tokenizer: "o200k_base",
|
|
80
|
-
},
|
|
81
|
-
"gemini-2.5-pro": {
|
|
82
|
-
provider: "google",
|
|
83
|
-
model: "gemini-2.5-pro",
|
|
84
|
-
inputPer1MUsd: 1.25,
|
|
85
|
-
outputPer1MUsd: 10.0,
|
|
86
|
-
tokenizer: "gemini_spm",
|
|
87
|
-
},
|
|
88
|
-
"gemini-2.5-flash": {
|
|
89
|
-
provider: "google",
|
|
90
|
-
model: "gemini-2.5-flash",
|
|
91
|
-
inputPer1MUsd: 0.15,
|
|
92
|
-
outputPer1MUsd: 0.6,
|
|
93
|
-
tokenizer: "gemini_spm",
|
|
94
|
-
},
|
|
95
|
-
"deepseek-chat": {
|
|
96
|
-
provider: "deepseek",
|
|
97
|
-
model: "deepseek-chat",
|
|
98
|
-
inputPer1MUsd: 0.27,
|
|
99
|
-
outputPer1MUsd: 1.1,
|
|
100
|
-
tokenizer: "cl100k_base",
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
/**
|
|
104
|
-
* Official cl100k_base pre-tokenization pattern:
|
|
105
|
-
* - English contractions ('s, 'd, 'm, 't, 'll, 've, 're)
|
|
106
|
-
* - Optional space + Unicode letters
|
|
107
|
-
* - Optional space + 1..3 Unicode digits (numbers split every 3 digits in cl100k)
|
|
108
|
-
* - Optional space + non-whitespace/non-alphanumeric punctuation
|
|
109
|
-
* - Newlines / trailing whitespace runs
|
|
110
|
-
*/
|
|
111
|
-
const CL100K_PRETOKEN_REGEX = /'(?:[sdmt]|ll|ve|re)| ?\p{L}+| ?\p{N}{1,3}| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu;
|
|
112
|
-
/**
|
|
113
|
-
* Common high-frequency programming/English tokens that map 1:1 to a single BPE token
|
|
114
|
-
* in cl100k_base / o200k_base.
|
|
115
|
-
*/
|
|
116
|
-
const SINGLE_BPE_WORDS = new Set([
|
|
117
|
-
"function", "return", "const", "let", "var", "import", "export", "from", "async", "await",
|
|
118
|
-
"interface", "type", "class", "public", "private", "protected", "static", "readonly",
|
|
119
|
-
"string", "number", "boolean", "object", "undefined", "null", "true", "false", "void",
|
|
120
|
-
"promise", "record", "array", "error", "default", "switch", "case", "break", "continue",
|
|
121
|
-
"throw", "catch", "finally", "typeof", "instanceof", "extends", "implements", "constructor",
|
|
122
|
-
"package", "module", "require", "process", "console", "Buffer", "Math", "JSON", "Date",
|
|
123
|
-
"SELECT", "INSERT", "UPDATE", "DELETE", "FROM", "WHERE", "CREATE", "TABLE", "INDEX",
|
|
124
|
-
]);
|
|
125
|
-
/**
|
|
126
|
-
* Estimate how many BPE subword tokens a single pre-token chunk splits into.
|
|
127
|
-
*/
|
|
128
|
-
function countChunkBpeTokens(chunk) {
|
|
129
|
-
const trimmed = chunk.trimStart();
|
|
130
|
-
if (!trimmed) {
|
|
131
|
-
// Pure whitespace run: 1 token per ~4 spaces or per newline group
|
|
132
|
-
return Math.max(1, Math.ceil(chunk.length / 4));
|
|
133
|
-
}
|
|
134
|
-
// 1-3 digit numbers are guaranteed 1 token by CL100K_PRETOKEN_REGEX
|
|
135
|
-
if (/^\p{N}{1,3}$/u.test(trimmed)) {
|
|
136
|
-
return 1;
|
|
137
|
-
}
|
|
138
|
-
// Punctuation runs: common 1-3 char operators (===, !==, =>, ->, ::, ..., etc.) are 1 token
|
|
139
|
-
if (/^[^\s\p{L}\p{N}]+$/u.test(trimmed)) {
|
|
140
|
-
if (trimmed.length <= 3)
|
|
141
|
-
return 1;
|
|
142
|
-
return Math.ceil(trimmed.length / 2.5);
|
|
143
|
-
}
|
|
144
|
-
// ASCII word/identifier
|
|
145
|
-
if (/^[A-Za-z]+$/.test(trimmed)) {
|
|
146
|
-
if (trimmed.length <= 6 || SINGLE_BPE_WORDS.has(trimmed) || SINGLE_BPE_WORDS.has(trimmed.toLowerCase())) {
|
|
147
|
-
return 1;
|
|
148
|
-
}
|
|
149
|
-
// Split camelCase / PascalCase subwords first
|
|
150
|
-
const camelParts = trimmed.split(/(?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z])/);
|
|
151
|
-
if (camelParts.length > 1) {
|
|
152
|
-
let subTokens = 0;
|
|
153
|
-
for (const part of camelParts) {
|
|
154
|
-
subTokens += part.length <= 6 ? 1 : Math.ceil(part.length / 4.5);
|
|
155
|
-
}
|
|
156
|
-
return subTokens;
|
|
157
|
-
}
|
|
158
|
-
// Longer natural word: BPE averages ~4.5 chars per subword token
|
|
159
|
-
return Math.ceil(trimmed.length / 4.5);
|
|
160
|
-
}
|
|
161
|
-
// Non-ASCII Unicode (Turkish diacritics, CJK, emoji, etc.) — count UTF-8 byte density
|
|
162
|
-
const byteLength = Buffer.byteLength(trimmed, "utf8");
|
|
163
|
-
// In BPE, 2-byte UTF-8 chars (e.g. ş, ğ, ı, ö, ü, ç) take ~1 token per 2.8 bytes
|
|
164
|
-
return Math.max(1, Math.ceil(byteLength / 2.8));
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Estimate BPE tokens in a string using cl100k-style pre-tokenization.
|
|
168
|
-
* NOTE: approximate — see file header. `_model` is accepted for API
|
|
169
|
-
* compatibility but per-model tokenizer differences are not yet applied.
|
|
170
|
-
*/
|
|
171
|
-
/** Heuristic fallback used only if the real BPE encoder cannot be initialised. */
|
|
172
|
-
function approximateCountTokens(text) {
|
|
173
|
-
const matches = text.match(CL100K_PRETOKEN_REGEX);
|
|
174
|
-
if (!matches)
|
|
175
|
-
return 0;
|
|
176
|
-
let total = 0;
|
|
177
|
-
for (const chunk of matches) {
|
|
178
|
-
total += countChunkBpeTokens(chunk);
|
|
179
|
-
}
|
|
180
|
-
return total;
|
|
181
|
-
}
|
|
182
|
-
/** Lazily-initialised real cl100k_base BPE encoder (js-tiktoken, lite build). */
|
|
183
|
-
let realEncoder = null;
|
|
184
|
-
let realEncoderFailed = false;
|
|
185
|
-
function getRealEncoder() {
|
|
186
|
-
if (realEncoder)
|
|
187
|
-
return realEncoder;
|
|
188
|
-
if (realEncoderFailed)
|
|
189
|
-
return null;
|
|
190
|
-
try {
|
|
191
|
-
realEncoder = new Tiktoken(cl100kRanks);
|
|
192
|
-
return realEncoder;
|
|
193
|
-
}
|
|
194
|
-
catch {
|
|
195
|
-
realEncoderFailed = true;
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Count BPE tokens in a string.
|
|
201
|
-
*
|
|
202
|
-
* Uses the REAL `cl100k_base` tokenizer (js-tiktoken) for ~±2% accuracy.
|
|
203
|
-
* Falls back to a cl100k-style heuristic only if the BPE table cannot load.
|
|
204
|
-
*/
|
|
205
|
-
export function countTokens(text, _model) {
|
|
206
|
-
if (!text)
|
|
207
|
-
return 0;
|
|
208
|
-
const encoder = getRealEncoder();
|
|
209
|
-
if (encoder) {
|
|
210
|
-
try {
|
|
211
|
-
return encoder.encode(text).length;
|
|
212
|
-
}
|
|
213
|
-
catch {
|
|
214
|
-
/* fall through to heuristic */
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return approximateCountTokens(text);
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Resolve pricing for a given model name (supports partial matches and env overrides).
|
|
221
|
-
*/
|
|
222
|
-
export function resolveModelPricing(model) {
|
|
223
|
-
const requested = (model || process.env.ATABEY_MODEL || "claude-sonnet-4").toLowerCase().trim();
|
|
224
|
-
if (MODEL_PRICING_CATALOG[requested]) {
|
|
225
|
-
return MODEL_PRICING_CATALOG[requested];
|
|
226
|
-
}
|
|
227
|
-
for (const [key, pricing] of Object.entries(MODEL_PRICING_CATALOG)) {
|
|
228
|
-
if (requested.includes(key) || key.includes(requested)) {
|
|
229
|
-
return pricing;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
const envPer1k = parseFloat(process.env.ATABEY_COST_PER_1K_TOKENS_USD || "");
|
|
233
|
-
if (Number.isFinite(envPer1k) && envPer1k > 0) {
|
|
234
|
-
return {
|
|
235
|
-
provider: "generic",
|
|
236
|
-
model: requested || "custom",
|
|
237
|
-
inputPer1MUsd: envPer1k * 1000,
|
|
238
|
-
outputPer1MUsd: envPer1k * 1000,
|
|
239
|
-
tokenizer: "cl100k_base",
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
return MODEL_PRICING_CATALOG["claude-sonnet-4"];
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Calculate USD cost from separate input and output token counts.
|
|
246
|
-
*/
|
|
247
|
-
export function calculateModelCost(inputTokens, outputTokens, model) {
|
|
248
|
-
const pricing = resolveModelPricing(model);
|
|
249
|
-
const inputCost = (Math.max(0, inputTokens) / 1_000_000) * pricing.inputPer1MUsd;
|
|
250
|
-
const outputCost = (Math.max(0, outputTokens) / 1_000_000) * pricing.outputPer1MUsd;
|
|
251
|
-
const costUsd = Number((inputCost + outputCost).toFixed(8));
|
|
252
|
-
return { costUsd, pricing };
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Compute full input/output token and FinOps cost breakdown for an MCP tool invocation.
|
|
256
|
-
*/
|
|
257
|
-
export function measureToolCallUsage(toolName, args, resultText, model) {
|
|
258
|
-
const argsSerialized = typeof args === "string" ? args : JSON.stringify(args ?? {});
|
|
259
|
-
// Include MCP tool framing overhead (+8 tokens for tool call envelope)
|
|
260
|
-
const inputTokens = countTokens(toolName, model) + countTokens(argsSerialized, model) + 8;
|
|
261
|
-
const outputTokens = countTokens(resultText || "", model);
|
|
262
|
-
const { costUsd, pricing } = calculateModelCost(inputTokens, outputTokens, model);
|
|
263
|
-
return {
|
|
264
|
-
inputTokens,
|
|
265
|
-
outputTokens,
|
|
266
|
-
totalTokens: inputTokens + outputTokens,
|
|
267
|
-
estimatedCostUsd: costUsd,
|
|
268
|
-
model: pricing.model,
|
|
269
|
-
provider: pricing.provider,
|
|
270
|
-
};
|
|
271
|
-
}
|
|
1
|
+
export * from "./modules/agent/tokenizer.js";
|
|
272
2
|
//# sourceMappingURL=tokenizer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokenizer.js","sourceRoot":"","sources":["../../../../shared/src/tokenizer.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"tokenizer.js","sourceRoot":"","sources":["../../../../shared/src/tokenizer.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC"}
|
|
@@ -1,27 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Atabey Framework — Internal Branded Types
|
|
3
|
-
* Used to enforce absolute type safety within the core orchestration logic.
|
|
4
|
-
*/
|
|
5
|
-
export type Brand<K, T> = K & {
|
|
6
|
-
__brand: T;
|
|
7
|
-
};
|
|
8
|
-
export type TraceID = Brand<string, "TraceID">;
|
|
9
|
-
export type AgentID = Brand<string, "AgentID">;
|
|
10
|
-
export type PhaseID = Brand<string, "PhaseID">;
|
|
11
|
-
export type ProjectPath = Brand<string, "ProjectPath">;
|
|
12
|
-
export type MessageID = Brand<number, "MessageID">;
|
|
13
|
-
export type TaskID = Brand<string, "TaskID">;
|
|
14
|
-
export type LogID = Brand<number, "LogID">;
|
|
15
|
-
export type PlanID = Brand<string, "PlanID">;
|
|
16
|
-
/**
|
|
17
|
-
* Casts a raw string to a Branded Type.
|
|
18
|
-
* Use this only at the boundaries of the system.
|
|
19
|
-
*/
|
|
20
|
-
export declare function asTraceID(val: string): TraceID;
|
|
21
|
-
export declare function asAgentID(val: string): AgentID;
|
|
22
|
-
export declare function asPhaseID(val: string): PhaseID;
|
|
23
|
-
export declare function asProjectPath(val: string): ProjectPath;
|
|
24
|
-
export declare function asMessageID(val: number): MessageID;
|
|
25
|
-
export declare function asTaskID(val: string): TaskID;
|
|
26
|
-
export declare function asLogID(val: number): LogID;
|
|
27
|
-
export declare function asPlanID(val: string): PlanID;
|
|
1
|
+
export * from "./modules/primitives/types.js";
|
package/dist/shared/src/types.js
CHANGED
|
@@ -1,17 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* Atabey Framework — Internal Branded Types
|
|
3
|
-
* Used to enforce absolute type safety within the core orchestration logic.
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Casts a raw string to a Branded Type.
|
|
7
|
-
* Use this only at the boundaries of the system.
|
|
8
|
-
*/
|
|
9
|
-
export function asTraceID(val) { return val; }
|
|
10
|
-
export function asAgentID(val) { return val; }
|
|
11
|
-
export function asPhaseID(val) { return val; }
|
|
12
|
-
export function asProjectPath(val) { return val; }
|
|
13
|
-
export function asMessageID(val) { return val; }
|
|
14
|
-
export function asTaskID(val) { return val; }
|
|
15
|
-
export function asLogID(val) { return val; }
|
|
16
|
-
export function asPlanID(val) { return val; }
|
|
1
|
+
export * from "./modules/primitives/types.js";
|
|
17
2
|
//# sourceMappingURL=types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../shared/src/types.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../shared/src/types.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC"}
|
|
@@ -1,52 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* In-Memory Worker Pool — worker_threads based, no Redis/BullMQ.
|
|
3
|
-
*
|
|
4
|
-
* Heavy / isolable work runs off the main thread. Concurrency is capped
|
|
5
|
-
* (default os.cpus().length) via an internal queue so RAM/CPU stay bounded.
|
|
6
|
-
*
|
|
7
|
-
* Thread-safety note: SQLite (better-sqlite3) bindings are NOT thread-safe.
|
|
8
|
-
* Worker threads MUST NOT access AtabeyStorage or any SQLite connection.
|
|
9
|
-
* Only CPU-bound isolable work (hash, tokenize, etc.) should be dispatched
|
|
10
|
-
* to the worker pool.
|
|
11
|
-
*/
|
|
12
|
-
export interface WorkerPoolOptions {
|
|
13
|
-
/** Max concurrent worker jobs. Default: os.cpus().length (clamped 1–16). */
|
|
14
|
-
maxConcurrency?: number;
|
|
15
|
-
/** Idle workers retained in the pool. Default: min(2, maxConcurrency). */
|
|
16
|
-
minWorkers?: number;
|
|
17
|
-
/** Per-job timeout in ms. Default: 30_000. */
|
|
18
|
-
jobTimeoutMs?: number;
|
|
19
|
-
/** Optional absolute path to worker script (tests / monorepo layouts). */
|
|
20
|
-
workerScript?: string;
|
|
21
|
-
}
|
|
22
|
-
export declare class WorkerPool {
|
|
23
|
-
private readonly maxConcurrency;
|
|
24
|
-
private readonly minWorkers;
|
|
25
|
-
private readonly jobTimeoutMs;
|
|
26
|
-
private readonly workerScript;
|
|
27
|
-
private readonly workers;
|
|
28
|
-
private readonly queue;
|
|
29
|
-
private readonly inflight;
|
|
30
|
-
private jobSeq;
|
|
31
|
-
private closed;
|
|
32
|
-
constructor(options?: WorkerPoolOptions);
|
|
33
|
-
/**
|
|
34
|
-
* Run a named job type on a worker thread.
|
|
35
|
-
* Returns when the worker posts a result (or on timeout/error).
|
|
36
|
-
*/
|
|
37
|
-
run<T = unknown>(type: string, payload?: unknown): Promise<T>;
|
|
38
|
-
getStats(): {
|
|
39
|
-
maxConcurrency: number;
|
|
40
|
-
workers: number;
|
|
41
|
-
busy: number;
|
|
42
|
-
pending: number;
|
|
43
|
-
inflight: number;
|
|
44
|
-
};
|
|
45
|
-
close(): Promise<void>;
|
|
46
|
-
private failWorkerJob;
|
|
47
|
-
private spawnWorker;
|
|
48
|
-
private removeWorker;
|
|
49
|
-
private dispatch;
|
|
50
|
-
}
|
|
51
|
-
export declare function getDefaultWorkerPool(options?: WorkerPoolOptions): WorkerPool;
|
|
52
|
-
export declare function resetDefaultWorkerPool(): Promise<void>;
|
|
1
|
+
export * from "./modules/concurrency/worker-pool.js";
|
|
@@ -1,229 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* In-Memory Worker Pool — worker_threads based, no Redis/BullMQ.
|
|
3
|
-
*
|
|
4
|
-
* Heavy / isolable work runs off the main thread. Concurrency is capped
|
|
5
|
-
* (default os.cpus().length) via an internal queue so RAM/CPU stay bounded.
|
|
6
|
-
*
|
|
7
|
-
* Thread-safety note: SQLite (better-sqlite3) bindings are NOT thread-safe.
|
|
8
|
-
* Worker threads MUST NOT access AtabeyStorage or any SQLite connection.
|
|
9
|
-
* Only CPU-bound isolable work (hash, tokenize, etc.) should be dispatched
|
|
10
|
-
* to the worker pool.
|
|
11
|
-
*/
|
|
12
|
-
import fs from "fs";
|
|
13
|
-
import os from "os";
|
|
14
|
-
import path from "path";
|
|
15
|
-
import { fileURLToPath } from "url";
|
|
16
|
-
import { Worker } from "worker_threads";
|
|
17
|
-
function defaultWorkerScript() {
|
|
18
|
-
// Compiled ESM layout: dist/worker-thread.js next to this module
|
|
19
|
-
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
20
|
-
const jsScript = path.join(here, "worker-thread.js");
|
|
21
|
-
const tsScript = path.join(here, "worker-thread.ts");
|
|
22
|
-
// Prefer compiled JS; fall back to TS for dev (tsx/ts-node)
|
|
23
|
-
if (fs.existsSync(jsScript))
|
|
24
|
-
return jsScript;
|
|
25
|
-
if (fs.existsSync(tsScript))
|
|
26
|
-
return tsScript;
|
|
27
|
-
// Fallback: try resolving via import.meta.resolve for monorepo layouts
|
|
28
|
-
try {
|
|
29
|
-
const resolved = import.meta.resolve("./worker-thread.js");
|
|
30
|
-
if (resolved)
|
|
31
|
-
return new URL(resolved).pathname;
|
|
32
|
-
}
|
|
33
|
-
catch {
|
|
34
|
-
// ignore
|
|
35
|
-
}
|
|
36
|
-
return jsScript;
|
|
37
|
-
}
|
|
38
|
-
export class WorkerPool {
|
|
39
|
-
maxConcurrency;
|
|
40
|
-
minWorkers;
|
|
41
|
-
jobTimeoutMs;
|
|
42
|
-
workerScript;
|
|
43
|
-
workers = [];
|
|
44
|
-
queue = [];
|
|
45
|
-
inflight = new Map();
|
|
46
|
-
jobSeq = 0;
|
|
47
|
-
closed = false;
|
|
48
|
-
constructor(options = {}) {
|
|
49
|
-
const cpus = Math.max(1, os.cpus()?.length || 1);
|
|
50
|
-
const requested = options.maxConcurrency ?? cpus;
|
|
51
|
-
this.maxConcurrency = Math.max(1, Math.min(requested, 16));
|
|
52
|
-
this.minWorkers = Math.max(0, Math.min(options.minWorkers ?? Math.min(2, this.maxConcurrency), this.maxConcurrency));
|
|
53
|
-
this.jobTimeoutMs = options.jobTimeoutMs ?? 30_000;
|
|
54
|
-
this.workerScript = options.workerScript ?? defaultWorkerScript();
|
|
55
|
-
for (let i = 0; i < this.minWorkers; i++) {
|
|
56
|
-
this.spawnWorker();
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Run a named job type on a worker thread.
|
|
61
|
-
* Returns when the worker posts a result (or on timeout/error).
|
|
62
|
-
*/
|
|
63
|
-
run(type, payload) {
|
|
64
|
-
if (this.closed) {
|
|
65
|
-
return Promise.reject(new Error("[WorkerPool] Pool is closed."));
|
|
66
|
-
}
|
|
67
|
-
const id = `wjob-${++this.jobSeq}-${Date.now()}`;
|
|
68
|
-
const job = { id, type, payload };
|
|
69
|
-
return new Promise((resolve, reject) => {
|
|
70
|
-
const timer = setTimeout(() => {
|
|
71
|
-
this.inflight.delete(id);
|
|
72
|
-
// Free (and recycle) the worker stuck on this job so a hung job
|
|
73
|
-
// cannot permanently occupy the pool and deadlock every caller.
|
|
74
|
-
const stuck = this.workers.find((w) => w.currentJobId === id);
|
|
75
|
-
if (stuck) {
|
|
76
|
-
stuck.busy = false;
|
|
77
|
-
stuck.currentJobId = null;
|
|
78
|
-
this.removeWorker(stuck);
|
|
79
|
-
void stuck.worker.terminate().catch(() => { });
|
|
80
|
-
}
|
|
81
|
-
reject(new Error(`[WorkerPool] Job ${id} timed out after ${this.jobTimeoutMs}ms`));
|
|
82
|
-
this.dispatch();
|
|
83
|
-
if (!this.closed && this.workers.length < this.minWorkers) {
|
|
84
|
-
this.spawnWorker();
|
|
85
|
-
}
|
|
86
|
-
}, this.jobTimeoutMs);
|
|
87
|
-
const pending = {
|
|
88
|
-
job,
|
|
89
|
-
resolve: (v) => resolve(v),
|
|
90
|
-
reject,
|
|
91
|
-
timer,
|
|
92
|
-
};
|
|
93
|
-
this.queue.push(pending);
|
|
94
|
-
this.dispatch();
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
getStats() {
|
|
98
|
-
return {
|
|
99
|
-
maxConcurrency: this.maxConcurrency,
|
|
100
|
-
workers: this.workers.length,
|
|
101
|
-
busy: this.workers.filter((w) => w.busy).length,
|
|
102
|
-
pending: this.queue.length,
|
|
103
|
-
inflight: this.inflight.size,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
async close() {
|
|
107
|
-
this.closed = true;
|
|
108
|
-
while (this.queue.length > 0) {
|
|
109
|
-
const p = this.queue.shift();
|
|
110
|
-
if (p) {
|
|
111
|
-
clearTimeout(p.timer);
|
|
112
|
-
p.reject(new Error("[WorkerPool] Pool closed while job was queued."));
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
// Settle any in-flight jobs so their awaiters don't hang forever.
|
|
116
|
-
for (const pending of this.inflight.values()) {
|
|
117
|
-
clearTimeout(pending.timer);
|
|
118
|
-
pending.reject(new Error("[WorkerPool] Pool closed while the job was in flight."));
|
|
119
|
-
}
|
|
120
|
-
this.inflight.clear();
|
|
121
|
-
await Promise.all(this.workers.map((pw) => new Promise((resolve) => {
|
|
122
|
-
pw.worker.once("exit", () => resolve());
|
|
123
|
-
void pw.worker.terminate();
|
|
124
|
-
})));
|
|
125
|
-
this.workers.length = 0;
|
|
126
|
-
}
|
|
127
|
-
failWorkerJob(pw, error) {
|
|
128
|
-
const jobId = pw.currentJobId;
|
|
129
|
-
pw.busy = false;
|
|
130
|
-
pw.currentJobId = null;
|
|
131
|
-
if (!jobId)
|
|
132
|
-
return;
|
|
133
|
-
const pending = this.inflight.get(jobId);
|
|
134
|
-
if (!pending)
|
|
135
|
-
return;
|
|
136
|
-
clearTimeout(pending.timer);
|
|
137
|
-
this.inflight.delete(jobId);
|
|
138
|
-
pending.reject(error);
|
|
139
|
-
}
|
|
140
|
-
spawnWorker() {
|
|
141
|
-
const worker = new Worker(this.workerScript);
|
|
142
|
-
const pw = { worker, busy: false, currentJobId: null };
|
|
143
|
-
worker.on("message", (msg) => {
|
|
144
|
-
const pending = this.inflight.get(msg.id);
|
|
145
|
-
pw.busy = false;
|
|
146
|
-
pw.currentJobId = null;
|
|
147
|
-
if (!pending) {
|
|
148
|
-
this.dispatch();
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
clearTimeout(pending.timer);
|
|
152
|
-
this.inflight.delete(msg.id);
|
|
153
|
-
if (msg.ok) {
|
|
154
|
-
pending.resolve(msg.result);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
pending.reject(new Error(msg.error || "Worker job failed"));
|
|
158
|
-
}
|
|
159
|
-
this.dispatch();
|
|
160
|
-
});
|
|
161
|
-
worker.on("error", (err) => {
|
|
162
|
-
this.failWorkerJob(pw, err instanceof Error ? err : new Error(String(err)));
|
|
163
|
-
this.removeWorker(pw);
|
|
164
|
-
if (!this.closed) {
|
|
165
|
-
this.spawnWorker();
|
|
166
|
-
}
|
|
167
|
-
this.dispatch();
|
|
168
|
-
});
|
|
169
|
-
worker.on("exit", (code) => {
|
|
170
|
-
if (pw.busy && pw.currentJobId) {
|
|
171
|
-
this.failWorkerJob(pw, new Error(`[WorkerPool] Worker exited with code ${code} while running job ${pw.currentJobId}`));
|
|
172
|
-
}
|
|
173
|
-
this.removeWorker(pw);
|
|
174
|
-
if (!this.closed && code !== 0) {
|
|
175
|
-
// Replace crashed worker so pool size recovers
|
|
176
|
-
if (this.workers.length < this.minWorkers) {
|
|
177
|
-
this.spawnWorker();
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
this.dispatch();
|
|
181
|
-
});
|
|
182
|
-
this.workers.push(pw);
|
|
183
|
-
return pw;
|
|
184
|
-
}
|
|
185
|
-
removeWorker(pw) {
|
|
186
|
-
const idx = this.workers.indexOf(pw);
|
|
187
|
-
if (idx >= 0)
|
|
188
|
-
this.workers.splice(idx, 1);
|
|
189
|
-
}
|
|
190
|
-
dispatch() {
|
|
191
|
-
if (this.closed)
|
|
192
|
-
return;
|
|
193
|
-
while (this.queue.length > 0) {
|
|
194
|
-
let free = this.workers.find((w) => !w.busy);
|
|
195
|
-
if (!free && this.workers.length < this.maxConcurrency) {
|
|
196
|
-
free = this.spawnWorker();
|
|
197
|
-
}
|
|
198
|
-
if (!free)
|
|
199
|
-
break;
|
|
200
|
-
const pending = this.queue.shift();
|
|
201
|
-
if (!pending)
|
|
202
|
-
break;
|
|
203
|
-
free.busy = true;
|
|
204
|
-
free.currentJobId = pending.job.id;
|
|
205
|
-
this.inflight.set(pending.job.id, pending);
|
|
206
|
-
free.worker.postMessage(pending.job);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
let defaultPool = null;
|
|
211
|
-
export function getDefaultWorkerPool(options) {
|
|
212
|
-
if (!defaultPool) {
|
|
213
|
-
const fromEnv = process.env.ATABEY_WORKER_CONCURRENCY
|
|
214
|
-
? parseInt(process.env.ATABEY_WORKER_CONCURRENCY, 10)
|
|
215
|
-
: undefined;
|
|
216
|
-
defaultPool = new WorkerPool({
|
|
217
|
-
maxConcurrency: options?.maxConcurrency ?? (Number.isFinite(fromEnv) ? fromEnv : undefined),
|
|
218
|
-
...options,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
return defaultPool;
|
|
222
|
-
}
|
|
223
|
-
export async function resetDefaultWorkerPool() {
|
|
224
|
-
if (defaultPool) {
|
|
225
|
-
await defaultPool.close();
|
|
226
|
-
defaultPool = null;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
1
|
+
export * from "./modules/concurrency/worker-pool.js";
|
|
229
2
|
//# sourceMappingURL=worker-pool.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-pool.js","sourceRoot":"","sources":["../../../../shared/src/worker-pool.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"worker-pool.js","sourceRoot":"","sources":["../../../../shared/src/worker-pool.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC"}
|
|
@@ -1,21 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Generic worker_threads entrypoint for Atabey WorkerPool.
|
|
3
|
-
* Receives serializable jobs and returns serializable results.
|
|
4
|
-
*
|
|
5
|
-
* Supported job types (extensible):
|
|
6
|
-
* - "ping" — health check
|
|
7
|
-
* - "hash" — simple string hash (CPU sample)
|
|
8
|
-
* - "tokenize" — tokenization for TF-IDF style work
|
|
9
|
-
* - "sleep" — test helper
|
|
10
|
-
*/
|
|
11
|
-
export interface WorkerJob {
|
|
12
|
-
id: string;
|
|
13
|
-
type: string;
|
|
14
|
-
payload?: unknown;
|
|
15
|
-
}
|
|
16
|
-
export interface WorkerResult {
|
|
17
|
-
id: string;
|
|
18
|
-
ok: boolean;
|
|
19
|
-
result?: unknown;
|
|
20
|
-
error?: string;
|
|
21
|
-
}
|
|
1
|
+
export * from "./modules/concurrency/worker-thread.js";
|