@simulacra-ai/core 0.0.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 (134) hide show
  1. package/README.md +232 -0
  2. package/dist/checkpoints/default-summarization-strategy.d.ts +13 -0
  3. package/dist/checkpoints/default-summarization-strategy.d.ts.map +1 -0
  4. package/dist/checkpoints/default-summarization-strategy.js +63 -0
  5. package/dist/checkpoints/default-summarization-strategy.js.map +1 -0
  6. package/dist/checkpoints/index.d.ts +3 -0
  7. package/dist/checkpoints/index.d.ts.map +1 -0
  8. package/dist/checkpoints/index.js +3 -0
  9. package/dist/checkpoints/index.js.map +1 -0
  10. package/dist/checkpoints/types.d.ts +32 -0
  11. package/dist/checkpoints/types.d.ts.map +1 -0
  12. package/dist/checkpoints/types.js +2 -0
  13. package/dist/checkpoints/types.js.map +1 -0
  14. package/dist/context-transformers/checkpoint-context-transformer.d.ts +13 -0
  15. package/dist/context-transformers/checkpoint-context-transformer.d.ts.map +1 -0
  16. package/dist/context-transformers/checkpoint-context-transformer.js +32 -0
  17. package/dist/context-transformers/checkpoint-context-transformer.js.map +1 -0
  18. package/dist/context-transformers/composite-context-transformer.d.ts +31 -0
  19. package/dist/context-transformers/composite-context-transformer.d.ts.map +1 -0
  20. package/dist/context-transformers/composite-context-transformer.js +41 -0
  21. package/dist/context-transformers/composite-context-transformer.js.map +1 -0
  22. package/dist/context-transformers/index.d.ts +6 -0
  23. package/dist/context-transformers/index.d.ts.map +1 -0
  24. package/dist/context-transformers/index.js +6 -0
  25. package/dist/context-transformers/index.js.map +1 -0
  26. package/dist/context-transformers/noop-context-transformer.d.ts +24 -0
  27. package/dist/context-transformers/noop-context-transformer.d.ts.map +1 -0
  28. package/dist/context-transformers/noop-context-transformer.js +26 -0
  29. package/dist/context-transformers/noop-context-transformer.js.map +1 -0
  30. package/dist/context-transformers/tool-context-transformer.d.ts +26 -0
  31. package/dist/context-transformers/tool-context-transformer.d.ts.map +1 -0
  32. package/dist/context-transformers/tool-context-transformer.js +52 -0
  33. package/dist/context-transformers/tool-context-transformer.js.map +1 -0
  34. package/dist/context-transformers/types.d.ts +65 -0
  35. package/dist/context-transformers/types.d.ts.map +1 -0
  36. package/dist/context-transformers/types.js +2 -0
  37. package/dist/context-transformers/types.js.map +1 -0
  38. package/dist/conversations/conversation.d.ts +146 -0
  39. package/dist/conversations/conversation.d.ts.map +1 -0
  40. package/dist/conversations/conversation.js +642 -0
  41. package/dist/conversations/conversation.js.map +1 -0
  42. package/dist/conversations/index.d.ts +5 -0
  43. package/dist/conversations/index.d.ts.map +1 -0
  44. package/dist/conversations/index.js +5 -0
  45. package/dist/conversations/index.js.map +1 -0
  46. package/dist/conversations/stream-listener.d.ts +37 -0
  47. package/dist/conversations/stream-listener.d.ts.map +1 -0
  48. package/dist/conversations/stream-listener.js +68 -0
  49. package/dist/conversations/stream-listener.js.map +1 -0
  50. package/dist/conversations/token-tracker.d.ts +59 -0
  51. package/dist/conversations/token-tracker.d.ts.map +1 -0
  52. package/dist/conversations/token-tracker.js +98 -0
  53. package/dist/conversations/token-tracker.js.map +1 -0
  54. package/dist/conversations/types.d.ts +346 -0
  55. package/dist/conversations/types.d.ts.map +1 -0
  56. package/dist/conversations/types.js +2 -0
  57. package/dist/conversations/types.js.map +1 -0
  58. package/dist/index.d.ts +10 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +9 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/policies/composite-policy.d.ts +29 -0
  63. package/dist/policies/composite-policy.d.ts.map +1 -0
  64. package/dist/policies/composite-policy.js +77 -0
  65. package/dist/policies/composite-policy.js.map +1 -0
  66. package/dist/policies/default-policy.d.ts +7 -0
  67. package/dist/policies/default-policy.d.ts.map +1 -0
  68. package/dist/policies/default-policy.js +14 -0
  69. package/dist/policies/default-policy.js.map +1 -0
  70. package/dist/policies/index.d.ts +8 -0
  71. package/dist/policies/index.d.ts.map +1 -0
  72. package/dist/policies/index.js +8 -0
  73. package/dist/policies/index.js.map +1 -0
  74. package/dist/policies/noop-policy.d.ts +21 -0
  75. package/dist/policies/noop-policy.d.ts.map +1 -0
  76. package/dist/policies/noop-policy.js +36 -0
  77. package/dist/policies/noop-policy.js.map +1 -0
  78. package/dist/policies/rate-limit-policy.d.ts +44 -0
  79. package/dist/policies/rate-limit-policy.d.ts.map +1 -0
  80. package/dist/policies/rate-limit-policy.js +98 -0
  81. package/dist/policies/rate-limit-policy.js.map +1 -0
  82. package/dist/policies/retry-policy.d.ts +42 -0
  83. package/dist/policies/retry-policy.d.ts.map +1 -0
  84. package/dist/policies/retry-policy.js +114 -0
  85. package/dist/policies/retry-policy.js.map +1 -0
  86. package/dist/policies/token-limit-policy.d.ts +76 -0
  87. package/dist/policies/token-limit-policy.d.ts.map +1 -0
  88. package/dist/policies/token-limit-policy.js +134 -0
  89. package/dist/policies/token-limit-policy.js.map +1 -0
  90. package/dist/policies/types.d.ts +49 -0
  91. package/dist/policies/types.d.ts.map +1 -0
  92. package/dist/policies/types.js +9 -0
  93. package/dist/policies/types.js.map +1 -0
  94. package/dist/tools/index.d.ts +2 -0
  95. package/dist/tools/index.d.ts.map +1 -0
  96. package/dist/tools/index.js +2 -0
  97. package/dist/tools/index.js.map +1 -0
  98. package/dist/tools/types.d.ts +180 -0
  99. package/dist/tools/types.d.ts.map +1 -0
  100. package/dist/tools/types.js +2 -0
  101. package/dist/tools/types.js.map +1 -0
  102. package/dist/utils/async.d.ts +125 -0
  103. package/dist/utils/async.d.ts.map +1 -0
  104. package/dist/utils/async.js +217 -0
  105. package/dist/utils/async.js.map +1 -0
  106. package/dist/utils/index.d.ts +4 -0
  107. package/dist/utils/index.d.ts.map +1 -0
  108. package/dist/utils/index.js +4 -0
  109. package/dist/utils/index.js.map +1 -0
  110. package/dist/utils/object.d.ts +80 -0
  111. package/dist/utils/object.d.ts.map +1 -0
  112. package/dist/utils/object.js +189 -0
  113. package/dist/utils/object.js.map +1 -0
  114. package/dist/utils/types.d.ts +17 -0
  115. package/dist/utils/types.d.ts.map +1 -0
  116. package/dist/utils/types.js +2 -0
  117. package/dist/utils/types.js.map +1 -0
  118. package/dist/workflows/index.d.ts +4 -0
  119. package/dist/workflows/index.d.ts.map +1 -0
  120. package/dist/workflows/index.js +4 -0
  121. package/dist/workflows/index.js.map +1 -0
  122. package/dist/workflows/types.d.ts +70 -0
  123. package/dist/workflows/types.d.ts.map +1 -0
  124. package/dist/workflows/types.js +2 -0
  125. package/dist/workflows/types.js.map +1 -0
  126. package/dist/workflows/workflow-manager.d.ts +74 -0
  127. package/dist/workflows/workflow-manager.d.ts.map +1 -0
  128. package/dist/workflows/workflow-manager.js +165 -0
  129. package/dist/workflows/workflow-manager.js.map +1 -0
  130. package/dist/workflows/workflow.d.ts +116 -0
  131. package/dist/workflows/workflow.d.ts.map +1 -0
  132. package/dist/workflows/workflow.js +351 -0
  133. package/dist/workflows/workflow.js.map +1 -0
  134. package/package.json +20 -0
@@ -0,0 +1,76 @@
1
+ import { Conversation } from "../conversations/index.ts";
2
+ import { CancellationToken } from "../utils/async.ts";
3
+ import { Policy, PolicyResult } from "./types.ts";
4
+ /**
5
+ * Record of token usage for a single request.
6
+ */
7
+ export interface TokenRecord {
8
+ /** When this usage occurred. */
9
+ timestamp: number;
10
+ /** Number of input tokens consumed. */
11
+ input_tokens: number;
12
+ /** Number of output tokens generated. */
13
+ output_tokens: number;
14
+ }
15
+ /**
16
+ * Base options for token limit policy.
17
+ */
18
+ type BaseOptions = {
19
+ /** Time period in milliseconds for the token budget. */
20
+ period_ms: number;
21
+ };
22
+ /**
23
+ * Options for separate input and output token limits.
24
+ */
25
+ type InputOutputTokenOptions = {
26
+ /** Maximum input tokens allowed per period. */
27
+ input_tokens_per_period: number;
28
+ /** Maximum output tokens allowed per period. */
29
+ output_tokens_per_period: number;
30
+ };
31
+ /**
32
+ * Options for a combined total token limit.
33
+ */
34
+ type TotalTokenOptions = {
35
+ /** Maximum total tokens (input + output) allowed per period. */
36
+ total_tokens_per_period: number;
37
+ };
38
+ /**
39
+ * Configuration options for token limit policy.
40
+ *
41
+ * Can specify either separate input/output limits or a combined total limit.
42
+ */
43
+ export type TokenLimitPolicyOptions = BaseOptions & (InputOutputTokenOptions | TotalTokenOptions);
44
+ /**
45
+ * Policy that enforces token usage limits over a time period.
46
+ *
47
+ * Tracks token consumption and delays execution when limits are exceeded.
48
+ */
49
+ export declare class TokenLimitPolicy extends Policy {
50
+ #private;
51
+ /**
52
+ * Creates a new token limit policy.
53
+ *
54
+ * @param options - The token limit configuration.
55
+ */
56
+ constructor(options: TokenLimitPolicyOptions);
57
+ /**
58
+ * Executes a function with token limit enforcement applied.
59
+ *
60
+ * @template TResult - The return type of the function.
61
+ * @template TArgs - The argument types of the function.
62
+ * @param cancellation_token - Token for cancelling the operation.
63
+ * @param fn - The function to execute.
64
+ * @param args - Arguments to pass to the function.
65
+ * @returns A promise that resolves to the policy result.
66
+ */
67
+ execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
68
+ /**
69
+ * Attaches this policy to a conversation to track its token usage.
70
+ *
71
+ * @param conversation - The conversation to monitor.
72
+ */
73
+ attach(conversation: Conversation): void;
74
+ }
75
+ export {};
76
+ //# sourceMappingURL=token-limit-policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-limit-policy.d.ts","sourceRoot":"","sources":["../../src/policies/token-limit-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EAGb,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAS,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,gCAAgC;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,uCAAuC;IACvC,YAAY,EAAE,MAAM,CAAC;IACrB,yCAAyC;IACzC,aAAa,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,KAAK,WAAW,GAAG;IACjB,wDAAwD;IACxD,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,KAAK,uBAAuB,GAAG;IAC7B,+CAA+C;IAC/C,uBAAuB,EAAE,MAAM,CAAC;IAChC,gDAAgD;IAChD,wBAAwB,EAAE,MAAM,CAAC;CAClC,CAAC;AAEF;;GAEG;AACH,KAAK,iBAAiB,GAAG;IACvB,gEAAgE;IAChE,uBAAuB,EAAE,MAAM,CAAC;CACjC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,uBAAuB,GAAG,WAAW,GAAG,CAAC,uBAAuB,GAAG,iBAAiB,CAAC,CAAC;AAElG;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,MAAM;;IAK1C;;;;OAIG;gBACS,OAAO,EAAE,uBAAuB;IAK5C;;;;;;;;;OASG;IACG,OAAO,CAAC,OAAO,EAAE,KAAK,SAAS,OAAO,EAAE,EAC5C,kBAAkB,EAAE,iBAAiB,EACrC,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,EACxC,GAAG,IAAI,EAAE,KAAK,GACb,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IA6EjC;;;;OAIG;IACH,MAAM,CAAC,YAAY,EAAE,YAAY;CA8ClC"}
@@ -0,0 +1,134 @@
1
+ import { sleep } from "../utils/async.js";
2
+ import { Policy } from "./types.js";
3
+ /**
4
+ * Policy that enforces token usage limits over a time period.
5
+ *
6
+ * Tracks token consumption and delays execution when limits are exceeded.
7
+ */
8
+ export class TokenLimitPolicy extends Policy {
9
+ #options;
10
+ #usage = [];
11
+ /**
12
+ * Creates a new token limit policy.
13
+ *
14
+ * @param options - The token limit configuration.
15
+ */
16
+ constructor(options) {
17
+ super();
18
+ this.#options = options;
19
+ }
20
+ /**
21
+ * Executes a function with token limit enforcement applied.
22
+ *
23
+ * @template TResult - The return type of the function.
24
+ * @template TArgs - The argument types of the function.
25
+ * @param cancellation_token - Token for cancelling the operation.
26
+ * @param fn - The function to execute.
27
+ * @param args - Arguments to pass to the function.
28
+ * @returns A promise that resolves to the policy result.
29
+ */
30
+ async execute(cancellation_token, fn, ...args) {
31
+ const now = Date.now();
32
+ this.#usage = this.#usage.filter((record) => now - record.timestamp < this.#options.period_ms);
33
+ const input_tokens = this.#usage.reduce((sum, record) => sum + record.input_tokens, 0);
34
+ const output_tokens = this.#usage.reduce((sum, record) => sum + record.output_tokens, 0);
35
+ const total_tokens = input_tokens + output_tokens;
36
+ let wait_time;
37
+ const options = this.#options;
38
+ if ("total_tokens_per_period" in options) {
39
+ const total_token_wait_time = this.#calculate_wait_time(total_tokens, options.total_tokens_per_period);
40
+ wait_time = total_token_wait_time;
41
+ }
42
+ else {
43
+ const input_token_wait_time = this.#calculate_wait_time(input_tokens, options.input_tokens_per_period);
44
+ const output_token_wait_time = this.#calculate_wait_time(output_tokens, options.output_tokens_per_period);
45
+ wait_time = Math.max(input_token_wait_time, output_token_wait_time);
46
+ }
47
+ const metadata = {
48
+ policy: TokenLimitPolicy.name,
49
+ ...this.#options,
50
+ current_period_start: now - this.#options.period_ms,
51
+ current_period_end: now,
52
+ input_tokens,
53
+ output_tokens,
54
+ total_tokens,
55
+ wait_time,
56
+ recent_usage_count: this.#usage.length,
57
+ recent_usage: this.#usage,
58
+ };
59
+ if ("total_tokens_per_period" in options) {
60
+ metadata.total_token_wait_time = wait_time;
61
+ }
62
+ else {
63
+ metadata.input_token_wait_time = this.#calculate_wait_time(input_tokens, options.input_tokens_per_period);
64
+ metadata.output_token_wait_time = this.#calculate_wait_time(output_tokens, options.output_tokens_per_period);
65
+ }
66
+ if (wait_time > 0) {
67
+ metadata.wait_time_ms = wait_time;
68
+ await sleep(wait_time, cancellation_token);
69
+ }
70
+ cancellation_token.throw_if_cancellation_requested();
71
+ try {
72
+ const value = await Promise.race([fn(...args), cancellation_token.await_cancellation()]);
73
+ return {
74
+ result: true,
75
+ value,
76
+ metadata,
77
+ };
78
+ }
79
+ catch (error) {
80
+ return {
81
+ result: false,
82
+ error,
83
+ metadata,
84
+ };
85
+ }
86
+ }
87
+ /**
88
+ * Attaches this policy to a conversation to track its token usage.
89
+ *
90
+ * @param conversation - The conversation to monitor.
91
+ */
92
+ attach(conversation) {
93
+ conversation.on("message_complete", this.#on_message_complete);
94
+ conversation.on("child_event", this.#on_child_event);
95
+ conversation.once("dispose", () => {
96
+ conversation.off("message_complete", this.#on_message_complete);
97
+ conversation.off("child_event", this.#on_child_event);
98
+ });
99
+ }
100
+ #on_message_complete = ({ usage }) => {
101
+ const input_tokens = usage?.input_tokens || 0;
102
+ const output_tokens = usage?.output_tokens || 0;
103
+ this.#usage.push({
104
+ timestamp: Date.now(),
105
+ input_tokens,
106
+ output_tokens,
107
+ });
108
+ };
109
+ #on_child_event = (event_data) => {
110
+ const { event_name, event_args } = event_data;
111
+ if (event_name === "message_complete") {
112
+ const [event_data] = event_args;
113
+ if (event_data) {
114
+ this.#on_message_complete(event_data);
115
+ }
116
+ }
117
+ if (event_name === "child_event") {
118
+ const [child_event_data] = event_args;
119
+ this.#on_child_event(child_event_data);
120
+ }
121
+ };
122
+ #calculate_wait_time(current_tokens, tokens_per_period) {
123
+ if (current_tokens < tokens_per_period) {
124
+ return 0;
125
+ }
126
+ const now = Date.now();
127
+ const oldest = this.#usage[0];
128
+ if (!oldest) {
129
+ return 0;
130
+ }
131
+ return Math.max(0, oldest.timestamp + this.#options.period_ms - now);
132
+ }
133
+ }
134
+ //# sourceMappingURL=token-limit-policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-limit-policy.js","sourceRoot":"","sources":["../../src/policies/token-limit-policy.ts"],"names":[],"mappings":"AAKA,OAAO,EAAqB,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAgB,MAAM,YAAY,CAAC;AA+Cl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AC,CAAC;YACxC,CAAC;QACH,CAAC;QAED,IAAI,UAAU,KAAK,aAAa,EAAE,CAAC;YACjC,MAAM,CAAC,gBAAgB,CAAC,GAAG,UAAU,CAAC;YACtC,IAAI,CAAC,eAAe,CAAC,gBAAgB,CAAC,CAAC;QACzC,CAAC;IACH,CAAC,CAAC;IAEF,oBAAoB,CAAC,cAAsB,EAAE,iBAAyB;QACpE,IAAI,cAAc,GAAG,iBAAiB,EAAE,CAAC;YACvC,OAAO,CAAC,CAAC;QACX,CAAC;QACD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAC9B,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,CAAC,CAAC;QACX,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC;IACvE,CAAC;CACF"}
@@ -0,0 +1,49 @@
1
+ import { CancellationToken } from "../utils/async.ts";
2
+ /**
3
+ * Represents a successful policy execution.
4
+ *
5
+ * @template T - The type of the result value.
6
+ */
7
+ export interface PolicySuccessResult<T> {
8
+ result: true;
9
+ /** Metadata about the policy execution. */
10
+ metadata: object;
11
+ /** The value returned by the wrapped function. */
12
+ value: T;
13
+ }
14
+ /**
15
+ * Represents a failed policy execution.
16
+ */
17
+ export interface PolicyErrorResult {
18
+ result: false;
19
+ /** Metadata about the policy execution. */
20
+ metadata: object;
21
+ /** The error that occurred. */
22
+ error: unknown;
23
+ }
24
+ /**
25
+ * Union of all possible policy execution results.
26
+ *
27
+ * @template T - The type of the success result value.
28
+ */
29
+ export type PolicyResult<T> = PolicySuccessResult<T> | PolicyErrorResult;
30
+ /**
31
+ * Base class for all policies.
32
+ *
33
+ * Policies wrap function execution to provide cross-cutting concerns like
34
+ * retries, rate limiting, and token budget management.
35
+ */
36
+ export declare abstract class Policy {
37
+ /**
38
+ * Executes a function with this policy applied.
39
+ *
40
+ * @template TResult - The return type of the function.
41
+ * @template TArgs - The argument types of the function.
42
+ * @param cancellation_token - Token for cancelling the operation.
43
+ * @param fn - The function to execute.
44
+ * @param args - Arguments to pass to the function.
45
+ * @returns A promise that resolves to the policy result.
46
+ */
47
+ abstract execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
48
+ }
49
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/policies/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD;;;;GAIG;AACH,MAAM,WAAW,mBAAmB,CAAC,CAAC;IACpC,MAAM,EAAE,IAAI,CAAC;IACb,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;IACjB,kDAAkD;IAClD,KAAK,EAAE,CAAC,CAAC;CACV;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,KAAK,CAAC;IACd,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;IACjB,+BAA+B;IAC/B,KAAK,EAAE,OAAO,CAAC;CAChB;AAED;;;;GAIG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,mBAAmB,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC;AAEzE;;;;;GAKG;AACH,8BAAsB,MAAM;IAC1B;;;;;;;;;OASG;IACH,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,KAAK,SAAS,OAAO,EAAE,EAC/C,kBAAkB,EAAE,iBAAiB,EACrC,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,EACxC,GAAG,IAAI,EAAE,KAAK,GACb,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;CAClC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Base class for all policies.
3
+ *
4
+ * Policies wrap function execution to provide cross-cutting concerns like
5
+ * retries, rate limiting, and token budget management.
6
+ */
7
+ export class Policy {
8
+ }
9
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/policies/types.ts"],"names":[],"mappings":"AAiCA;;;;;GAKG;AACH,MAAM,OAAgB,MAAM;CAgB3B"}
@@ -0,0 +1,2 @@
1
+ export * from "./types.ts";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./types.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
@@ -0,0 +1,180 @@
1
+ import type { Conversation } from "../conversations/index.ts";
2
+ import type { Workflow } from "../workflows/index.ts";
3
+ /**
4
+ * Context provided to tools when they are executed.
5
+ *
6
+ * This interface can be extended with custom properties by passing
7
+ * additional data through the workflow's context_data.
8
+ */
9
+ export interface ToolContext {
10
+ /** The conversation instance where the tool is being executed. */
11
+ conversation: Conversation;
12
+ /** The workflow instance managing the tool execution. */
13
+ workflow: Workflow;
14
+ /** Additional custom context properties. */
15
+ [key: string]: unknown;
16
+ }
17
+ /**
18
+ * Result indicating successful tool execution.
19
+ */
20
+ export interface ToolSuccessResult {
21
+ result: true;
22
+ }
23
+ /**
24
+ * Result indicating failed tool execution.
25
+ */
26
+ export interface ToolErrorResult {
27
+ result: false;
28
+ /** Description of what went wrong. */
29
+ message: string;
30
+ /** Optional underlying error object. */
31
+ error?: unknown;
32
+ }
33
+ /**
34
+ * Union of all possible tool execution results.
35
+ */
36
+ export type ToolResult = ToolSuccessResult | ToolErrorResult;
37
+ /**
38
+ * String parameter type definition.
39
+ */
40
+ export type StringParameterType = {
41
+ type: "string";
42
+ required: true;
43
+ default?: never;
44
+ description?: string;
45
+ } | {
46
+ type: "string";
47
+ required?: false;
48
+ default?: string;
49
+ description?: string;
50
+ };
51
+ /**
52
+ * Enumeration parameter type definition.
53
+ */
54
+ export type EnumParameterType = {
55
+ type: "string";
56
+ required: true;
57
+ enum: string[];
58
+ default?: never;
59
+ description?: string;
60
+ } | {
61
+ type: "string";
62
+ required?: false;
63
+ enum: string[];
64
+ default?: string;
65
+ description?: string;
66
+ };
67
+ /**
68
+ * Number parameter type definition.
69
+ */
70
+ export type NumberParameterType = {
71
+ type: "number";
72
+ required: true;
73
+ default?: never;
74
+ description?: string;
75
+ } | {
76
+ type: "number";
77
+ required?: false;
78
+ default?: number;
79
+ description?: string;
80
+ };
81
+ /**
82
+ * Boolean parameter type definition.
83
+ */
84
+ export type BooleanParameterType = {
85
+ type: "boolean";
86
+ required: true;
87
+ default?: never;
88
+ description?: string;
89
+ } | {
90
+ type: "boolean";
91
+ required?: false;
92
+ default?: boolean;
93
+ description?: string;
94
+ };
95
+ /**
96
+ * Union of all primitive parameter types.
97
+ */
98
+ export type PrimitiveParameterType = StringParameterType | EnumParameterType | NumberParameterType | BooleanParameterType;
99
+ /**
100
+ * Object parameter type definition with nested properties.
101
+ */
102
+ export interface ObjectParameterType {
103
+ type: "object";
104
+ required?: boolean;
105
+ properties: Record<string, ParameterType>;
106
+ default?: never;
107
+ description?: string;
108
+ }
109
+ /**
110
+ * Array parameter type definition.
111
+ */
112
+ export interface ArrayParameterType {
113
+ type: "array";
114
+ required?: boolean;
115
+ items: ParameterType;
116
+ default?: never;
117
+ description?: string;
118
+ }
119
+ /**
120
+ * Union of all parameter types.
121
+ */
122
+ export type ParameterType = PrimitiveParameterType | ObjectParameterType | ArrayParameterType;
123
+ /**
124
+ * A named parameter with its type definition and optional description.
125
+ */
126
+ export type ToolParameterDefinition = ParameterType & {
127
+ name: string;
128
+ description?: string;
129
+ };
130
+ /**
131
+ * Complete definition of a tool including its name, description, and parameters.
132
+ */
133
+ export interface ToolDefinition {
134
+ /** The unique name of the tool. */
135
+ name: string;
136
+ /** Description of what the tool does. */
137
+ description: string;
138
+ /** The parameters the tool accepts. */
139
+ parameters: ToolParameterDefinition[];
140
+ /** Whether this tool can be executed in parallel with other tools. */
141
+ parallelizable?: boolean;
142
+ }
143
+ /**
144
+ * Constructor interface for tool classes.
145
+ *
146
+ * @template TParams - The parameter type for the tool.
147
+ * @template TSuccessResult - The success result type.
148
+ * @template TErrorResult - The error result type.
149
+ */
150
+ export interface ToolClass<TParams extends Record<string, unknown> = Record<string, unknown>, TSuccessResult extends ToolSuccessResult = ToolSuccessResult, TErrorResult extends ToolErrorResult = ToolErrorResult> {
151
+ /**
152
+ * Constructs a new tool instance.
153
+ *
154
+ * @param context - The context for tool execution.
155
+ */
156
+ new (context: ToolContext): Tool<TParams, TSuccessResult, TErrorResult>;
157
+ /**
158
+ * Gets the static definition of this tool.
159
+ *
160
+ * @returns The tool definition.
161
+ */
162
+ get_definition(): ToolDefinition;
163
+ }
164
+ /**
165
+ * Interface that all tools must implement.
166
+ *
167
+ * @template TParams - The parameter type for the tool.
168
+ * @template TSuccessResult - The success result type.
169
+ * @template TErrorResult - The error result type.
170
+ */
171
+ export interface Tool<TParams extends Record<string, unknown> = Record<string, unknown>, TSuccessResult extends ToolSuccessResult = ToolSuccessResult, TErrorResult extends ToolErrorResult = ToolErrorResult> {
172
+ /**
173
+ * Executes the tool with the provided parameters.
174
+ *
175
+ * @param params - The parameters for tool execution.
176
+ * @returns A promise that resolves to either success or error result.
177
+ */
178
+ execute(params: TParams): Promise<TSuccessResult | TErrorResult>;
179
+ }
180
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":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;OAKG;IACH,OAAO,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,GAAG,YAAY,CAAC,CAAC;CAClE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/tools/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Token for monitoring and responding to cancellation requests.
3
+ *
4
+ * Provides a way to check if an operation has been cancelled and to
5
+ * register callbacks for when cancellation occurs.
6
+ */
7
+ export declare class CancellationToken {
8
+ #private;
9
+ /**
10
+ * Creates a new cancellation token.
11
+ *
12
+ * @param source - Optional source that controls this token.
13
+ */
14
+ constructor(source?: CancellationTokenSource);
15
+ /**
16
+ * Whether cancellation has been requested.
17
+ */
18
+ get is_cancellation_requested(): boolean;
19
+ /**
20
+ * Registers a one-time handler for cancellation.
21
+ *
22
+ * @param event - The event name (always "cancel").
23
+ * @param handler - The callback to invoke when cancelled.
24
+ */
25
+ once(event: "cancel", handler: () => void): void;
26
+ /**
27
+ * Removes a cancellation handler.
28
+ *
29
+ * @param event - The event name (always "cancel").
30
+ * @param handler - The callback to remove.
31
+ */
32
+ off(event: "cancel", handler: () => void): void;
33
+ /**
34
+ * Throws an error if cancellation has been requested.
35
+ */
36
+ throw_if_cancellation_requested(): void;
37
+ /**
38
+ * Creates a promise that rejects when cancellation is requested.
39
+ *
40
+ * @returns A promise that never resolves but rejects on cancellation.
41
+ */
42
+ await_cancellation(): Promise<never>;
43
+ /**
44
+ * Creates an empty cancellation token that can never be cancelled.
45
+ *
46
+ * @returns A new cancellation token with no source.
47
+ */
48
+ static empty(): CancellationToken;
49
+ }
50
+ /**
51
+ * Source for creating and controlling cancellation tokens.
52
+ *
53
+ * Allows triggering cancellation for associated tokens.
54
+ */
55
+ export declare class CancellationTokenSource {
56
+ #private;
57
+ /**
58
+ * Disposes the cancellation token source.
59
+ *
60
+ * This method is called automatically when using the `using` keyword.
61
+ */
62
+ [Symbol.dispose](): void;
63
+ /**
64
+ * Gets a cancellation token associated with this source.
65
+ */
66
+ get token(): CancellationToken;
67
+ /**
68
+ * Whether cancellation has been triggered.
69
+ */
70
+ get is_cancelled(): boolean;
71
+ /**
72
+ * Registers a one-time handler for cancellation.
73
+ *
74
+ * @param event - The event name (always "cancel").
75
+ * @param handler - The callback to invoke when cancelled.
76
+ */
77
+ once(event: "cancel", handler: () => void): void;
78
+ /**
79
+ * Removes a cancellation handler.
80
+ *
81
+ * @param event - The event name (always "cancel").
82
+ * @param handler - The callback to remove.
83
+ */
84
+ off(event: "cancel", handler: () => void): void;
85
+ /**
86
+ * Triggers cancellation for all associated tokens.
87
+ */
88
+ cancel(): void;
89
+ }
90
+ /**
91
+ * Error thrown when an operation is cancelled.
92
+ */
93
+ export declare class OperationCanceledError extends Error {
94
+ #private;
95
+ /**
96
+ * Creates a new operation cancelled error.
97
+ *
98
+ * @param token - The cancellation token associated with this error.
99
+ */
100
+ constructor(token: CancellationToken);
101
+ /**
102
+ * The cancellation token associated with this error.
103
+ */
104
+ get token(): CancellationToken;
105
+ }
106
+ /**
107
+ * Delays execution for a specified time period with cancellation support.
108
+ *
109
+ * @param ms - The number of milliseconds to sleep.
110
+ * @param cancellation_token - Optional token for cancelling the sleep.
111
+ * @returns A promise that resolves after the delay or rejects if cancelled.
112
+ */
113
+ export declare function sleep(ms: number, cancellation_token?: CancellationToken): Promise<void>;
114
+ /**
115
+ * Peeks at the first value from an async generator without consuming it.
116
+ *
117
+ * @template T - The type of values yielded by the generator.
118
+ * @param generator - The async generator to peek.
119
+ * @returns An object containing the peeked value and a new generator that includes it.
120
+ */
121
+ export declare function peek_generator<T>(generator: AsyncGenerator<T>): Promise<{
122
+ peeked_value: T;
123
+ generator: AsyncGenerator<Awaited<T>, void, unknown>;
124
+ }>;
125
+ //# sourceMappingURL=async.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async.d.ts","sour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