@aws-blocks/bb-agent 0.1.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.
Files changed (75) hide show
  1. package/LICENSE +174 -0
  2. package/README.md +801 -0
  3. package/dist/agent.aws.d.ts +7 -0
  4. package/dist/agent.aws.d.ts.map +1 -0
  5. package/dist/agent.aws.js +9 -0
  6. package/dist/agent.d.ts +121 -0
  7. package/dist/agent.d.ts.map +1 -0
  8. package/dist/agent.js +588 -0
  9. package/dist/agent.mock.d.ts +7 -0
  10. package/dist/agent.mock.d.ts.map +1 -0
  11. package/dist/agent.mock.js +12 -0
  12. package/dist/errors.d.ts +39 -0
  13. package/dist/errors.d.ts.map +1 -0
  14. package/dist/errors.js +40 -0
  15. package/dist/file-bucket-snapshot-storage.d.ts +49 -0
  16. package/dist/file-bucket-snapshot-storage.d.ts.map +1 -0
  17. package/dist/file-bucket-snapshot-storage.js +84 -0
  18. package/dist/index.aws.d.ts +5 -0
  19. package/dist/index.aws.d.ts.map +1 -0
  20. package/dist/index.aws.js +5 -0
  21. package/dist/index.browser.d.ts +4 -0
  22. package/dist/index.browser.d.ts.map +1 -0
  23. package/dist/index.browser.js +8 -0
  24. package/dist/index.cdk.d.ts +15 -0
  25. package/dist/index.cdk.d.ts.map +1 -0
  26. package/dist/index.cdk.js +60 -0
  27. package/dist/index.hooks.d.ts +122 -0
  28. package/dist/index.hooks.d.ts.map +1 -0
  29. package/dist/index.hooks.js +179 -0
  30. package/dist/index.mock.d.ts +5 -0
  31. package/dist/index.mock.d.ts.map +1 -0
  32. package/dist/index.mock.js +5 -0
  33. package/dist/index.test.d.ts +2 -0
  34. package/dist/index.test.d.ts.map +1 -0
  35. package/dist/index.test.js +864 -0
  36. package/dist/model-factory.d.ts +26 -0
  37. package/dist/model-factory.d.ts.map +1 -0
  38. package/dist/model-factory.js +197 -0
  39. package/dist/models.d.ts +83 -0
  40. package/dist/models.d.ts.map +1 -0
  41. package/dist/models.js +84 -0
  42. package/dist/providers/canned.d.ts +32 -0
  43. package/dist/providers/canned.d.ts.map +1 -0
  44. package/dist/providers/canned.js +187 -0
  45. package/dist/providers/throwing.d.ts +10 -0
  46. package/dist/providers/throwing.d.ts.map +1 -0
  47. package/dist/providers/throwing.js +16 -0
  48. package/dist/schemas.d.ts +59 -0
  49. package/dist/schemas.d.ts.map +1 -0
  50. package/dist/schemas.js +36 -0
  51. package/dist/types.d.ts +295 -0
  52. package/dist/types.d.ts.map +1 -0
  53. package/dist/types.js +3 -0
  54. package/dist/version.d.ts +3 -0
  55. package/dist/version.d.ts.map +1 -0
  56. package/dist/version.js +3 -0
  57. package/package.json +59 -0
  58. package/src/agent.aws.ts +13 -0
  59. package/src/agent.mock.ts +16 -0
  60. package/src/agent.ts +604 -0
  61. package/src/errors.ts +44 -0
  62. package/src/file-bucket-snapshot-storage.ts +85 -0
  63. package/src/index.aws.ts +7 -0
  64. package/src/index.browser.ts +10 -0
  65. package/src/index.cdk.ts +70 -0
  66. package/src/index.hooks.ts +256 -0
  67. package/src/index.mock.ts +7 -0
  68. package/src/index.test.ts +1010 -0
  69. package/src/model-factory.ts +228 -0
  70. package/src/models.ts +88 -0
  71. package/src/providers/canned.ts +205 -0
  72. package/src/providers/throwing.ts +19 -0
  73. package/src/schemas.ts +40 -0
  74. package/src/types.ts +311 -0
  75. package/src/version.ts +3 -0
@@ -0,0 +1,7 @@
1
+ import type { ScopeParent } from '@aws-blocks/core';
2
+ import { AgentBase } from './agent.js';
3
+ import type { AgentConfig, DefaultToolContext } from './types.js';
4
+ export declare class Agent<TContext = DefaultToolContext> extends AgentBase<TContext> {
5
+ constructor(scope: ScopeParent, id: string, config: AgentConfig<TContext>);
6
+ }
7
+ //# sourceMappingURL=agent.aws.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.aws.d.ts","sourceRoot":"","sources":["../src/agent.aws.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAElE,qBAAa,KAAK,CAAC,QAAQ,GAAG,kBAAkB,CAAE,SAAQ,SAAS,CAAC,QAAQ,CAAC;gBAChE,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,QAAQ,CAAC;CAGzE"}
@@ -0,0 +1,9 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { AgentBase } from './agent.js';
4
+ import { S3Storage } from '@strands-agents/sdk/session/s3-storage';
5
+ export class Agent extends AgentBase {
6
+ constructor(scope, id, config) {
7
+ super(scope, id, config, config.model.deployed, (bucket) => new S3Storage({ bucket: bucket.fullId }));
8
+ }
9
+ }
@@ -0,0 +1,121 @@
1
+ import { Scope } from '@aws-blocks/core';
2
+ import type { ScopeParent } from '@aws-blocks/core';
3
+ import { FileBucket } from '@aws-blocks/bb-file-bucket';
4
+ import type { ChildLogger } from '@aws-blocks/bb-logger';
5
+ import type { SnapshotStorage } from '@strands-agents/sdk';
6
+ import type { AgentConfig, AgentStreamResult, StreamOptions, Message, Conversation, ModelConfig, InterruptResponse, DefaultToolContext } from './types.js';
7
+ /**
8
+ * Base class for the Agent BB. Extended by agent.mock.ts (model.local) and agent.aws.ts (model.deployed).
9
+ *
10
+ * Creates up to 4 internal BBs depending on mode:
11
+ * - FileBucket: session snapshot storage for Strands SessionManager (always)
12
+ * - DistributedTable: frontend message history (when inferenceOnly = false)
13
+ * - Realtime: streaming chunks to browser + AsyncJob result delivery (always)
14
+ * - AsyncJob: runs Strands agent asynchronously (always)
15
+ * - TODO logging
16
+ */
17
+ export declare class AgentBase<TContext = DefaultToolContext> extends Scope {
18
+ /** Developer-facing agent configuration. */
19
+ private config;
20
+ /** Tools resolved from the `tools` callback into a name→tool map (name = Record key). */
21
+ private toolMap;
22
+ /** Conversation metadata table. */
23
+ private conversations?;
24
+ /** Message history table. */
25
+ private messages?;
26
+ /** Realtime pub/sub — streams chunks to browser. */
27
+ private rt;
28
+ /** Internal async job — runs the Strands agent in a separate execution context. */
29
+ private job;
30
+ /** Which model provider to use. */
31
+ private modelConfig;
32
+ /** Where to persist Strands agent state (snapshots). */
33
+ private snapshotStorage;
34
+ /** Internal FileBucket for session storage. */
35
+ private sessionBucket;
36
+ /** @internal Logger for internal operations. Defaults to error-level when not provided. */
37
+ protected log: ChildLogger;
38
+ /**
39
+ * @param scope - Blocks scope parent (determines resource naming and CDK discovery)
40
+ * @param id - unique agent ID (used in resource names, keep short for AppSync namespace limits)
41
+ * @param config - developer-facing agent configuration
42
+ * @param modelConfig - which model to use, picked by subclass (model.local or model.deployed)
43
+ * @param createSnapshotStorage - factory that receives the internal FileBucket and returns the appropriate SnapshotStorage
44
+ */
45
+ constructor(scope: ScopeParent, id: string, config: AgentConfig<TContext>, modelConfig: ModelConfig | ModelConfig[] | undefined, createSnapshotStorage: (bucket: FileBucket) => SnapshotStorage);
46
+ /**
47
+ * Executes the Strands agent, publishes chunks to Realtime, persists messages to DynamoDB.
48
+ *
49
+ * Called by: AsyncJob consumer.
50
+ * NOT called directly — stream() submits to AsyncJob, which invokes this.
51
+ *
52
+ * Flow: AsyncJob handler → runAgent() → Strands agent.stream() → publishes chunks to Realtime BB
53
+ * TODO add comments for args
54
+ */
55
+ private runAgent;
56
+ private createStrandsAgent;
57
+ /**
58
+ * Submit a message to the agent. Returns immediately with a channelId.
59
+ *
60
+ * Flow: stream() → AsyncJob.submit() → returns { channelId }
61
+ * The AsyncJob consumer calls runAgent() separately.
62
+ * Chunks are published to Realtime on the returned channelId.
63
+ *
64
+ * Subscribe to chunks via result.channel, or await result.complete() for the final response.
65
+ */
66
+ stream(message: string, options?: StreamOptions<TContext>): Promise<AgentStreamResult>;
67
+ /**
68
+ * Resume an interrupted agent with user's responses.
69
+ * Submits a new AsyncJob that loads the session and continues from the interrupt point.
70
+ * Chunks are published to the same channelId — use the existing subscription or call complete() again to wait for the result.
71
+ */
72
+ resume(channelId: string, responses: Array<InterruptResponse>, options?: {
73
+ conversationId?: string;
74
+ userId?: string;
75
+ context?: TContext;
76
+ }): Promise<void>;
77
+ /**
78
+ * Validates the per-call tool context against `toolContextSchema` (when set) and returns it.
79
+ * Throws InvalidModelConfig when the schema is declared but the context is missing or invalid.
80
+ */
81
+ private resolveContext;
82
+ /** Generate a new conversation ID and create the conversation record. */
83
+ createConversationId(userId: string): Promise<string>;
84
+ /** Get a Realtime channel for streaming chunks. Use this to subscribe before calling stream(). */
85
+ getChannel(channelId: string): Promise<import("@aws-blocks/bb-realtime").RealtimeChannel<any>>;
86
+ /** Check if a conversation has pending (unanswered) interrupts by checking DynamoDB history.
87
+ *
88
+ * ⚠️ Does NOT verify ownership — it reads by conversationId alone. The caller must
89
+ * authorize the request (e.g. confirm the conversation belongs to the authenticated
90
+ * user via listConversations(userId)) before exposing the result. See the
91
+ * "Authorization (caller responsibility)" section in the README.
92
+ *
93
+ * TODO: optimize — query in reverse with limit instead of loading all messages.
94
+ */
95
+ getPendingInterrupts(conversationId: string): Promise<Array<{
96
+ id: string;
97
+ name: string;
98
+ reason?: any;
99
+ }>>;
100
+ /** List all conversations for a user. */
101
+ listConversations(userId: string): Promise<Conversation[]>;
102
+ /** Get messages in a conversation (for frontend display).
103
+ * Returns the most recent messages when `limit` is specified.
104
+ *
105
+ * ⚠️ Does NOT verify ownership — it reads by conversationId alone. The caller must
106
+ * authorize the request (e.g. confirm the conversation belongs to the authenticated
107
+ * user via listConversations(userId)) before returning messages. See the
108
+ * "Authorization (caller responsibility)" section in the README.
109
+ *
110
+ * @param options.limit - Maximum number of (most recent) messages to return.
111
+ * A `limit` of `0` returns an empty array, and any negative value is treated
112
+ * the same as `0` (returns no messages). Omit `limit` to return all messages.
113
+ * TODO: support pagination
114
+ */
115
+ getConversation(id: string, options?: {
116
+ limit?: number;
117
+ }): Promise<Message[]>;
118
+ /** Delete a conversation and its agent state. */
119
+ deleteConversation(id: string, userId: string): Promise<void>;
120
+ }
121
+ //# sourceMappingURL=agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAA6C,MAAM,kBAAkB,CAAC;AACpF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAIpD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAExD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAMzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3D,OAAO,KAAK,EAAE,WAAW,EAAoB,iBAAiB,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAyC,WAAW,EAAa,iBAAiB,EAAE,kBAAkB,EAA6B,MAAM,YAAY,CAAC;AA+D1P;;;;;;;;;GASG;AACH,qBAAa,SAAS,CAAC,QAAQ,GAAG,kBAAkB,CAAE,SAAQ,KAAK;IAClE,4CAA4C;IAC5C,OAAO,CAAC,MAAM,CAAwB;IACtC,yFAAyF;IACzF,OAAO,CAAC,OAAO,CAAmC;IAClD,mCAAmC;IACnC,OAAO,CAAC,aAAa,CAAC,CAA8G;IACpI,6BAA6B;IAC7B,OAAO,CAAC,QAAQ,CAAC,CAA4G;IAC7H,oDAAoD;IACpD,OAAO,CAAC,EAAE,CAAgC;IAC1C,mFAAmF;IACnF,OAAO,CAAC,GAAG,CAA6C;IACxD,mCAAmC;IACnC,OAAO,CAAC,WAAW,CAA0C;IAC7D,wDAAwD;IACxD,OAAO,CAAC,eAAe,CAAkB;IACzC,+CAA+C;IAC/C,OAAO,CAAC,aAAa,CAAa;IAClC,2FAA2F;IAC3F,SAAS,CAAC,GAAG,EAAE,WAAW,CAAC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