@polos/sdk 0.1.2 → 0.2.0

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/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { ZodSchema, ZodTypeDef } from 'zod';
1
+ import { ZodType } from 'zod';
2
2
  import { ModelMessage, LanguageModel, jsonSchema } from 'ai';
3
3
  export { LanguageModel } from 'ai';
4
4
  import { Span, Tracer } from '@opentelemetry/api';
@@ -157,8 +157,6 @@ declare class AgentRunConfig {
157
157
  readonly input: string | Record<string, unknown>[];
158
158
  /** Session ID */
159
159
  readonly sessionId: string | undefined;
160
- /** Conversation ID for history tracking */
161
- readonly conversationId: string | undefined;
162
160
  /** User ID */
163
161
  readonly userId: string | undefined;
164
162
  /** Whether to stream the response */
@@ -173,7 +171,6 @@ declare class AgentRunConfig {
173
171
  agent: Workflow;
174
172
  input: string | Record<string, unknown>[];
175
173
  sessionId?: string;
176
- conversationId?: string;
177
174
  userId?: string;
178
175
  streaming?: boolean;
179
176
  initialState?: Record<string, unknown>;
@@ -370,8 +367,302 @@ interface AgentContext<TState = unknown> extends WorkflowContext<TState> {
370
367
  readonly temperature?: number | undefined;
371
368
  /** Maximum output tokens */
372
369
  readonly maxTokens?: number | undefined;
373
- /** Conversation ID (mutable — set by agent handler, matching Python) */
374
- conversationId?: string | undefined;
370
+ }
371
+
372
+ /**
373
+ * Event-related type definitions.
374
+ *
375
+ * Types match sdk/python/polos/features/events.py exactly.
376
+ */
377
+ /**
378
+ * Event data structure for publishing events.
379
+ * Matches Python EventData.
380
+ */
381
+ interface EventData {
382
+ /** Type of event */
383
+ eventType?: string | undefined;
384
+ /** Event payload */
385
+ data: Record<string, unknown>;
386
+ }
387
+ /**
388
+ * Event payload received when waiting for events in workflows.
389
+ * Returned by ctx.step.waitForEvent() when an event is received.
390
+ * Matches Python EventPayload.
391
+ */
392
+ interface EventPayload {
393
+ /** Event ID (UUID string) */
394
+ id: string;
395
+ /** Global sequence ID for ordering */
396
+ sequenceId: number;
397
+ /** Event topic */
398
+ topic: string;
399
+ /** Type of event */
400
+ eventType?: string | undefined;
401
+ /** Event payload */
402
+ data: Record<string, unknown>;
403
+ /** Timestamp when event was created */
404
+ createdAt: string;
405
+ }
406
+ /**
407
+ * Single event item in a batch of events.
408
+ * Used in BatchEventPayload for event-triggered workflows with batching.
409
+ * Matches Python EventItem.
410
+ */
411
+ interface EventItem {
412
+ /** Event ID (UUID string) */
413
+ id: string;
414
+ /** Global sequence ID for ordering */
415
+ sequenceId: number;
416
+ /** Event topic */
417
+ topic: string;
418
+ /** Type of event */
419
+ eventType?: string | undefined;
420
+ /** Event payload */
421
+ data: Record<string, unknown>;
422
+ /** Timestamp when event was created */
423
+ createdAt: string;
424
+ }
425
+ /**
426
+ * Batch event payload for event-triggered workflows with batching.
427
+ * Matches Python BatchEventPayload.
428
+ */
429
+ interface BatchEventPayload {
430
+ /** List of events in the batch */
431
+ events: EventItem[];
432
+ }
433
+ /**
434
+ * Event received from an SSE stream.
435
+ * Matches Python StreamEvent.
436
+ */
437
+ interface StreamEvent {
438
+ /** Event ID */
439
+ id: string;
440
+ /** Sequence ID for ordering */
441
+ sequenceId: number;
442
+ /** Event topic */
443
+ topic: string;
444
+ /** Event type (optional categorization) */
445
+ eventType?: string | undefined;
446
+ /** Event data */
447
+ data: Record<string, unknown>;
448
+ /** Creation timestamp (RFC3339 string) */
449
+ createdAt?: string | undefined;
450
+ }
451
+ /**
452
+ * Represents an event in the event system.
453
+ * Matches Python Event class.
454
+ */
455
+ interface Event {
456
+ /** Unique event identifier */
457
+ id: string;
458
+ /** Global sequence ID for ordering */
459
+ sequenceId: number;
460
+ /** Event topic */
461
+ topic: string;
462
+ /** Event type */
463
+ eventType?: string | undefined;
464
+ /** Event data */
465
+ data?: Record<string, unknown> | undefined;
466
+ /** Event status */
467
+ status: string;
468
+ /** Source workflow execution ID */
469
+ executionId?: string | undefined;
470
+ /** Attempt number */
471
+ attemptNumber: number;
472
+ /** When the event was created */
473
+ createdAt?: string | undefined;
474
+ }
475
+ /**
476
+ * Options for streaming events from a topic.
477
+ */
478
+ interface StreamTopicOptions {
479
+ /** Topic to stream from */
480
+ topic: string;
481
+ /** Start streaming after this sequence ID */
482
+ lastSequenceId?: number;
483
+ /** Start streaming after this timestamp */
484
+ lastTimestamp?: Date;
485
+ }
486
+ /**
487
+ * Options for streaming events from a workflow run.
488
+ */
489
+ interface StreamWorkflowOptions {
490
+ /** Workflow ID (name) */
491
+ workflowId: string;
492
+ /** Workflow run ID (execution ID) */
493
+ workflowRunId: string;
494
+ /** Start streaming after this sequence ID */
495
+ lastSequenceId?: number;
496
+ /** Start streaming after this timestamp */
497
+ lastTimestamp?: Date;
498
+ }
499
+ /**
500
+ * Payload for event-triggered workflows.
501
+ */
502
+ interface EventTriggerPayload {
503
+ /** The events that triggered this execution */
504
+ events: EventItem[];
505
+ /** Batch information */
506
+ batch: {
507
+ /** Batch index (0-based) */
508
+ index: number;
509
+ /** Total events in this batch */
510
+ size: number;
511
+ /** Whether this is the last batch */
512
+ isLast: boolean;
513
+ };
514
+ }
515
+ /**
516
+ * Internal workflow lifecycle events.
517
+ */
518
+ type WorkflowEvent = WorkflowStartEvent | WorkflowFinishEvent | StepStartEvent | StepFinishEvent | TextDeltaEvent | ToolCallEvent;
519
+ interface WorkflowStartEvent {
520
+ type: 'workflow_start';
521
+ _metadata: {
522
+ workflowId: string;
523
+ executionId: string;
524
+ };
525
+ timestamp: Date;
526
+ payload: unknown;
527
+ }
528
+ interface WorkflowFinishEvent {
529
+ type: 'workflow_finish';
530
+ _metadata: {
531
+ workflowId: string;
532
+ executionId: string;
533
+ };
534
+ timestamp: Date;
535
+ result?: unknown;
536
+ error?: string;
537
+ durationMs: number;
538
+ }
539
+ interface StepStartEvent {
540
+ type: 'step_start';
541
+ _metadata: {
542
+ workflowId: string;
543
+ executionId: string;
544
+ };
545
+ stepKey: string;
546
+ timestamp: Date;
547
+ }
548
+ interface StepFinishEvent {
549
+ type: 'step_finish';
550
+ _metadata: {
551
+ workflowId: string;
552
+ executionId: string;
553
+ };
554
+ stepKey: string;
555
+ timestamp: Date;
556
+ result?: unknown;
557
+ error?: string;
558
+ durationMs: number;
559
+ }
560
+ interface TextDeltaEvent {
561
+ type: 'text_delta';
562
+ _metadata: {
563
+ workflowId: string;
564
+ executionId: string;
565
+ };
566
+ step: number;
567
+ chunkIndex: number;
568
+ content?: string;
569
+ timestamp: Date;
570
+ }
571
+ interface ToolCallEvent {
572
+ type: 'tool_call';
573
+ _metadata: {
574
+ workflowId: string;
575
+ executionId: string;
576
+ };
577
+ step: number;
578
+ chunkIndex: number;
579
+ toolCall?: {
580
+ id: string;
581
+ callId?: string;
582
+ type: string;
583
+ function: {
584
+ name: string;
585
+ arguments: string;
586
+ };
587
+ };
588
+ timestamp: Date;
589
+ }
590
+
591
+ /**
592
+ * Channel abstraction for delivering notifications when agents suspend.
593
+ *
594
+ * Channels are registered on the Worker and called automatically when any
595
+ * workflow suspends (e.g., via ask_user or tool approval). Implementations
596
+ * should be stateless and safe to call concurrently.
597
+ */
598
+
599
+ /**
600
+ * Originating channel context — identifies where a trigger came from
601
+ * so that output and notifications can be routed back.
602
+ */
603
+ interface ChannelContext {
604
+ /** Channel type: "slack", "discord", etc. */
605
+ channelId: string;
606
+ /** Channel-specific source metadata (e.g., { channel: "#general", threadTs: "..." }) */
607
+ source: Record<string, unknown>;
608
+ }
609
+ /**
610
+ * Controls how output events are streamed back to the originating channel.
611
+ * - `per_step`: Stream text_delta, tool_call, step_finish, and finish events
612
+ * - `final`: Only stream workflow_finish / agent_finish events
613
+ * - `none`: Do not stream output
614
+ */
615
+ type ChannelOutputMode = 'per_step' | 'final' | 'none';
616
+ /**
617
+ * Data passed to channels when an agent suspends for user input.
618
+ */
619
+ interface SuspendNotification {
620
+ /** Root workflow ID */
621
+ workflowId: string;
622
+ /** Root execution ID */
623
+ executionId: string;
624
+ /** Step key used in suspend() */
625
+ stepKey: string;
626
+ /** URL to the approval page */
627
+ approvalUrl: string;
628
+ /** Title from _form schema */
629
+ title?: string;
630
+ /** Description from _form schema */
631
+ description?: string;
632
+ /** Source: "ask_user", "ask_before_use", or custom */
633
+ source?: string;
634
+ /** Tool name if triggered by ask_before_use */
635
+ tool?: string;
636
+ /** Read-only context data from _form */
637
+ context?: Record<string, unknown>;
638
+ /** Form field definitions from _form.fields */
639
+ formFields?: Record<string, unknown>[];
640
+ /** ISO timestamp when the approval expires */
641
+ expiresAt?: string;
642
+ /** Channel-specific overrides from _notify */
643
+ channelOverrides?: Record<string, unknown>;
644
+ /** Originating channel context — used for thread routing */
645
+ channelContext?: ChannelContext;
646
+ }
647
+ /**
648
+ * A notification channel for delivering suspend notifications to users.
649
+ *
650
+ * Channels are registered on the Worker and called automatically when any
651
+ * workflow suspends. Implementations should be stateless and safe to call
652
+ * concurrently.
653
+ */
654
+ interface Channel {
655
+ /** Unique channel identifier (e.g., "slack", "discord", "email") */
656
+ readonly id: string;
657
+ /**
658
+ * Send a notification when an agent suspends for user input.
659
+ * Implementations should throw on failure — the SDK catches and logs errors.
660
+ */
661
+ notify(notification: SuspendNotification): Promise<void>;
662
+ /** Default output mode for this channel. */
663
+ readonly outputMode?: ChannelOutputMode;
664
+ /** Send output events back to the originating channel. */
665
+ sendOutput?(context: ChannelContext, event: StreamEvent): Promise<void>;
375
666
  }
376
667
 
377
668
  /**
@@ -550,15 +841,17 @@ interface WorkflowConfig<TPayload = unknown, TState = unknown, TResult = unknown
550
841
  /** Batch timeout in seconds for event-triggered workflows */
551
842
  batchTimeoutSeconds?: number | undefined;
552
843
  /** Zod schema for payload validation */
553
- payloadSchema?: ZodSchema<TPayload, ZodTypeDef, unknown> | undefined;
844
+ payloadSchema?: ZodType<TPayload> | undefined;
554
845
  /** Zod schema for state validation and defaults */
555
- stateSchema?: ZodSchema<TState, ZodTypeDef, unknown> | undefined;
846
+ stateSchema?: ZodType<TState> | undefined;
556
847
  /** Zod schema for output/result validation */
557
- outputSchema?: ZodSchema<TResult, ZodTypeDef, unknown> | undefined;
848
+ outputSchema?: ZodType<TResult> | undefined;
558
849
  /** Hook(s) to run before workflow execution (bare function or Hook object) */
559
850
  onStart?: HookHandler<TPayload, TState> | Hook<TPayload, TState> | (HookHandler<TPayload, TState> | Hook<TPayload, TState>)[] | undefined;
560
851
  /** Hook(s) to run after workflow completion (bare function or Hook object) */
561
852
  onEnd?: HookHandler<TPayload, TState> | Hook<TPayload, TState> | (HookHandler<TPayload, TState> | Hook<TPayload, TState>)[] | undefined;
853
+ /** Notification channels for suspend events. Overrides Worker-level channels. */
854
+ channels?: Channel[] | undefined;
562
855
  }
563
856
  /**
564
857
  * Workflow handler function type.
@@ -635,11 +928,11 @@ interface Workflow<TPayload = unknown, TState = unknown, TResult = unknown> {
635
928
  /** Workflow handler function */
636
929
  readonly handler: WorkflowHandler<TPayload, TState, TResult>;
637
930
  /** Payload schema (if provided) */
638
- readonly payloadSchema?: ZodSchema<TPayload, ZodTypeDef, unknown> | undefined;
931
+ readonly payloadSchema?: ZodType<TPayload> | undefined;
639
932
  /** State schema (if provided) */
640
- readonly stateSchema?: ZodSchema<TState, ZodTypeDef, unknown> | undefined;
933
+ readonly stateSchema?: ZodType<TState> | undefined;
641
934
  /** Output schema (if provided) */
642
- readonly outputSchema?: ZodSchema<TResult, ZodTypeDef, unknown> | undefined;
935
+ readonly outputSchema?: ZodType<TResult> | undefined;
643
936
  /**
644
937
  * Run workflow and wait for result (invoke + poll until complete).
645
938
  * Cannot be called from within a workflow; use step.invokeAndWait() instead.
@@ -697,11 +990,11 @@ interface DefineToolConfig<TInput = unknown, TOutput = unknown, TState = unknown
697
990
  /** Tool description shown to LLMs */
698
991
  description: string;
699
992
  /** Zod schema for input validation (optional — some tools take no input) */
700
- inputSchema?: ZodSchema<TInput, ZodTypeDef, unknown> | undefined;
993
+ inputSchema?: ZodType<TInput> | undefined;
701
994
  /** Zod schema for output validation */
702
- outputSchema?: ZodSchema<TOutput, ZodTypeDef, unknown> | undefined;
995
+ outputSchema?: ZodType<TOutput> | undefined;
703
996
  /** Zod schema for state validation and defaults */
704
- stateSchema?: ZodSchema<TState, ZodTypeDef, unknown> | undefined;
997
+ stateSchema?: ZodType<TState> | undefined;
705
998
  /** Queue assignment */
706
999
  queue?: string | QueueConfig$1 | undefined;
707
1000
  /** Hook(s) to run before tool execution */
@@ -712,6 +1005,8 @@ interface DefineToolConfig<TInput = unknown, TOutput = unknown, TState = unknown
712
1005
  autoRegister?: boolean | undefined;
713
1006
  /** Require human approval before tool execution. @default undefined (no approval) */
714
1007
  approval?: ToolApproval | undefined;
1008
+ /** Notification channels for suspend events. Overrides Worker-level channels. */
1009
+ channels?: Channel[] | undefined;
715
1010
  }
716
1011
  /**
717
1012
  * A Workflow with tool-specific LLM metadata.
@@ -854,9 +1149,9 @@ interface Tool<TInput = unknown, TOutput = unknown> {
854
1149
  /** Tool description (shown to LLM) */
855
1150
  description: string;
856
1151
  /** Zod schema for input validation */
857
- inputSchema: ZodSchema<TInput>;
1152
+ inputSchema: ZodType<TInput>;
858
1153
  /** Zod schema for output validation */
859
- outputSchema: ZodSchema<TOutput>;
1154
+ outputSchema: ZodType<TOutput>;
860
1155
  /** Tool handler function */
861
1156
  handler: ToolHandler<TInput, TOutput>;
862
1157
  }
@@ -873,9 +1168,9 @@ interface ToolConfig<TInput = unknown, TOutput = unknown> {
873
1168
  /** Tool description (shown to LLM) */
874
1169
  description: string;
875
1170
  /** Zod schema for input validation */
876
- inputSchema: ZodSchema<TInput>;
1171
+ inputSchema: ZodType<TInput>;
877
1172
  /** Zod schema for output validation */
878
- outputSchema: ZodSchema<TOutput>;
1173
+ outputSchema: ZodType<TOutput>;
879
1174
  }
880
1175
  /**
881
1176
  * Guardrail function type for validating LLM outputs.
@@ -935,7 +1230,7 @@ interface AgentConfig<TOutput = string, TState = unknown> {
935
1230
  /** Queue assignment for execution */
936
1231
  queue?: string | QueueConfig$1;
937
1232
  /** Zod schema for structured output */
938
- outputSchema?: ZodSchema<TOutput>;
1233
+ outputSchema?: ZodType<TOutput>;
939
1234
  /** Lifecycle hooks */
940
1235
  onStart?: Hook<string, TState>;
941
1236
  onEnd?: Hook<string, TState>;
@@ -947,8 +1242,6 @@ interface AgentConfig<TOutput = string, TState = unknown> {
947
1242
  guardrails?: Guardrail$1[];
948
1243
  /** Max retries when guardrail fails */
949
1244
  guardrailMaxRetries?: number;
950
- /** Number of messages to retain for context */
951
- conversationHistory?: number;
952
1245
  }
953
1246
  /**
954
1247
  * Result from running an agent (client-side).
@@ -1044,229 +1337,10 @@ type AgentStreamEvent = {
1044
1337
  };
1045
1338
 
1046
1339
  /**
1047
- * Event-related type definitions.
1048
- *
1049
- * Types match sdk/python/polos/features/events.py exactly.
1340
+ * Type definitions for orchestrator API requests and responses.
1050
1341
  */
1051
1342
  /**
1052
- * Event data structure for publishing events.
1053
- * Matches Python EventData.
1054
- */
1055
- interface EventData {
1056
- /** Type of event */
1057
- eventType?: string | undefined;
1058
- /** Event payload */
1059
- data: Record<string, unknown>;
1060
- }
1061
- /**
1062
- * Event payload received when waiting for events in workflows.
1063
- * Returned by ctx.step.waitForEvent() when an event is received.
1064
- * Matches Python EventPayload.
1065
- */
1066
- interface EventPayload {
1067
- /** Event ID (UUID string) */
1068
- id: string;
1069
- /** Global sequence ID for ordering */
1070
- sequenceId: number;
1071
- /** Event topic */
1072
- topic: string;
1073
- /** Type of event */
1074
- eventType?: string | undefined;
1075
- /** Event payload */
1076
- data: Record<string, unknown>;
1077
- /** Timestamp when event was created */
1078
- createdAt: string;
1079
- }
1080
- /**
1081
- * Single event item in a batch of events.
1082
- * Used in BatchEventPayload for event-triggered workflows with batching.
1083
- * Matches Python EventItem.
1084
- */
1085
- interface EventItem {
1086
- /** Event ID (UUID string) */
1087
- id: string;
1088
- /** Global sequence ID for ordering */
1089
- sequenceId: number;
1090
- /** Event topic */
1091
- topic: string;
1092
- /** Type of event */
1093
- eventType?: string | undefined;
1094
- /** Event payload */
1095
- data: Record<string, unknown>;
1096
- /** Timestamp when event was created */
1097
- createdAt: string;
1098
- }
1099
- /**
1100
- * Batch event payload for event-triggered workflows with batching.
1101
- * Matches Python BatchEventPayload.
1102
- */
1103
- interface BatchEventPayload {
1104
- /** List of events in the batch */
1105
- events: EventItem[];
1106
- }
1107
- /**
1108
- * Event received from an SSE stream.
1109
- * Matches Python StreamEvent.
1110
- */
1111
- interface StreamEvent {
1112
- /** Event ID */
1113
- id: string;
1114
- /** Sequence ID for ordering */
1115
- sequenceId: number;
1116
- /** Event topic */
1117
- topic: string;
1118
- /** Event type (optional categorization) */
1119
- eventType?: string | undefined;
1120
- /** Event data */
1121
- data: Record<string, unknown>;
1122
- /** Creation timestamp (RFC3339 string) */
1123
- createdAt?: string | undefined;
1124
- }
1125
- /**
1126
- * Represents an event in the event system.
1127
- * Matches Python Event class.
1128
- */
1129
- interface Event {
1130
- /** Unique event identifier */
1131
- id: string;
1132
- /** Global sequence ID for ordering */
1133
- sequenceId: number;
1134
- /** Event topic */
1135
- topic: string;
1136
- /** Event type */
1137
- eventType?: string | undefined;
1138
- /** Event data */
1139
- data?: Record<string, unknown> | undefined;
1140
- /** Event status */
1141
- status: string;
1142
- /** Source workflow execution ID */
1143
- executionId?: string | undefined;
1144
- /** Attempt number */
1145
- attemptNumber: number;
1146
- /** When the event was created */
1147
- createdAt?: string | undefined;
1148
- }
1149
- /**
1150
- * Options for streaming events from a topic.
1151
- */
1152
- interface StreamTopicOptions {
1153
- /** Topic to stream from */
1154
- topic: string;
1155
- /** Start streaming after this sequence ID */
1156
- lastSequenceId?: number;
1157
- /** Start streaming after this timestamp */
1158
- lastTimestamp?: Date;
1159
- }
1160
- /**
1161
- * Options for streaming events from a workflow run.
1162
- */
1163
- interface StreamWorkflowOptions {
1164
- /** Workflow ID (name) */
1165
- workflowId: string;
1166
- /** Workflow run ID (execution ID) */
1167
- workflowRunId: string;
1168
- /** Start streaming after this sequence ID */
1169
- lastSequenceId?: number;
1170
- /** Start streaming after this timestamp */
1171
- lastTimestamp?: Date;
1172
- }
1173
- /**
1174
- * Payload for event-triggered workflows.
1175
- */
1176
- interface EventTriggerPayload {
1177
- /** The events that triggered this execution */
1178
- events: EventItem[];
1179
- /** Batch information */
1180
- batch: {
1181
- /** Batch index (0-based) */
1182
- index: number;
1183
- /** Total events in this batch */
1184
- size: number;
1185
- /** Whether this is the last batch */
1186
- isLast: boolean;
1187
- };
1188
- }
1189
- /**
1190
- * Internal workflow lifecycle events.
1191
- */
1192
- type WorkflowEvent = WorkflowStartEvent | WorkflowFinishEvent | StepStartEvent | StepFinishEvent | TextDeltaEvent | ToolCallEvent;
1193
- interface WorkflowStartEvent {
1194
- type: 'workflow_start';
1195
- _metadata: {
1196
- workflowId: string;
1197
- executionId: string;
1198
- };
1199
- timestamp: Date;
1200
- payload: unknown;
1201
- }
1202
- interface WorkflowFinishEvent {
1203
- type: 'workflow_finish';
1204
- _metadata: {
1205
- workflowId: string;
1206
- executionId: string;
1207
- };
1208
- timestamp: Date;
1209
- result?: unknown;
1210
- error?: string;
1211
- durationMs: number;
1212
- }
1213
- interface StepStartEvent {
1214
- type: 'step_start';
1215
- _metadata: {
1216
- workflowId: string;
1217
- executionId: string;
1218
- };
1219
- stepKey: string;
1220
- timestamp: Date;
1221
- }
1222
- interface StepFinishEvent {
1223
- type: 'step_finish';
1224
- _metadata: {
1225
- workflowId: string;
1226
- executionId: string;
1227
- };
1228
- stepKey: string;
1229
- timestamp: Date;
1230
- result?: unknown;
1231
- error?: string;
1232
- durationMs: number;
1233
- }
1234
- interface TextDeltaEvent {
1235
- type: 'text_delta';
1236
- _metadata: {
1237
- workflowId: string;
1238
- executionId: string;
1239
- };
1240
- step: number;
1241
- chunkIndex: number;
1242
- content?: string;
1243
- timestamp: Date;
1244
- }
1245
- interface ToolCallEvent {
1246
- type: 'tool_call';
1247
- _metadata: {
1248
- workflowId: string;
1249
- executionId: string;
1250
- };
1251
- step: number;
1252
- chunkIndex: number;
1253
- toolCall?: {
1254
- id: string;
1255
- callId?: string;
1256
- type: string;
1257
- function: {
1258
- name: string;
1259
- arguments: string;
1260
- };
1261
- };
1262
- timestamp: Date;
1263
- }
1264
-
1265
- /**
1266
- * Type definitions for orchestrator API requests and responses.
1267
- */
1268
- /**
1269
- * Worker capabilities sent during registration.
1343
+ * Worker capabilities sent during registration.
1270
1344
  */
1271
1345
  interface WorkerCapabilities {
1272
1346
  runtime: 'typescript' | 'python';
@@ -1362,6 +1436,10 @@ interface ExecutionContext {
1362
1436
  initialState?: Record<string, unknown> | undefined;
1363
1437
  runTimeoutSeconds?: number | undefined;
1364
1438
  createdAt?: string | undefined;
1439
+ channelContext?: {
1440
+ channelId: string;
1441
+ source: Record<string, unknown>;
1442
+ } | undefined;
1365
1443
  }
1366
1444
  /**
1367
1445
  * Request to complete an execution.
@@ -1513,6 +1591,10 @@ interface InvokeWorkflowRequest {
1513
1591
  otelTraceparent?: string | undefined;
1514
1592
  initialState?: Record<string, unknown> | undefined;
1515
1593
  runTimeoutSeconds?: number | undefined;
1594
+ channelContext?: {
1595
+ channelId: string;
1596
+ source: Record<string, unknown>;
1597
+ } | undefined;
1516
1598
  }
1517
1599
  /**
1518
1600
  * Invoke workflow response.
@@ -1609,6 +1691,20 @@ interface ConversationMessage {
1609
1691
  role: string;
1610
1692
  content: unknown;
1611
1693
  }
1694
+ /**
1695
+ * Response from GET /internal/session/{sessionId}/memory.
1696
+ */
1697
+ interface SessionMemoryResponse {
1698
+ summary: string | null;
1699
+ messages: ConversationMessage[];
1700
+ }
1701
+ /**
1702
+ * Request body for PUT /internal/session/{sessionId}/memory.
1703
+ */
1704
+ interface PutSessionMemoryRequest {
1705
+ summary: string | null;
1706
+ messages: ConversationMessage[];
1707
+ }
1612
1708
 
1613
1709
  /**
1614
1710
  * HTTP client for orchestrator API communication.
@@ -1667,6 +1763,11 @@ declare class OrchestratorClient {
1667
1763
  * Register a worker with the orchestrator.
1668
1764
  */
1669
1765
  registerWorker(request: RegisterWorkerRequest): Promise<RegisterWorkerResponse>;
1766
+ /**
1767
+ * Get active worker IDs for the current project.
1768
+ * Uses the X-Project-ID header already set on all requests.
1769
+ */
1770
+ getActiveWorkerIds(): Promise<string[]>;
1670
1771
  /**
1671
1772
  * Register a deployment.
1672
1773
  */
@@ -1774,6 +1875,16 @@ declare class OrchestratorClient {
1774
1875
  * GET /api/v1/conversation/{conversationId}/get
1775
1876
  */
1776
1877
  getConversationHistory(conversationId: string, params: GetConversationHistoryParams): Promise<ConversationMessage[]>;
1878
+ /**
1879
+ * Get session memory (summary + metadata).
1880
+ * GET /internal/session/{sessionId}/memory
1881
+ */
1882
+ getSessionMemory(sessionId: string): Promise<SessionMemoryResponse>;
1883
+ /**
1884
+ * Store session memory (summary + messages).
1885
+ * PUT /internal/session/{sessionId}/memory
1886
+ */
1887
+ putSessionMemory(sessionId: string, request: PutSessionMemoryRequest): Promise<void>;
1777
1888
  /**
1778
1889
  * Create or update a schedule. Returns schedule_id.
1779
1890
  */
@@ -1898,6 +2009,11 @@ interface ClientInvokeOptions {
1898
2009
  rootExecutionId?: string | undefined;
1899
2010
  /** Step key (when invoked from a step) */
1900
2011
  stepKey?: string | undefined;
2012
+ /** Channel context for bidirectional channels (e.g., originating Slack thread) */
2013
+ channelContext?: {
2014
+ channelId: string;
2015
+ source: Record<string, unknown>;
2016
+ } | undefined;
1901
2017
  }
1902
2018
  /**
1903
2019
  * Input for batch workflow invocation via PolosClient.
@@ -2100,46 +2216,161 @@ declare class PolosClient {
2100
2216
  }
2101
2217
 
2102
2218
  /**
2103
- * State management for workflows.
2104
- *
2105
- * Handles state initialization, validation, and serialization using Zod schemas.
2219
+ * Unified Polos class that combines PolosClient (submit/stream work) and Worker
2220
+ * (receive/execute work) into a single object.
2106
2221
  */
2107
2222
 
2108
2223
  /**
2109
- * Error thrown when state validation fails.
2110
- */
2111
- declare class StateValidationError extends Error {
2112
- readonly issues: unknown[];
2113
- constructor(message: string, issues: unknown[]);
2114
- }
2115
- /**
2116
- * Error thrown when state exceeds size limit.
2224
+ * Configuration for the unified Polos class.
2225
+ * All fields are optional — defaults come from environment variables.
2117
2226
  */
2118
- declare class StateSizeError extends Error {
2119
- readonly size: number;
2120
- readonly maxSize: number;
2121
- constructor(size: number, maxSize: number);
2227
+ interface PolosConfig {
2228
+ /** Project ID (default: POLOS_PROJECT_ID env var) */
2229
+ projectId?: string | undefined;
2230
+ /** Orchestrator API URL (default: POLOS_API_URL or http://localhost:8080) */
2231
+ apiUrl?: string | undefined;
2232
+ /** API key (default: POLOS_API_KEY env var) */
2233
+ apiKey?: string | undefined;
2234
+ /** Deployment ID (default: POLOS_DEPLOYMENT_ID or "default") */
2235
+ deploymentId?: string | undefined;
2236
+ /** Worker server port (default: 8000) */
2237
+ port?: number | undefined;
2238
+ /** Maximum concurrent workflow executions */
2239
+ maxConcurrentWorkflows?: number | undefined;
2240
+ /** Notification channels for suspend events */
2241
+ channels?: Channel[] | undefined;
2242
+ /** Path to a log file. When set, SDK logs are written here instead of stdout. */
2243
+ logFile?: string | undefined;
2122
2244
  }
2123
2245
  /**
2124
- * Initialize state from a Zod schema.
2125
- * Uses schema defaults to create the initial state.
2126
- */
2127
- declare function initializeState<TState>(schema: ZodSchema<TState, ZodTypeDef, unknown>): TState;
2128
- /**
2129
- * Validate state against a Zod schema.
2130
- */
2131
- declare function validateState<TState>(state: unknown, schema: ZodSchema<TState, ZodTypeDef, unknown>): TState;
2132
-
2133
- /**
2134
- * Workflow registry for tracking registered workflows.
2246
+ * Unified Polos client + worker.
2135
2247
  *
2136
- * Used by Worker to find workflows to execute based on workflow ID.
2137
- */
2138
-
2139
- /**
2140
- * Error thrown when a workflow is not found in the registry.
2141
- */
2142
- declare class WorkflowNotFoundError extends Error {
2248
+ * @example
2249
+ * ```typescript
2250
+ * import { Polos, defineAgent } from '@polos/sdk';
2251
+ * import { openai } from '@ai-sdk/openai';
2252
+ *
2253
+ * const agent = defineAgent({
2254
+ * id: 'weather',
2255
+ * model: openai('gpt-4o'),
2256
+ * systemPrompt: 'You are a weather assistant.',
2257
+ * });
2258
+ *
2259
+ * const polos = new Polos();
2260
+ * await polos.start();
2261
+ * const result = await agent.run(polos, { input: "What's the weather?" });
2262
+ * await polos.stop();
2263
+ * ```
2264
+ */
2265
+ declare class Polos implements WorkflowRunClient {
2266
+ private readonly client;
2267
+ private readonly worker;
2268
+ private started;
2269
+ private serverPromise;
2270
+ /**
2271
+ * Events API for publishing and subscribing to events.
2272
+ */
2273
+ readonly events: EventsApi;
2274
+ /**
2275
+ * Schedules API for managing workflow schedules.
2276
+ */
2277
+ readonly schedules: SchedulesApi;
2278
+ constructor(config?: PolosConfig);
2279
+ /**
2280
+ * Start the worker in background (non-blocking).
2281
+ *
2282
+ * Registers with orchestrator, starts worker server, begins heartbeat.
2283
+ * Returns once registration is complete so the caller can immediately
2284
+ * invoke workflows.
2285
+ */
2286
+ start(): Promise<void>;
2287
+ /**
2288
+ * Start the worker and block until shutdown signal (SIGINT/SIGTERM).
2289
+ *
2290
+ * This is the deployment mode — equivalent to Worker.run().
2291
+ * Use for servers, Kubernetes, Docker, etc.
2292
+ */
2293
+ serve(): Promise<void>;
2294
+ /**
2295
+ * Gracefully stop the worker and clean up.
2296
+ */
2297
+ stop(): Promise<void>;
2298
+ /**
2299
+ * Get the underlying PolosClient.
2300
+ * Needed for APIs that require PolosClient explicitly (e.g., agent.stream()).
2301
+ */
2302
+ getClient(): PolosClient;
2303
+ /**
2304
+ * Invoke a workflow (fire and forget).
2305
+ */
2306
+ invoke(workflow: string | Workflow, payload?: unknown, options?: ClientInvokeOptions): Promise<ExecutionHandle>;
2307
+ /**
2308
+ * Invoke multiple workflows in batch.
2309
+ */
2310
+ batchInvoke(items: ClientBatchWorkflowInput[], options?: {
2311
+ sessionId?: string;
2312
+ userId?: string;
2313
+ parentExecutionId?: string;
2314
+ rootWorkflowId?: string;
2315
+ rootExecutionId?: string;
2316
+ stepKey?: string;
2317
+ waitForSubworkflow?: boolean;
2318
+ }): Promise<ExecutionHandle[]>;
2319
+ /**
2320
+ * Resume a suspended execution.
2321
+ */
2322
+ resume(suspendWorkflowId: string, suspendExecutionId: string, suspendStepKey: string, data: unknown): Promise<void>;
2323
+ /**
2324
+ * Get execution details.
2325
+ */
2326
+ getExecution(executionId: string): Promise<GetExecutionResponse>;
2327
+ /**
2328
+ * Cancel an execution.
2329
+ */
2330
+ cancelExecution(executionId: string): Promise<boolean>;
2331
+ }
2332
+
2333
+ /**
2334
+ * State management for workflows.
2335
+ *
2336
+ * Handles state initialization, validation, and serialization using Zod schemas.
2337
+ */
2338
+
2339
+ /**
2340
+ * Error thrown when state validation fails.
2341
+ */
2342
+ declare class StateValidationError extends Error {
2343
+ readonly issues: unknown[];
2344
+ constructor(message: string, issues: unknown[]);
2345
+ }
2346
+ /**
2347
+ * Error thrown when state exceeds size limit.
2348
+ */
2349
+ declare class StateSizeError extends Error {
2350
+ readonly size: number;
2351
+ readonly maxSize: number;
2352
+ constructor(size: number, maxSize: number);
2353
+ }
2354
+ /**
2355
+ * Initialize state from a Zod schema.
2356
+ * Uses schema defaults to create the initial state.
2357
+ */
2358
+ declare function initializeState<TState>(schema: ZodType<TState>): TState;
2359
+ /**
2360
+ * Validate state against a Zod schema.
2361
+ */
2362
+ declare function validateState<TState>(state: unknown, schema: ZodType<TState>): TState;
2363
+
2364
+ /**
2365
+ * Workflow registry for tracking registered workflows.
2366
+ *
2367
+ * Used by Worker to find workflows to execute based on workflow ID.
2368
+ */
2369
+
2370
+ /**
2371
+ * Error thrown when a workflow is not found in the registry.
2372
+ */
2373
+ declare class WorkflowNotFoundError extends Error {
2143
2374
  readonly workflowId: string;
2144
2375
  constructor(workflowId: string);
2145
2376
  }
@@ -2155,8 +2386,8 @@ declare class DuplicateWorkflowError extends Error {
2155
2386
  */
2156
2387
  interface WorkflowRegistry {
2157
2388
  /**
2158
- * Register a workflow.
2159
- * @throws DuplicateWorkflowError if workflow ID is already registered
2389
+ * Register a workflow. If a workflow with the same ID is already
2390
+ * registered, it is silently replaced.
2160
2391
  */
2161
2392
  register(workflow: Workflow): void;
2162
2393
  /**
@@ -2189,10 +2420,6 @@ interface WorkflowRegistry {
2189
2420
  * Create a workflow registry.
2190
2421
  */
2191
2422
  declare function createWorkflowRegistry(): WorkflowRegistry;
2192
- /**
2193
- * Global workflow registry instance.
2194
- * Used for automatic workflow registration when using defineWorkflow.
2195
- */
2196
2423
  declare const globalRegistry: WorkflowRegistry;
2197
2424
 
2198
2425
  /**
@@ -2245,6 +2472,380 @@ interface QueueConfig {
2245
2472
  concurrencyLimit?: number | undefined;
2246
2473
  }
2247
2474
 
2475
+ /**
2476
+ * Shared types for the execution framework.
2477
+ *
2478
+ * Defines interfaces for execution environments, command results,
2479
+ * file operations, and configuration.
2480
+ */
2481
+ /**
2482
+ * Abstract interface for an execution environment (Docker, E2B, Local).
2483
+ * All sandbox tools operate against this interface.
2484
+ */
2485
+ interface ExecutionEnvironment {
2486
+ /** Environment type discriminator */
2487
+ readonly type: 'local' | 'docker' | 'e2b';
2488
+ /** Execute a shell command in the environment */
2489
+ exec(command: string, opts?: ExecOptions): Promise<ExecResult>;
2490
+ /** Read a file's contents as UTF-8 text */
2491
+ readFile(path: string): Promise<string>;
2492
+ /** Write content to a file, creating parent directories as needed */
2493
+ writeFile(path: string, content: string): Promise<void>;
2494
+ /** Check whether a file exists */
2495
+ fileExists(path: string): Promise<boolean>;
2496
+ /** Find files matching a glob pattern */
2497
+ glob(pattern: string, opts?: GlobOptions): Promise<string[]>;
2498
+ /** Search file contents for a pattern */
2499
+ grep(pattern: string, opts?: GrepOptions): Promise<GrepMatch[]>;
2500
+ /** Initialize the environment (create container, connect to sandbox, etc.) */
2501
+ initialize(): Promise<void>;
2502
+ /** Tear down the environment (remove container, kill sandbox, etc.) */
2503
+ destroy(): Promise<void>;
2504
+ /** Get the current working directory inside the environment */
2505
+ getCwd(): string;
2506
+ /** Get environment metadata */
2507
+ getInfo(): EnvironmentInfo;
2508
+ }
2509
+ /**
2510
+ * Options for command execution.
2511
+ */
2512
+ interface ExecOptions {
2513
+ /** Working directory for the command */
2514
+ cwd?: string | undefined;
2515
+ /** Environment variables to set */
2516
+ env?: Record<string, string> | undefined;
2517
+ /** Timeout in seconds (default: 300) */
2518
+ timeout?: number | undefined;
2519
+ /** Data to pipe to stdin */
2520
+ stdin?: string | undefined;
2521
+ }
2522
+ /**
2523
+ * Result of a command execution.
2524
+ */
2525
+ interface ExecResult {
2526
+ /** Process exit code (0 = success) */
2527
+ exitCode: number;
2528
+ /** Standard output */
2529
+ stdout: string;
2530
+ /** Standard error */
2531
+ stderr: string;
2532
+ /** Execution duration in milliseconds */
2533
+ durationMs: number;
2534
+ /** Whether output was truncated due to size limits */
2535
+ truncated: boolean;
2536
+ }
2537
+ /**
2538
+ * Options for glob file search.
2539
+ */
2540
+ interface GlobOptions {
2541
+ /** Working directory for the search */
2542
+ cwd?: string | undefined;
2543
+ /** Glob patterns to exclude */
2544
+ ignore?: string[] | undefined;
2545
+ }
2546
+ /**
2547
+ * Options for grep content search.
2548
+ */
2549
+ interface GrepOptions {
2550
+ /** Working directory for the search */
2551
+ cwd?: string | undefined;
2552
+ /** File glob patterns to include (e.g., "*.ts") */
2553
+ include?: string[] | undefined;
2554
+ /** Maximum number of matches to return */
2555
+ maxResults?: number | undefined;
2556
+ /** Number of context lines around each match */
2557
+ contextLines?: number | undefined;
2558
+ }
2559
+ /**
2560
+ * A single grep match result.
2561
+ */
2562
+ interface GrepMatch {
2563
+ /** File path (relative to search root) */
2564
+ path: string;
2565
+ /** Line number of the match */
2566
+ line: number;
2567
+ /** The matching line text */
2568
+ text: string;
2569
+ /** Context lines around the match */
2570
+ context?: string | undefined;
2571
+ }
2572
+ /**
2573
+ * Metadata about an execution environment.
2574
+ */
2575
+ interface EnvironmentInfo {
2576
+ /** Environment type */
2577
+ type: 'local' | 'docker' | 'e2b';
2578
+ /** Current working directory */
2579
+ cwd: string;
2580
+ /** Sandbox/container identifier (container ID for Docker, sandbox ID for E2B) */
2581
+ sandboxId?: string | undefined;
2582
+ /** Operating system info */
2583
+ os?: string | undefined;
2584
+ }
2585
+ /**
2586
+ * Configuration for a Docker execution environment.
2587
+ */
2588
+ interface DockerEnvironmentConfig {
2589
+ /** Docker image to use (e.g., "node:20-slim") */
2590
+ image: string;
2591
+ /** Host directory to mount as workspace */
2592
+ workspaceDir: string;
2593
+ /** Working directory inside the container (default: "/workspace") */
2594
+ containerWorkdir?: string | undefined;
2595
+ /** Environment variables to set in the container */
2596
+ env?: Record<string, string> | undefined;
2597
+ /** Memory limit (e.g., "512m", "2g") */
2598
+ memory?: string | undefined;
2599
+ /** CPU limit (e.g., "1", "0.5") */
2600
+ cpus?: string | undefined;
2601
+ /** Network mode (default: "none") */
2602
+ network?: string | undefined;
2603
+ /** Command to run after container creation (e.g., "npm install") */
2604
+ setupCommand?: string | undefined;
2605
+ }
2606
+ /**
2607
+ * Configuration for an E2B execution environment.
2608
+ */
2609
+ interface E2BEnvironmentConfig {
2610
+ /** E2B template name (default: "base") */
2611
+ template?: string | undefined;
2612
+ /** E2B API key (defaults to E2B_API_KEY env var) */
2613
+ apiKey?: string | undefined;
2614
+ /** Sandbox timeout in seconds (default: 3600) */
2615
+ timeout?: number | undefined;
2616
+ /** Working directory inside the sandbox */
2617
+ cwd?: string | undefined;
2618
+ /** Environment variables */
2619
+ env?: Record<string, string> | undefined;
2620
+ /** Setup command to run after sandbox creation */
2621
+ setupCommand?: string | undefined;
2622
+ }
2623
+ /**
2624
+ * Configuration for a local execution environment.
2625
+ */
2626
+ interface LocalEnvironmentConfig {
2627
+ /** Working directory (default: auto-provisioned workspace) */
2628
+ cwd?: string | undefined;
2629
+ /**
2630
+ * Restrict file operations to this directory.
2631
+ * Defaults to `cwd` when running inside a managed sandbox.
2632
+ * Set to `false` to explicitly disable path restriction.
2633
+ */
2634
+ pathRestriction?: string | false | undefined;
2635
+ }
2636
+ /**
2637
+ * Configuration for the exec tool's security and behavior.
2638
+ */
2639
+ interface ExecToolConfig {
2640
+ /** Security mode: allow-always (default), allowlist, or always require approval */
2641
+ security?: 'allow-always' | 'allowlist' | 'approval-always' | undefined;
2642
+ /** Allowed command patterns (for allowlist mode) */
2643
+ allowlist?: string[] | undefined;
2644
+ /** Default command timeout in seconds (default: 300) */
2645
+ timeout?: number | undefined;
2646
+ /** Maximum output characters before truncation (default: 100000) */
2647
+ maxOutputChars?: number | undefined;
2648
+ }
2649
+ /**
2650
+ * Sandbox lifecycle scope.
2651
+ *
2652
+ * - `'execution'` — sandbox created on first tool use, destroyed when execution completes.
2653
+ * - `'session'` — sandbox persists across executions sharing the same sessionId.
2654
+ */
2655
+ type SandboxScope = 'execution' | 'session';
2656
+ /**
2657
+ * Configuration for a managed sandbox.
2658
+ */
2659
+ interface SandboxConfig {
2660
+ /** Custom sandbox ID. Auto-generated if omitted. */
2661
+ id?: string | undefined;
2662
+ /** Lifecycle scope. Default: 'execution'. */
2663
+ scope?: SandboxScope | undefined;
2664
+ /** Environment type. Default: 'docker'. */
2665
+ env?: 'local' | 'docker' | 'e2b' | undefined;
2666
+ /** Docker environment configuration. */
2667
+ docker?: DockerEnvironmentConfig | undefined;
2668
+ /** E2B environment configuration. */
2669
+ e2b?: E2BEnvironmentConfig | undefined;
2670
+ /** Local environment configuration. */
2671
+ local?: LocalEnvironmentConfig | undefined;
2672
+ /** Exec tool configuration (security, timeout, etc). */
2673
+ exec?: ExecToolConfig | undefined;
2674
+ /** Approval mode for file-mutating tools (write, edit). Defaults to 'always' for local env. */
2675
+ fileApproval?: 'always' | 'none' | undefined;
2676
+ /**
2677
+ * Idle destruction timeout for session-scoped sandboxes.
2678
+ * Container destroyed after this duration of no tool calls.
2679
+ * Default: '1h'. Only applies to scope: 'session'.
2680
+ * Examples: '1h', '24h', '3d'.
2681
+ */
2682
+ idleDestroyTimeout?: string | undefined;
2683
+ }
2684
+ /**
2685
+ * Configuration for the sandboxTools() factory.
2686
+ */
2687
+ interface SandboxToolsConfig extends SandboxConfig {
2688
+ /** Working directory override */
2689
+ cwd?: string | undefined;
2690
+ /** Subset of tools to include (default: all) */
2691
+ tools?: ('exec' | 'read' | 'write' | 'edit' | 'glob' | 'grep')[] | undefined;
2692
+ }
2693
+
2694
+ /**
2695
+ * Managed sandbox — wraps an ExecutionEnvironment with identity,
2696
+ * lifecycle tracking, and crash recovery.
2697
+ */
2698
+
2699
+ /**
2700
+ * A managed sandbox wrapping an ExecutionEnvironment.
2701
+ */
2702
+ interface Sandbox {
2703
+ readonly id: string;
2704
+ readonly scope: SandboxScope;
2705
+ readonly config: SandboxConfig;
2706
+ readonly workerId: string;
2707
+ readonly sessionId?: string | undefined;
2708
+ readonly activeExecutionIds: ReadonlySet<string>;
2709
+ readonly initialized: boolean;
2710
+ readonly destroyed: boolean;
2711
+ readonly lastActivityAt: Date;
2712
+ /** Get or lazily initialize the environment. Updates lastActivityAt. */
2713
+ getEnvironment(): Promise<ExecutionEnvironment>;
2714
+ /** Record that an execution is using this sandbox. */
2715
+ attachExecution(executionId: string): void;
2716
+ /** Record that an execution is done with this sandbox. */
2717
+ detachExecution(executionId: string): void;
2718
+ /** Destroy the sandbox. Safe to call multiple times. */
2719
+ destroy(): Promise<void>;
2720
+ /** Recreate after container crash. Filesystem survives via bind mount. */
2721
+ recreate(): Promise<void>;
2722
+ }
2723
+ /**
2724
+ * Concrete implementation of the Sandbox interface.
2725
+ */
2726
+ declare class ManagedSandbox implements Sandbox {
2727
+ readonly id: string;
2728
+ readonly scope: SandboxScope;
2729
+ readonly config: SandboxConfig;
2730
+ private _workerId;
2731
+ readonly projectId: string;
2732
+ readonly sessionId?: string | undefined;
2733
+ private readonly _activeExecutionIds;
2734
+ private _lastActivityAt;
2735
+ private _destroyed;
2736
+ private _env;
2737
+ private _envPromise;
2738
+ private _lastHealthCheckAt;
2739
+ constructor(config: SandboxConfig, workerId: string, projectId: string, sessionId?: string);
2740
+ get workerId(): string;
2741
+ get activeExecutionIds(): ReadonlySet<string>;
2742
+ get initialized(): boolean;
2743
+ get destroyed(): boolean;
2744
+ get lastActivityAt(): Date;
2745
+ getEnvironment(): Promise<ExecutionEnvironment>;
2746
+ attachExecution(executionId: string): void;
2747
+ detachExecution(executionId: string): void;
2748
+ destroy(): Promise<void>;
2749
+ recreate(): Promise<void>;
2750
+ /**
2751
+ * Create the execution environment based on config.
2752
+ */
2753
+ /**
2754
+ * Compute the default workspace directory for Docker.
2755
+ * Uses `POLOS_WORKSPACES_DIR/{projectId}/{sessionId || sandboxId}`.
2756
+ */
2757
+ private _getDefaultWorkspaceDir;
2758
+ private _initializeEnvironment;
2759
+ /**
2760
+ * Health check with 30s debounce. Only probes Docker containers.
2761
+ */
2762
+ private _healthCheck;
2763
+ }
2764
+
2765
+ /**
2766
+ * SandboxManager — manages sandbox creation, reuse, auto-cleanup,
2767
+ * and orphan detection. Lives on the Worker.
2768
+ */
2769
+
2770
+ /**
2771
+ * Manages sandbox lifecycle across executions.
2772
+ */
2773
+ declare class SandboxManager {
2774
+ private _workerId;
2775
+ private _projectId;
2776
+ private readonly _orchestratorClient;
2777
+ private readonly sandboxes;
2778
+ private readonly sessionSandboxes;
2779
+ private readonly sessionCreationLocks;
2780
+ private sweepTimer;
2781
+ constructor(workerId: string, projectId: string, orchestratorClient?: OrchestratorClient);
2782
+ /**
2783
+ * Update the worker ID (called after registration or re-registration).
2784
+ */
2785
+ setWorkerId(workerId: string): void;
2786
+ /**
2787
+ * Create or retrieve a sandbox.
2788
+ *
2789
+ * - Session-scoped: returns existing sandbox for the session if available.
2790
+ * - Execution-scoped: always creates a new sandbox.
2791
+ */
2792
+ getOrCreateSandbox(config: SandboxConfig, ctx: {
2793
+ executionId: string;
2794
+ sessionId?: string | undefined;
2795
+ }): Promise<Sandbox>;
2796
+ /**
2797
+ * Notify that an execution completed. Triggers cleanup for execution-scoped sandboxes.
2798
+ */
2799
+ onExecutionComplete(executionId: string): Promise<void>;
2800
+ /**
2801
+ * Destroy a specific sandbox by ID.
2802
+ */
2803
+ destroySandbox(sandboxId: string): Promise<void>;
2804
+ /**
2805
+ * Destroy all managed sandboxes. Called during worker shutdown.
2806
+ */
2807
+ destroyAll(): Promise<void>;
2808
+ /**
2809
+ * Start periodic sweep. Each cycle:
2810
+ * 1. Destroys own sandboxes idle past their idleDestroyTimeout.
2811
+ * 2. Removes orphan Docker containers from dead workers (orchestrator-based).
2812
+ */
2813
+ startSweep(intervalMs?: number): void;
2814
+ /**
2815
+ * Stop the periodic sweep.
2816
+ */
2817
+ stopSweep(): void;
2818
+ /**
2819
+ * Lookup a sandbox by ID.
2820
+ */
2821
+ getSandbox(sandboxId: string): Sandbox | undefined;
2822
+ /**
2823
+ * Lookup a session sandbox by session ID.
2824
+ */
2825
+ getSessionSandbox(sessionId: string): Sandbox | undefined;
2826
+ private _createExecutionSandbox;
2827
+ private _createSessionSandbox;
2828
+ private _destroyAndRemove;
2829
+ /**
2830
+ * Unified sweep: Phase 1 cleans own idle sandboxes, Phase 2 cleans orphan containers.
2831
+ */
2832
+ private _sweep;
2833
+ /**
2834
+ * Phase 1: Destroy own sandboxes that have been idle past their timeout.
2835
+ */
2836
+ private _sweepIdleSandboxes;
2837
+ /**
2838
+ * Phase 2: Remove Docker containers from dead workers.
2839
+ *
2840
+ * Queries the orchestrator for active worker IDs, lists all polos-managed
2841
+ * Docker containers, and removes any whose worker-id is not in the active set
2842
+ * AND whose age exceeds ORPHAN_GRACE_PERIOD_MS.
2843
+ *
2844
+ * If the orchestrator is unavailable, this phase is skipped entirely.
2845
+ */
2846
+ private _sweepOrphanContainers;
2847
+ }
2848
+
2248
2849
  /**
2249
2850
  * Worker class for executing Polos workflows.
2250
2851
  *
@@ -2278,6 +2879,8 @@ interface WorkerConfig {
2278
2879
  localMode?: boolean | undefined;
2279
2880
  /** Request timeout in milliseconds (default: 30000) */
2280
2881
  timeout?: number | undefined;
2882
+ /** Default notification channels for suspend events (e.g., Slack, Discord) */
2883
+ channels?: Channel[] | undefined;
2281
2884
  }
2282
2885
  /**
2283
2886
  * Worker state.
@@ -2313,9 +2916,12 @@ declare class Worker {
2313
2916
  private readonly config;
2314
2917
  private readonly orchestratorClient;
2315
2918
  private readonly workflowRegistry;
2919
+ private readonly channels;
2316
2920
  private readonly maxConcurrentWorkflows;
2317
2921
  private readonly workerServerUrl;
2318
2922
  private readonly port;
2923
+ /** Sandbox manager for managed sandbox lifecycle. */
2924
+ readonly sandboxManager: SandboxManager;
2319
2925
  private workerId;
2320
2926
  private workerServer;
2321
2927
  private state;
@@ -2343,6 +2949,16 @@ declare class Worker {
2343
2949
  * Run the worker (blocks until shutdown).
2344
2950
  */
2345
2951
  run(): Promise<void>;
2952
+ /**
2953
+ * Phase 1: Initialize tracing and register with orchestrator.
2954
+ * @internal
2955
+ */
2956
+ registerAll(): Promise<void>;
2957
+ /**
2958
+ * Phase 2: Start worker server, heartbeat, and signal handlers (blocks until shutdown).
2959
+ * @internal
2960
+ */
2961
+ runServer(): Promise<void>;
2346
2962
  /**
2347
2963
  * Gracefully shutdown the worker.
2348
2964
  */
@@ -2434,7 +3050,11 @@ declare class Worker {
2434
3050
  /**
2435
3051
  * Confirm cancellation.
2436
3052
  */
2437
- private confirmCancellation;
3053
+ private confirmCancellation;
3054
+ /**
3055
+ * Start a channel bridge that streams execution events back to the originating channel.
3056
+ */
3057
+ private startChannelBridge;
2438
3058
  }
2439
3059
 
2440
3060
  /**
@@ -2462,6 +3082,10 @@ interface WorkerExecutionData {
2462
3082
  otelSpanId?: string | undefined;
2463
3083
  initialState?: Record<string, unknown> | undefined;
2464
3084
  runTimeoutSeconds?: number | undefined;
3085
+ channelContext?: {
3086
+ channelId: string;
3087
+ source: Record<string, unknown>;
3088
+ } | undefined;
2465
3089
  }
2466
3090
  /**
2467
3091
  * Callback for handling received work.
@@ -2552,6 +3176,15 @@ interface ExecuteWorkflowOptions {
2552
3176
  workerId: string;
2553
3177
  /** Abort signal for cancellation */
2554
3178
  abortSignal?: AbortSignal | undefined;
3179
+ /** Notification channels for suspend events */
3180
+ channels?: Channel[] | undefined;
3181
+ /** Sandbox manager for managed sandbox lifecycle */
3182
+ sandboxManager?: SandboxManager | undefined;
3183
+ /** Originating channel context for bidirectional channels */
3184
+ channelContext?: {
3185
+ channelId: string;
3186
+ source: Record<string, unknown>;
3187
+ } | undefined;
2555
3188
  }
2556
3189
  /**
2557
3190
  * Result of workflow execution.
@@ -3006,6 +3639,8 @@ interface LLMUsage {
3006
3639
  input_tokens: number;
3007
3640
  output_tokens: number;
3008
3641
  total_tokens: number;
3642
+ cache_read_input_tokens?: number | undefined;
3643
+ cache_creation_input_tokens?: number | undefined;
3009
3644
  }
3010
3645
  /**
3011
3646
  * A tool call made by the LLM
@@ -3148,6 +3783,10 @@ declare function convertVercelUsageToPython(usage: {
3148
3783
  inputTokens: number | undefined;
3149
3784
  outputTokens: number | undefined;
3150
3785
  totalTokens?: number | undefined;
3786
+ inputTokenDetails?: {
3787
+ cacheReadTokens?: number | undefined;
3788
+ cacheWriteTokens?: number | undefined;
3789
+ };
3151
3790
  }): LLMUsage;
3152
3791
  /**
3153
3792
  * Convert Vercel finish reason (kebab-case) to Python format (snake_case).
@@ -3346,6 +3985,64 @@ declare const hasText: StopConditionFactory<{
3346
3985
  texts: string[];
3347
3986
  }>;
3348
3987
 
3988
+ /**
3989
+ * Types for the session compaction memory system.
3990
+ *
3991
+ * Two-tier memory:
3992
+ * - Tier 1: Rolling summary of older messages (compacted via LLM)
3993
+ * - Tier 2: Recent raw messages kept verbatim
3994
+ */
3995
+
3996
+ /**
3997
+ * Full session memory state.
3998
+ */
3999
+ interface SessionMemory {
4000
+ /** Compacted summary of older messages, or null if no compaction has occurred */
4001
+ summary: string | null;
4002
+ /** Recent messages kept verbatim */
4003
+ messages: ConversationMessage[];
4004
+ }
4005
+ /**
4006
+ * User-facing compaction configuration.
4007
+ */
4008
+ interface CompactionConfig {
4009
+ /** Maximum total conversation tokens before compaction triggers (default: 80000) */
4010
+ maxConversationTokens?: number | undefined;
4011
+ /** Maximum tokens for the summary (default: 20000) */
4012
+ maxSummaryTokens?: number | undefined;
4013
+ /** Minimum recent messages to always keep verbatim (default: 4) */
4014
+ minRecentMessages?: number | undefined;
4015
+ /** Model to use for compaction (default: agent's own model) */
4016
+ compactionModel?: LanguageModel | undefined;
4017
+ /** Whether compaction is enabled (default: true) */
4018
+ enabled?: boolean | undefined;
4019
+ }
4020
+ /**
4021
+ * Internal — all fields resolved to concrete values.
4022
+ */
4023
+ interface NormalizedCompactionConfig {
4024
+ maxConversationTokens: number;
4025
+ maxSummaryTokens: number;
4026
+ minRecentMessages: number;
4027
+ compactionModel: LanguageModel;
4028
+ enabled: boolean;
4029
+ }
4030
+ /**
4031
+ * Result from compactIfNeeded.
4032
+ */
4033
+ interface CompactionResult {
4034
+ /** Whether compaction was performed */
4035
+ compacted: boolean;
4036
+ /** The (possibly shortened) messages array */
4037
+ messages: ConversationMessage[];
4038
+ /** The current summary text (null if no summary) */
4039
+ summary: string | null;
4040
+ /** Estimated token count of the summary */
4041
+ summaryTokens: number;
4042
+ /** Total conversation turns */
4043
+ totalTurns: number;
4044
+ }
4045
+
3349
4046
  /**
3350
4047
  * Agent stream function — core execution loop.
3351
4048
  *
@@ -3372,7 +4069,6 @@ interface AgentStreamPayload {
3372
4069
  agent_config: AgentStreamConfig;
3373
4070
  input: string | Record<string, unknown>[];
3374
4071
  streaming: boolean;
3375
- conversation_id?: string | undefined;
3376
4072
  }
3377
4073
  /**
3378
4074
  * Definition of an agent for the stream function.
@@ -3390,7 +4086,8 @@ interface AgentDefinition {
3390
4086
  };
3391
4087
  guardrails: Guardrail[];
3392
4088
  guardrailMaxRetries: number;
3393
- conversationHistory: number;
4089
+ /** Session compaction configuration — always enabled */
4090
+ compaction: CompactionConfig;
3394
4091
  outputSchema?: unknown;
3395
4092
  /** Original Zod schema for Vercel AI SDK structured output (Output.object()) */
3396
4093
  outputZodSchema?: unknown;
@@ -3401,7 +4098,6 @@ interface AgentDefinition {
3401
4098
  */
3402
4099
  interface AgentStreamResult {
3403
4100
  agent_run_id: string;
3404
- conversation_id: string | undefined;
3405
4101
  result: unknown;
3406
4102
  result_schema: string | null;
3407
4103
  tool_results: ToolResultInfo[];
@@ -3508,7 +4204,7 @@ interface DefineAgentConfig {
3508
4204
  /** Conditions to stop agent execution */
3509
4205
  stopConditions?: StopCondition[] | undefined;
3510
4206
  /** Zod schema for structured output */
3511
- outputSchema?: ZodSchema<unknown, ZodTypeDef, unknown> | undefined;
4207
+ outputSchema?: ZodType | undefined;
3512
4208
  /** Hook(s) to run before workflow execution */
3513
4209
  onStart?: HookOrHandler | undefined;
3514
4210
  /** Hook(s) to run after workflow completion */
@@ -3525,8 +4221,12 @@ interface DefineAgentConfig {
3525
4221
  guardrails?: (Guardrail | GuardrailHandler)[] | undefined;
3526
4222
  /** Maximum guardrail retries (default: 2) */
3527
4223
  guardrailMaxRetries?: number | undefined;
3528
- /** Number of conversation history messages to retain (default: 10) */
3529
- conversationHistory?: number | undefined;
4224
+ /** Session compaction configuration always enabled with sensible defaults */
4225
+ compaction?: CompactionConfig | undefined;
4226
+ /** Publish text_delta and tool_call events to the workflow topic for all invocations */
4227
+ streamToWorkflow?: boolean | undefined;
4228
+ /** Notification channels for suspend events. Overrides Worker-level channels. */
4229
+ channels?: Channel[] | undefined;
3530
4230
  }
3531
4231
  /**
3532
4232
  * Payload for agent run() and stream() calls.
@@ -3535,8 +4235,6 @@ interface DefineAgentConfig {
3535
4235
  interface AgentRunPayload {
3536
4236
  /** Input for the agent (string message or message array) */
3537
4237
  input: string | Record<string, unknown>[];
3538
- /** Conversation ID for history tracking */
3539
- conversationId?: string | undefined;
3540
4238
  }
3541
4239
  /**
3542
4240
  * A Workflow extended with agent-specific properties.
@@ -3577,7 +4275,6 @@ interface AgentWorkflow extends Workflow {
3577
4275
  * Matches Python Agent.with_input().
3578
4276
  */
3579
4277
  withInput(input: string | Record<string, unknown>[], options?: {
3580
- conversationId?: string;
3581
4278
  sessionId?: string;
3582
4279
  userId?: string;
3583
4280
  initialState?: Record<string, unknown>;
@@ -3740,6 +4437,25 @@ interface LoggerOptions {
3740
4437
  /** Custom log handler */
3741
4438
  handler?: ((entry: LogEntry) => void) | undefined;
3742
4439
  }
4440
+ interface ConfigureLoggingOptions {
4441
+ /** Custom log handler for all SDK loggers */
4442
+ handler?: ((entry: LogEntry) => void) | undefined;
4443
+ /** Path to a log file. SDK logs will be appended to this file instead of stdout. */
4444
+ file?: string | undefined;
4445
+ }
4446
+ /**
4447
+ * Configure logging for all SDK loggers globally.
4448
+ *
4449
+ * @example
4450
+ * ```typescript
4451
+ * // Redirect all SDK logs to a file
4452
+ * configureLogging({ file: 'polos.log' });
4453
+ *
4454
+ * // Use a custom handler
4455
+ * configureLogging({ handler: (entry) => myLogger.log(entry) });
4456
+ * ```
4457
+ */
4458
+ declare function configureLogging(options: ConfigureLoggingOptions): void;
3743
4459
  /**
3744
4460
  * Create a logger instance.
3745
4461
  *
@@ -3918,209 +4634,18 @@ interface WebSearchToolConfig extends TavilySearchConfig {
3918
4634
  */
3919
4635
  declare function createWebSearchTool(config?: WebSearchToolConfig): ToolWorkflow;
3920
4636
 
3921
- /**
3922
- * Shared types for the execution framework.
3923
- *
3924
- * Defines interfaces for execution environments, command results,
3925
- * file operations, and configuration.
3926
- */
3927
- /**
3928
- * Abstract interface for an execution environment (Docker, E2B, Local).
3929
- * All sandbox tools operate against this interface.
3930
- */
3931
- interface ExecutionEnvironment {
3932
- /** Environment type discriminator */
3933
- readonly type: 'local' | 'docker' | 'e2b';
3934
- /** Execute a shell command in the environment */
3935
- exec(command: string, opts?: ExecOptions): Promise<ExecResult>;
3936
- /** Read a file's contents as UTF-8 text */
3937
- readFile(path: string): Promise<string>;
3938
- /** Write content to a file, creating parent directories as needed */
3939
- writeFile(path: string, content: string): Promise<void>;
3940
- /** Check whether a file exists */
3941
- fileExists(path: string): Promise<boolean>;
3942
- /** Find files matching a glob pattern */
3943
- glob(pattern: string, opts?: GlobOptions): Promise<string[]>;
3944
- /** Search file contents for a pattern */
3945
- grep(pattern: string, opts?: GrepOptions): Promise<GrepMatch[]>;
3946
- /** Initialize the environment (create container, connect to sandbox, etc.) */
3947
- initialize(): Promise<void>;
3948
- /** Tear down the environment (remove container, kill sandbox, etc.) */
3949
- destroy(): Promise<void>;
3950
- /** Get the current working directory inside the environment */
3951
- getCwd(): string;
3952
- /** Get environment metadata */
3953
- getInfo(): EnvironmentInfo;
3954
- }
3955
- /**
3956
- * Options for command execution.
3957
- */
3958
- interface ExecOptions {
3959
- /** Working directory for the command */
3960
- cwd?: string | undefined;
3961
- /** Environment variables to set */
3962
- env?: Record<string, string> | undefined;
3963
- /** Timeout in seconds (default: 300) */
3964
- timeout?: number | undefined;
3965
- /** Data to pipe to stdin */
3966
- stdin?: string | undefined;
3967
- }
3968
- /**
3969
- * Result of a command execution.
3970
- */
3971
- interface ExecResult {
3972
- /** Process exit code (0 = success) */
3973
- exitCode: number;
3974
- /** Standard output */
3975
- stdout: string;
3976
- /** Standard error */
3977
- stderr: string;
3978
- /** Execution duration in milliseconds */
3979
- durationMs: number;
3980
- /** Whether output was truncated due to size limits */
3981
- truncated: boolean;
3982
- }
3983
- /**
3984
- * Options for glob file search.
3985
- */
3986
- interface GlobOptions {
3987
- /** Working directory for the search */
3988
- cwd?: string | undefined;
3989
- /** Glob patterns to exclude */
3990
- ignore?: string[] | undefined;
3991
- }
3992
- /**
3993
- * Options for grep content search.
3994
- */
3995
- interface GrepOptions {
3996
- /** Working directory for the search */
3997
- cwd?: string | undefined;
3998
- /** File glob patterns to include (e.g., "*.ts") */
3999
- include?: string[] | undefined;
4000
- /** Maximum number of matches to return */
4001
- maxResults?: number | undefined;
4002
- /** Number of context lines around each match */
4003
- contextLines?: number | undefined;
4004
- }
4005
- /**
4006
- * A single grep match result.
4007
- */
4008
- interface GrepMatch {
4009
- /** File path (relative to search root) */
4010
- path: string;
4011
- /** Line number of the match */
4012
- line: number;
4013
- /** The matching line text */
4014
- text: string;
4015
- /** Context lines around the match */
4016
- context?: string | undefined;
4017
- }
4018
- /**
4019
- * Metadata about an execution environment.
4020
- */
4021
- interface EnvironmentInfo {
4022
- /** Environment type */
4023
- type: 'local' | 'docker' | 'e2b';
4024
- /** Current working directory */
4025
- cwd: string;
4026
- /** Sandbox/container identifier (container ID for Docker, sandbox ID for E2B) */
4027
- sandboxId?: string | undefined;
4028
- /** Operating system info */
4029
- os?: string | undefined;
4030
- }
4031
- /**
4032
- * Configuration for a Docker execution environment.
4033
- */
4034
- interface DockerEnvironmentConfig {
4035
- /** Docker image to use (e.g., "node:20-slim") */
4036
- image: string;
4037
- /** Host directory to mount as workspace */
4038
- workspaceDir: string;
4039
- /** Working directory inside the container (default: "/workspace") */
4040
- containerWorkdir?: string | undefined;
4041
- /** Environment variables to set in the container */
4042
- env?: Record<string, string> | undefined;
4043
- /** Memory limit (e.g., "512m", "2g") */
4044
- memory?: string | undefined;
4045
- /** CPU limit (e.g., "1", "0.5") */
4046
- cpus?: string | undefined;
4047
- /** Network mode (default: "none") */
4048
- network?: string | undefined;
4049
- /** Command to run after container creation (e.g., "npm install") */
4050
- setupCommand?: string | undefined;
4051
- }
4052
- /**
4053
- * Configuration for an E2B execution environment.
4054
- */
4055
- interface E2BEnvironmentConfig {
4056
- /** E2B template name (default: "base") */
4057
- template?: string | undefined;
4058
- /** E2B API key (defaults to E2B_API_KEY env var) */
4059
- apiKey?: string | undefined;
4060
- /** Sandbox timeout in seconds (default: 3600) */
4061
- timeout?: number | undefined;
4062
- /** Working directory inside the sandbox */
4063
- cwd?: string | undefined;
4064
- /** Environment variables */
4065
- env?: Record<string, string> | undefined;
4066
- /** Setup command to run after sandbox creation */
4067
- setupCommand?: string | undefined;
4068
- }
4069
- /**
4070
- * Configuration for a local execution environment.
4071
- */
4072
- interface LocalEnvironmentConfig {
4073
- /** Working directory (default: process.cwd()) */
4074
- cwd?: string | undefined;
4075
- /** Restrict file operations to this directory */
4076
- pathRestriction?: string | undefined;
4077
- }
4078
- /**
4079
- * Configuration for the exec tool's security and behavior.
4080
- */
4081
- interface ExecToolConfig {
4082
- /** Security mode: allow-always (default), allowlist, or always require approval */
4083
- security?: 'allow-always' | 'allowlist' | 'approval-always' | undefined;
4084
- /** Allowed command patterns (for allowlist mode) */
4085
- allowlist?: string[] | undefined;
4086
- /** Default command timeout in seconds (default: 300) */
4087
- timeout?: number | undefined;
4088
- /** Maximum output characters before truncation (default: 100000) */
4089
- maxOutputChars?: number | undefined;
4090
- }
4091
- /**
4092
- * Configuration for the sandboxTools() factory.
4093
- */
4094
- interface SandboxToolsConfig {
4095
- /** Environment type (default: "docker") */
4096
- env?: 'local' | 'docker' | 'e2b' | undefined;
4097
- /** Working directory override */
4098
- cwd?: string | undefined;
4099
- /** Subset of tools to include (default: all) */
4100
- tools?: ('exec' | 'read' | 'write' | 'edit' | 'glob' | 'grep')[] | undefined;
4101
- /** Docker environment configuration */
4102
- docker?: DockerEnvironmentConfig | undefined;
4103
- /** E2B environment configuration */
4104
- e2b?: E2BEnvironmentConfig | undefined;
4105
- /** Local environment configuration */
4106
- local?: LocalEnvironmentConfig | undefined;
4107
- /** Exec tool configuration */
4108
- exec?: ExecToolConfig | undefined;
4109
- /** Approval mode for file-mutating tools (write, edit). Defaults to 'always' for local env. */
4110
- fileApproval?: 'always' | 'none' | undefined;
4111
- }
4112
-
4113
4637
  /**
4114
4638
  * Sandbox tools factory.
4115
4639
  *
4116
4640
  * Creates a set of tools (exec, read, write, edit, glob, grep) that share
4117
- * a lazily-initialized execution environment via closure. The environment
4118
- * is created on first tool use and reused for all subsequent calls.
4641
+ * a managed sandbox. The sandbox is created lazily on first tool use
4642
+ * via the SandboxManager injected through the execution context.
4119
4643
  *
4120
4644
  * @example
4121
4645
  * ```typescript
4122
4646
  * import { defineAgent, sandboxTools } from '@polos/sdk';
4123
4647
  *
4648
+ * // Per-execution (default) — sandbox dies when the workflow finishes
4124
4649
  * const agent = defineAgent({
4125
4650
  * id: 'solver',
4126
4651
  * tools: sandboxTools({
@@ -4128,25 +4653,27 @@ interface SandboxToolsConfig {
4128
4653
  * docker: { image: 'node:20', workspaceDir: '/path/to/project' },
4129
4654
  * }),
4130
4655
  * });
4656
+ *
4657
+ * // Per-session — sandbox lives across turns
4658
+ * const agent2 = defineAgent({
4659
+ * id: 'coder',
4660
+ * tools: sandboxTools({
4661
+ * scope: 'session',
4662
+ * env: 'docker',
4663
+ * docker: { image: 'node:20', workspaceDir: '/path/to/project' },
4664
+ * }),
4665
+ * });
4131
4666
  * ```
4132
4667
  */
4133
4668
 
4134
- /**
4135
- * Return type for sandboxTools — an array of ToolWorkflow with a cleanup method.
4136
- */
4137
- interface SandboxToolsResult extends Array<ToolWorkflow> {
4138
- /** Destroy the shared execution environment (remove container, etc.) */
4139
- cleanup(): Promise<void>;
4140
- }
4141
4669
  /**
4142
4670
  * Create sandbox tools for AI agents.
4143
4671
  *
4144
4672
  * Returns an array of ToolWorkflow that can be passed directly to defineAgent().
4145
- * All tools share a single execution environment that is lazily created on first use.
4146
- *
4147
- * The returned array has a `cleanup()` method for destroying the environment.
4673
+ * All tools share a single managed sandbox that is lazily created on first use.
4674
+ * Lifecycle is managed by the SandboxManager — no manual cleanup needed.
4148
4675
  */
4149
- declare function sandboxTools(config?: SandboxToolsConfig): SandboxToolsResult;
4676
+ declare function sandboxTools(config?: SandboxToolsConfig): ToolWorkflow[];
4150
4677
 
4151
4678
  /**
4152
4679
  * Docker execution environment.
@@ -4171,7 +4698,11 @@ declare class DockerEnvironment implements ExecutionEnvironment {
4171
4698
  private readonly containerWorkdir;
4172
4699
  private readonly maxOutputChars;
4173
4700
  constructor(config: DockerEnvironmentConfig, maxOutputChars?: number);
4174
- initialize(): Promise<void>;
4701
+ /**
4702
+ * Get the container name (useful for health checking and identification).
4703
+ */
4704
+ getContainerName(): string;
4705
+ initialize(labels?: Record<string, string>): Promise<void>;
4175
4706
  exec(command: string, opts?: ExecOptions): Promise<ExecResult>;
4176
4707
  readFile(filePath: string): Promise<string>;
4177
4708
  writeFile(filePath: string, content: string): Promise<void>;
@@ -4325,4 +4856,84 @@ declare function createGlobTool(getEnv: () => Promise<ExecutionEnvironment>, pat
4325
4856
  */
4326
4857
  declare function createGrepTool(getEnv: () => Promise<ExecutionEnvironment>, pathConfig?: PathRestrictionConfig): ToolWorkflow;
4327
4858
 
4328
- export { type Agent, type AgentConfig, type AgentContext, type AgentResult, AgentRunConfig, type AgentRunOptions, type AgentRunPayload, type AgentStep, type AgentStream, type AgentStreamEvent, type AgentStreamPayload, type AgentStreamResult, type AgentWorkflow, type BatchEventPayload, type BatchStepResult, type BatchWorkflowInput, type ClientBatchWorkflowInput, type ClientInvokeOptions, type CoreMessage$1 as CoreMessage, type DefineAgentConfig, type DefineGuardrailOptions, type DefineHookOptions, type DefineToolConfig, DockerEnvironment, type DockerEnvironmentConfig, DuplicateWorkflowError, type E2BEnvironmentConfig, type EnvironmentInfo, type Event, type EventData, type EventItem, type EventPayload, type EventTriggerPayload, type EventsApi, type ExecOptions, type ExecResult, type ExecToolConfig, type ExecuteGuardrailsOptions, type ExecuteHooksOptions, type ExecuteWorkflowOptions, type ExecutionContext, type ExecutionData, type ExecutionEnvironment, ExecutionHandle, type ExecutionHandleFields, type ExecutionResult, type FinishReason, type GenerateConfig, type GlobOptions, type GrepMatch, type GrepOptions, type Guardrail$1 as Guardrail, type GuardrailChainResult, type GuardrailContext$1 as GuardrailContext, GuardrailError, type GuardrailHandler, GuardrailResult$1 as GuardrailResult, type GuardrailResultType, type Hook, type HookChainResult, type HookContext, HookExecutionError, type HookHandler, HookResult, type HookResultType, type InferPayload, type InferResult, type InferState, type InvokeOptions, LLM, type LLMGenerateOptions, type LLMGeneratePayload, type LLMGenerateResult, type LLMProvider, type LLMResponse, type LLMStreamEvent, type LLMStreamPayload, type LLMToolCall, type LLMToolResult, type LLMUsage, type LlmToolDefinition, type LocalEnvironmentConfig, type Logger, type Guardrail as MiddlewareGuardrail, type GuardrailContext as MiddlewareGuardrailContext, GuardrailResult as MiddlewareGuardrailResult, OrchestratorApiError, OrchestratorClient, type OrchestratorClientConfig, type OtelConfig, PolosClient, type PolosClientConfig, type PublishEventFn, type PublishEventOptions, Queue, type QueueConfig$1 as QueueConfig, type QueueOptions, type ResumeOptions, type SandboxToolsConfig, type SandboxToolsResult, type ScheduleConfig, type SchedulePayload, type SchedulesApi, StateSizeError, StateValidationError, StepExecutionError, type StepHelper, type StepInfo, type StepOptions, type StepStore, type StopCondition, type StopConditionContext, type StreamEvent, StreamResult, type StreamTopicOptions, type StreamWorkflowOptions, type SuspendOptions, type TavilySearchConfig, type TextDeltaEvent, type TokenUsage, type Tool, type ToolApproval, type ToolCall, type ToolCallEvent, type ToolConfig, type ToolHandler, type ToolResult, type ToolResultInfo, type ToolWorkflow, WaitError, type WaitForEventOptions, type WaitForOptions, type WebSearchFunction, type WebSearchOptions, type WebSearchResult, type WebSearchResultItem, type WebSearchToolConfig, Worker, type WorkerConfig, type WorkerExecutionData, WorkerServer, type WorkerServerConfig, type Workflow, type WorkflowConfig, type WorkflowContext, type WorkflowEvent, type WorkflowHandle, type WorkflowHandler, WorkflowNotFoundError, type WorkflowRegistry, type WorkflowRunClient, type WorkflowRunOptions, type WorkflowStatus, agentStreamFunction, assertSafePath, batchAgentInvoke, batchInvoke, composeGuardrails, composeHooks, conditionalHook, convertFinishReason, convertMiddlewareToolCallToPython, convertPythonToolCallToMiddleware, convertToolResultsToMessages, convertToolsToVercel, convertVercelToolCallToPython, convertVercelUsageToPython, createAskUserTool, createEditTool, createExecTool, createGlobTool, createGrepTool, createLogger, createReadTool, createStepHelper, createStepStore, createWebSearchTool, createWorkflowRegistry, createWriteTool, defineAgent, defineGuardrail, defineHook, defineTool, defineWorkflow, evaluateAllowlist, executeGuardrailChain, executeGuardrailsOrThrow, executeHookChain, executeHooksOrThrow, executeWorkflow, executedTool, extractTraceparent, generateTraceIdFromExecutionId, getTracer, globalRegistry, hasText, initializeOtel, initializeState, isAgentWorkflow, isBinary, isGuardrail, isHook, isOtelAvailable, isToolWorkflow, isWaitError, llmGenerate, llmStream, maxSteps, maxTokens, normalizeGuardrail, normalizeGuardrails, normalizeHook, normalizeHooks, parseGrepOutput, retry, sandboxTools, serializeFinalState, sleep, stopCondition, stripAnsi, truncateOutput, validateState };
4859
+ /**
4860
+ * Token estimation utilities for session compaction.
4861
+ *
4862
+ * Uses a simple heuristic: ~4 characters per token.
4863
+ */
4864
+
4865
+ /**
4866
+ * Estimate token count for a string using the ~4 chars/token heuristic.
4867
+ */
4868
+ declare function estimateTokens(text: string): number;
4869
+ /**
4870
+ * Estimate token count for a single conversation message.
4871
+ */
4872
+ declare function estimateMessageTokens(message: ConversationMessage): number;
4873
+ /**
4874
+ * Estimate total token count for an array of conversation messages.
4875
+ */
4876
+ declare function estimateMessagesTokens(messages: ConversationMessage[]): number;
4877
+
4878
+ /**
4879
+ * Core session compaction logic.
4880
+ *
4881
+ * Compacts older conversation messages into a rolling summary via an LLM call,
4882
+ * keeping the last N recent messages verbatim.
4883
+ */
4884
+
4885
+ /**
4886
+ * Build the user/assistant summary pair to inject at the start of conversation.
4887
+ */
4888
+ declare function buildSummaryMessages(summary: string): [ConversationMessage, ConversationMessage];
4889
+ /**
4890
+ * Compact conversation messages if they exceed the token budget.
4891
+ *
4892
+ * 1. Estimate total tokens of all messages
4893
+ * 2. If under maxConversationTokens -> return as-is (no-op)
4894
+ * 3. Otherwise:
4895
+ * - Find summary pair at start (if present)
4896
+ * - Determine messages to fold (between summary pair and last minRecentMessages)
4897
+ * - Call compaction model to generate summary
4898
+ * - If summary exceeds maxSummaryTokens, re-summarize
4899
+ * - Replace folded messages + old summary pair with new summary pair
4900
+ * 4. On failure -> log warning, fall back to naive truncation
4901
+ */
4902
+ declare function compactIfNeeded(messages: ConversationMessage[], currentSummary: string | null, config: NormalizedCompactionConfig): Promise<CompactionResult>;
4903
+
4904
+ /**
4905
+ * Slack channel implementation — sends a Block Kit message with a "Respond"
4906
+ * link button when an agent suspends.
4907
+ *
4908
+ * Uses native `fetch` (Node 18+) — no @slack/web-api dependency required.
4909
+ */
4910
+
4911
+ /**
4912
+ * Configuration for the Slack notification channel.
4913
+ */
4914
+ interface SlackChannelConfig {
4915
+ /** Slack bot token (xoxb-...) */
4916
+ botToken: string;
4917
+ /** Default Slack channel for notifications (e.g., "#agent-notifications") */
4918
+ defaultChannel: string;
4919
+ /** Slack signing secret for verifying inbound webhooks */
4920
+ signingSecret?: string;
4921
+ }
4922
+ /**
4923
+ * Slack notification channel that posts Block Kit messages with a "Respond"
4924
+ * link button pointing to the approval page.
4925
+ */
4926
+ declare class SlackChannel implements Channel {
4927
+ readonly id = "slack";
4928
+ readonly outputMode: ChannelOutputMode;
4929
+ private readonly config;
4930
+ constructor(config: SlackChannelConfig);
4931
+ notify(notification: SuspendNotification): Promise<void>;
4932
+ sendOutput(context: ChannelContext, event: StreamEvent): Promise<void>;
4933
+ private postMessage;
4934
+ private formatOutputEvent;
4935
+ private buildBlocks;
4936
+ private isSimpleApproval;
4937
+ }
4938
+
4939
+ export { type Agent, type AgentConfig, type AgentContext, type AgentResult, AgentRunConfig, type AgentRunOptions, type AgentRunPayload, type AgentStep, type AgentStream, type AgentStreamEvent, type AgentStreamPayload, type AgentStreamResult, type AgentWorkflow, type BatchEventPayload, type BatchStepResult, type BatchWorkflowInput, type Channel, type ChannelContext, type ChannelOutputMode, type ClientBatchWorkflowInput, type ClientInvokeOptions, type CompactionConfig, type CompactionResult, type ConfigureLoggingOptions, type CoreMessage$1 as CoreMessage, type DefineAgentConfig, type DefineGuardrailOptions, type DefineHookOptions, type DefineToolConfig, DockerEnvironment, type DockerEnvironmentConfig, DuplicateWorkflowError, type E2BEnvironmentConfig, type EnvironmentInfo, type Event, type EventData, type EventItem, type EventPayload, type EventTriggerPayload, type EventsApi, type ExecOptions, type ExecResult, type ExecToolConfig, type ExecuteGuardrailsOptions, type ExecuteHooksOptions, type ExecuteWorkflowOptions, type ExecutionContext, type ExecutionData, type ExecutionEnvironment, ExecutionHandle, type ExecutionHandleFields, type ExecutionResult, type FinishReason, type GenerateConfig, type GlobOptions, type GrepMatch, type GrepOptions, type Guardrail$1 as Guardrail, type GuardrailChainResult, type GuardrailContext$1 as GuardrailContext, GuardrailError, type GuardrailHandler, GuardrailResult$1 as GuardrailResult, type GuardrailResultType, type Hook, type HookChainResult, type HookContext, HookExecutionError, type HookHandler, HookResult, type HookResultType, type InferPayload, type InferResult, type InferState, type InvokeOptions, LLM, type LLMGenerateOptions, type LLMGeneratePayload, type LLMGenerateResult, type LLMProvider, type LLMResponse, type LLMStreamEvent, type LLMStreamPayload, type LLMToolCall, type LLMToolResult, type LLMUsage, type LlmToolDefinition, type LocalEnvironmentConfig, type Logger, ManagedSandbox, type Guardrail as MiddlewareGuardrail, type GuardrailContext as MiddlewareGuardrailContext, GuardrailResult as MiddlewareGuardrailResult, type NormalizedCompactionConfig, OrchestratorApiError, OrchestratorClient, type OrchestratorClientConfig, type OtelConfig, Polos, PolosClient, type PolosClientConfig, type PolosConfig, type PublishEventFn, type PublishEventOptions, Queue, type QueueConfig$1 as QueueConfig, type QueueOptions, type ResumeOptions, type Sandbox, type SandboxConfig, SandboxManager, type SandboxScope, type SandboxToolsConfig, type ScheduleConfig, type SchedulePayload, type SchedulesApi, type SessionMemory, SlackChannel, type SlackChannelConfig, StateSizeError, StateValidationError, StepExecutionError, type StepHelper, type StepInfo, type StepOptions, type StepStore, type StopCondition, type StopConditionContext, type StreamEvent, StreamResult, type StreamTopicOptions, type StreamWorkflowOptions, type SuspendNotification, type SuspendOptions, type TavilySearchConfig, type TextDeltaEvent, type TokenUsage, type Tool, type ToolApproval, type ToolCall, type ToolCallEvent, type ToolConfig, type ToolHandler, type ToolResult, type ToolResultInfo, type ToolWorkflow, WaitError, type WaitForEventOptions, type WaitForOptions, type WebSearchFunction, type WebSearchOptions, type WebSearchResult, type WebSearchResultItem, type WebSearchToolConfig, Worker, type WorkerConfig, type WorkerExecutionData, WorkerServer, type WorkerServerConfig, type Workflow, type WorkflowConfig, type WorkflowContext, type WorkflowEvent, type WorkflowHandle, type WorkflowHandler, WorkflowNotFoundError, type WorkflowRegistry, type WorkflowRunClient, type WorkflowRunOptions, type WorkflowStatus, agentStreamFunction, assertSafePath, batchAgentInvoke, batchInvoke, buildSummaryMessages, compactIfNeeded, composeGuardrails, composeHooks, conditionalHook, configureLogging, convertFinishReason, convertMiddlewareToolCallToPython, convertPythonToolCallToMiddleware, convertToolResultsToMessages, convertToolsToVercel, convertVercelToolCallToPython, convertVercelUsageToPython, createAskUserTool, createEditTool, createExecTool, createGlobTool, createGrepTool, createLogger, createReadTool, createStepHelper, createStepStore, createWebSearchTool, createWorkflowRegistry, createWriteTool, defineAgent, defineGuardrail, defineHook, defineTool, defineWorkflow, estimateMessageTokens, estimateMessagesTokens, estimateTokens, evaluateAllowlist, executeGuardrailChain, executeGuardrailsOrThrow, executeHookChain, executeHooksOrThrow, executeWorkflow, executedTool, extractTraceparent, generateTraceIdFromExecutionId, getTracer, globalRegistry, hasText, initializeOtel, initializeState, isAgentWorkflow, isBinary, isGuardrail, isHook, isOtelAvailable, isToolWorkflow, isWaitError, llmGenerate, llmStream, maxSteps, maxTokens, normalizeGuardrail, normalizeGuardrails, normalizeHook, normalizeHooks, parseGrepOutput, retry, sandboxTools, serializeFinalState, sleep, stopCondition, stripAnsi, truncateOutput, validateState };