@superblocksteam/vite-plugin-file-sync 2.0.43-next.8 → 2.0.48-next.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/ai-service/agent/subagents/apis/api-executor.d.ts +57 -0
- package/dist/ai-service/agent/subagents/apis/api-executor.d.ts.map +1 -0
- package/dist/ai-service/agent/subagents/apis/api-executor.js +284 -0
- package/dist/ai-service/agent/subagents/apis/api-executor.js.map +1 -0
- package/dist/ai-service/agent/subagents/apis/context.d.ts +12 -0
- package/dist/ai-service/agent/subagents/apis/context.d.ts.map +1 -0
- package/dist/ai-service/agent/subagents/apis/context.js +18 -0
- package/dist/ai-service/agent/subagents/apis/context.js.map +1 -0
- package/dist/ai-service/agent/subagents/apis/generate-api-source.d.ts +37 -31
- package/dist/ai-service/agent/subagents/apis/generate-api-source.d.ts.map +1 -1
- package/dist/ai-service/agent/subagents/apis/generate-api-source.js +355 -479
- package/dist/ai-service/agent/subagents/apis/generate-api-source.js.map +1 -1
- package/dist/ai-service/agent/subagents/apis/state.d.ts +40 -0
- package/dist/ai-service/agent/subagents/apis/state.d.ts.map +1 -0
- package/dist/ai-service/agent/subagents/apis/state.js +25 -0
- package/dist/ai-service/agent/subagents/apis/state.js.map +1 -0
- package/dist/ai-service/agent/subagents/apis/types.d.ts +5 -0
- package/dist/ai-service/agent/subagents/apis/types.d.ts.map +1 -0
- package/dist/ai-service/agent/subagents/apis/types.js +2 -0
- package/dist/ai-service/agent/subagents/apis/types.js.map +1 -0
- package/dist/ai-service/agent/tool-message-utils.d.ts.map +1 -1
- package/dist/ai-service/agent/tool-message-utils.js +8 -24
- package/dist/ai-service/agent/tool-message-utils.js.map +1 -1
- package/dist/ai-service/agent/tools/apis/build-api.d.ts +20 -0
- package/dist/ai-service/agent/tools/apis/build-api.d.ts.map +1 -0
- package/dist/ai-service/agent/tools/apis/build-api.js +170 -0
- package/dist/ai-service/agent/tools/apis/build-api.js.map +1 -0
- package/dist/ai-service/agent/tools/apis/finalize-api.d.ts +15 -0
- package/dist/ai-service/agent/tools/apis/finalize-api.d.ts.map +1 -0
- package/dist/ai-service/agent/tools/apis/finalize-api.js +103 -0
- package/dist/ai-service/agent/tools/apis/finalize-api.js.map +1 -0
- package/dist/ai-service/agent/tools/build-finalize.d.ts +1 -22
- package/dist/ai-service/agent/tools/build-finalize.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-finalize.js +27 -18
- package/dist/ai-service/agent/tools/build-finalize.js.map +1 -1
- package/dist/ai-service/agent/tools/integrations/execute-request.d.ts +1 -1
- package/dist/ai-service/agent/tools/integrations/execute-request.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/integrations/index.d.ts +1 -1
- package/dist/ai-service/agent/tools/integrations/index.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/integrations/index.js +1 -1
- package/dist/ai-service/agent/tools/integrations/index.js.map +1 -1
- package/dist/ai-service/agent/tools/integrations/internal.d.ts +1 -1
- package/dist/ai-service/agent/tools/integrations/internal.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/integrations/internal.js +12 -1
- package/dist/ai-service/agent/tools/integrations/internal.js.map +1 -1
- package/dist/ai-service/agent/tools/integrations/metadata.d.ts +1 -1
- package/dist/ai-service/agent/tools/integrations/metadata.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/integrations/metadata.js +1 -1
- package/dist/ai-service/agent/tools/integrations/metadata.js.map +1 -1
- package/dist/ai-service/agent/tools/integrations/run-code.d.ts +1 -1
- package/dist/ai-service/agent/tools/integrations/run-code.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/study-current-app-state.d.ts +1 -0
- package/dist/ai-service/agent/tools/study-current-app-state.d.ts.map +1 -1
- package/dist/ai-service/agent/tools2/access-control.d.ts.map +1 -1
- package/dist/ai-service/agent/tools2/access-control.js +5 -2
- package/dist/ai-service/agent/tools2/access-control.js.map +1 -1
- package/dist/ai-service/agent/tools2/registry.d.ts +2 -1
- package/dist/ai-service/agent/tools2/registry.d.ts.map +1 -1
- package/dist/ai-service/agent/tools2/registry.js +4 -4
- package/dist/ai-service/agent/tools2/registry.js.map +1 -1
- package/dist/ai-service/agent/tools2/types.d.ts +17 -3
- package/dist/ai-service/agent/tools2/types.d.ts.map +1 -1
- package/dist/ai-service/agent/tools2/types.js +21 -0
- package/dist/ai-service/agent/tools2/types.js.map +1 -1
- package/dist/ai-service/agent/utils.d.ts +1 -0
- package/dist/ai-service/agent/utils.d.ts.map +1 -1
- package/dist/ai-service/agent/utils.js +1 -0
- package/dist/ai-service/agent/utils.js.map +1 -1
- package/dist/ai-service/integrations/metadata/database.d.ts.map +1 -1
- package/dist/ai-service/integrations/metadata/database.js +61 -20
- package/dist/ai-service/integrations/metadata/database.js.map +1 -1
- package/dist/ai-service/integrations/metadata/databricks.d.ts.map +1 -1
- package/dist/ai-service/integrations/metadata/databricks.js +5 -5
- package/dist/ai-service/integrations/metadata/databricks.js.map +1 -1
- package/dist/ai-service/integrations/metadata/graphql-based.d.ts +2 -0
- package/dist/ai-service/integrations/metadata/graphql-based.d.ts.map +1 -1
- package/dist/ai-service/integrations/metadata/graphql-based.js +95 -14
- package/dist/ai-service/integrations/metadata/graphql-based.js.map +1 -1
- package/dist/ai-service/integrations/metadata/llm-utils.d.ts +24 -0
- package/dist/ai-service/integrations/metadata/llm-utils.d.ts.map +1 -0
- package/dist/ai-service/integrations/metadata/llm-utils.js +45 -0
- package/dist/ai-service/integrations/metadata/llm-utils.js.map +1 -0
- package/dist/ai-service/integrations/store.d.ts +5 -5
- package/dist/ai-service/integrations/store.d.ts.map +1 -1
- package/dist/ai-service/integrations/store.js +52 -53
- package/dist/ai-service/integrations/store.js.map +1 -1
- package/dist/ai-service/llm/context/constants.d.ts +7 -6
- package/dist/ai-service/llm/context/constants.d.ts.map +1 -1
- package/dist/ai-service/llm/context/constants.js +7 -6
- package/dist/ai-service/llm/context/constants.js.map +1 -1
- package/dist/ai-service/llm/context/context-handle.d.ts +106 -0
- package/dist/ai-service/llm/context/context-handle.d.ts.map +1 -0
- package/dist/ai-service/llm/context/context-handle.js +134 -0
- package/dist/ai-service/llm/context/context-handle.js.map +1 -0
- package/dist/ai-service/llm/context/context-lock.d.ts +144 -0
- package/dist/ai-service/llm/context/context-lock.d.ts.map +1 -0
- package/dist/ai-service/llm/context/context-lock.js +221 -0
- package/dist/ai-service/llm/context/context-lock.js.map +1 -0
- package/dist/ai-service/llm/context/context.d.ts +18 -19
- package/dist/ai-service/llm/context/context.d.ts.map +1 -1
- package/dist/ai-service/llm/context/context.js +78 -129
- package/dist/ai-service/llm/context/context.js.map +1 -1
- package/dist/ai-service/llm/context/index.d.ts +4 -0
- package/dist/ai-service/llm/context/index.d.ts.map +1 -1
- package/dist/ai-service/llm/context/index.js +5 -0
- package/dist/ai-service/llm/context/index.js.map +1 -1
- package/dist/ai-service/llm/context/internal-types.d.ts +0 -2
- package/dist/ai-service/llm/context/internal-types.d.ts.map +1 -1
- package/dist/ai-service/llm/context/internal-types.js.map +1 -1
- package/dist/ai-service/llm/context/levels/l1.d.ts.map +1 -1
- package/dist/ai-service/llm/context/levels/l1.js +3 -5
- package/dist/ai-service/llm/context/levels/l1.js.map +1 -1
- package/dist/ai-service/llm/context/manager.d.ts +60 -11
- package/dist/ai-service/llm/context/manager.d.ts.map +1 -1
- package/dist/ai-service/llm/context/manager.js +113 -37
- package/dist/ai-service/llm/context/manager.js.map +1 -1
- package/dist/ai-service/llm/context/utils/content-compaction.d.ts +2 -2
- package/dist/ai-service/llm/context/utils/content-compaction.d.ts.map +1 -1
- package/dist/ai-service/llm/context/utils/content-compaction.js +6 -3
- package/dist/ai-service/llm/context/utils/content-compaction.js.map +1 -1
- package/dist/ai-service/llm/context/utils/index.d.ts +1 -1
- package/dist/ai-service/llm/context/utils/index.d.ts.map +1 -1
- package/dist/ai-service/llm/context/utils/index.js +1 -1
- package/dist/ai-service/llm/context/utils/index.js.map +1 -1
- package/dist/ai-service/llm/context/utils/message-utils.d.ts +17 -7
- package/dist/ai-service/llm/context/utils/message-utils.d.ts.map +1 -1
- package/dist/ai-service/llm/context/utils/message-utils.js +31 -18
- package/dist/ai-service/llm/context/utils/message-utils.js.map +1 -1
- package/dist/ai-service/llmobs/helpers.d.ts +9 -2
- package/dist/ai-service/llmobs/helpers.d.ts.map +1 -1
- package/dist/ai-service/llmobs/helpers.js +17 -4
- package/dist/ai-service/llmobs/helpers.js.map +1 -1
- package/dist/ai-service/llmobs/middleware/retry.d.ts +51 -0
- package/dist/ai-service/llmobs/middleware/retry.d.ts.map +1 -0
- package/dist/ai-service/llmobs/middleware/retry.js +147 -0
- package/dist/ai-service/llmobs/middleware/retry.js.map +1 -0
- package/dist/ai-service/llmobs/middleware/stream-text.d.ts.map +1 -1
- package/dist/ai-service/llmobs/middleware/stream-text.js +1 -0
- package/dist/ai-service/llmobs/middleware/stream-text.js.map +1 -1
- package/dist/ai-service/llmobs/tracer.d.ts +4 -0
- package/dist/ai-service/llmobs/tracer.d.ts.map +1 -1
- package/dist/ai-service/llmobs/tracer.js +11 -0
- package/dist/ai-service/llmobs/tracer.js.map +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ButtonPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/CheckboxPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ColumnPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ContainerPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/DatePickerPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/DropdownPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/IconPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ImagePropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/InputPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ModalPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/PagePropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/SectionPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/SlideoutPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/SwitchPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/TablePropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/TextPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-typedefs/Dim.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-typedefs/EventFlow.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-typedefs/TextStyleWithVariant.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/full-examples.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-api.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-components-rules.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-custom-components.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-data-filtering.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-event-flow.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-forms.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-layouts.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-page.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-rbac.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-routes.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-state.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-theming-chakra-new.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/system-base.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/system-incremental.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/system-specific-edit.js +1 -1
- package/dist/ai-service/state-machine/clark-fsm.d.ts +2 -0
- package/dist/ai-service/state-machine/clark-fsm.d.ts.map +1 -1
- package/dist/ai-service/state-machine/clark-fsm.js.map +1 -1
- package/dist/ai-service/state-machine/handlers/llm-generating.d.ts.map +1 -1
- package/dist/ai-service/state-machine/handlers/llm-generating.js +363 -336
- package/dist/ai-service/state-machine/handlers/llm-generating.js.map +1 -1
- package/dist/ai-service/util/retry-on-timeout.d.ts +93 -0
- package/dist/ai-service/util/retry-on-timeout.d.ts.map +1 -0
- package/dist/ai-service/util/retry-on-timeout.js +153 -0
- package/dist/ai-service/util/retry-on-timeout.js.map +1 -0
- package/dist/ai-service/util/stop-condition.d.ts +4 -1
- package/dist/ai-service/util/stop-condition.d.ts.map +1 -1
- package/dist/ai-service/util/stop-condition.js +14 -2
- package/dist/ai-service/util/stop-condition.js.map +1 -1
- package/dist/sync-service/download.d.ts.map +1 -1
- package/dist/sync-service/download.js +28 -7
- package/dist/sync-service/download.js.map +1 -1
- package/dist/util/logger.d.ts +13 -0
- package/dist/util/logger.d.ts.map +1 -1
- package/dist/util/logger.js +21 -0
- package/dist/util/logger.js.map +1 -1
- package/package.json +11 -9
- package/dist/ai-service/llm/context/logger.d.ts +0 -17
- package/dist/ai-service/llm/context/logger.d.ts.map +0 -1
- package/dist/ai-service/llm/context/logger.js +0 -26
- package/dist/ai-service/llm/context/logger.js.map +0 -1
|
@@ -5,9 +5,12 @@
|
|
|
5
5
|
* - Load/save context state from/to database
|
|
6
6
|
* - Cache active contexts in memory
|
|
7
7
|
* - Create ephemeral contexts for one-off operations
|
|
8
|
+
* - Ensure thread-safe exclusive context ownership
|
|
8
9
|
*
|
|
9
10
|
*/
|
|
10
11
|
import { type CacheStrategyType } from "./caching/index.js";
|
|
12
|
+
import { ContextHandle, type AcquireContextOptions } from "./context-handle.js";
|
|
13
|
+
import { ContextLockManager } from "./context-lock.js";
|
|
11
14
|
import { Context } from "./context.js";
|
|
12
15
|
import type { ContextStorage } from "./storage/index.js";
|
|
13
16
|
import type { ContextId, ContextOptions, LoggingOptions } from "./types.js";
|
|
@@ -23,7 +26,7 @@ export interface ContextManagerOptions {
|
|
|
23
26
|
logging?: LoggingOptions;
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
26
|
-
* Manager for creating and managing conversation contexts.
|
|
29
|
+
* Manager for creating and managing conversation contexts with thread-safe ownership.
|
|
27
30
|
*/
|
|
28
31
|
export declare class ContextManager {
|
|
29
32
|
private storage;
|
|
@@ -31,24 +34,70 @@ export declare class ContextManager {
|
|
|
31
34
|
private contextOptions?;
|
|
32
35
|
private loggingOptions?;
|
|
33
36
|
private contexts;
|
|
37
|
+
private lockManager;
|
|
34
38
|
private static readonly logPrefix;
|
|
35
39
|
constructor(options: ContextManagerOptions);
|
|
36
40
|
/**
|
|
37
|
-
*
|
|
41
|
+
* Creates a unique key for a context ID.
|
|
42
|
+
*/
|
|
43
|
+
private getContextKey;
|
|
44
|
+
/**
|
|
45
|
+
* Acquires exclusive access to a context.
|
|
38
46
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* 2. Loaded from storage if not cached
|
|
42
|
-
* 3. Created fresh if not found in storage
|
|
43
|
-
* 4. Cached for subsequent requests
|
|
44
|
-
* 5. Automatically persisted on changes
|
|
47
|
+
* This method ensures thread-safe access to contexts by requiring
|
|
48
|
+
* an owner ID and returning a handle that must be released when done.
|
|
45
49
|
*
|
|
46
50
|
* @param id - Unique context identifier
|
|
47
|
-
* @param
|
|
48
|
-
* @
|
|
51
|
+
* @param ownerId - Unique identifier of the entity acquiring the context
|
|
52
|
+
* @param options - Context and acquisition options
|
|
53
|
+
* @returns ContextHandle with exclusive access to the context
|
|
54
|
+
* @throws {ContextLockError} if the context is locked and acquisition fails
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const handle = await manager.acquireContext(contextId, "api-subagent-1");
|
|
59
|
+
* try {
|
|
60
|
+
* handle.context.startTurn(userMessage);
|
|
61
|
+
* // ... use the context
|
|
62
|
+
* } finally {
|
|
63
|
+
* handle.release();
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
49
66
|
*/
|
|
50
|
-
|
|
67
|
+
acquireContext(id: ContextId, ownerId: string, options?: {
|
|
68
|
+
contextOptions?: ContextOptions;
|
|
69
|
+
acquireOptions?: AcquireContextOptions;
|
|
70
|
+
}): Promise<ContextHandle>;
|
|
51
71
|
private get logger();
|
|
72
|
+
/**
|
|
73
|
+
* Checks if a context is currently locked.
|
|
74
|
+
*
|
|
75
|
+
* @param id - Context identifier
|
|
76
|
+
* @returns true if the context is locked
|
|
77
|
+
*/
|
|
78
|
+
isContextLocked(id: ContextId): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Gets the current owner of a context.
|
|
81
|
+
*
|
|
82
|
+
* @param id - Context identifier
|
|
83
|
+
* @returns The owner ID if locked, undefined otherwise
|
|
84
|
+
*/
|
|
85
|
+
getContextOwner(id: ContextId): string | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* Gets information about all active context locks.
|
|
88
|
+
* Useful for debugging and monitoring.
|
|
89
|
+
*
|
|
90
|
+
* @returns Array of active lock information
|
|
91
|
+
*/
|
|
92
|
+
getActiveLocks(): ReturnType<ContextLockManager["getActiveLocks"]>;
|
|
93
|
+
/**
|
|
94
|
+
* Releases a context lock if owned by the specified owner.
|
|
95
|
+
* This is useful for cleanup in error scenarios.
|
|
96
|
+
*
|
|
97
|
+
* @param id - Context identifier
|
|
98
|
+
* @param ownerId - The owner releasing the context
|
|
99
|
+
*/
|
|
100
|
+
releaseContext(id: ContextId, ownerId: string): void;
|
|
52
101
|
/**
|
|
53
102
|
* Creates an ephemeral context (no persistence or caching).
|
|
54
103
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../../../src/ai-service/llm/context/manager.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../../../src/ai-service/llm/context/manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAEL,KAAK,iBAAiB,EACvB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,aAAa,EAEb,KAAK,qBAAqB,EAC3B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAoB,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5E;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,mDAAmD;IACnD,OAAO,EAAE,cAAc,CAAC;IACxB,oEAAoE;IACpE,aAAa,CAAC,EAAE,iBAAiB,CAAC;IAClC,6CAA6C;IAC7C,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B;AAED;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAiB;IAChC,OAAO,CAAC,iBAAiB,CAAC,CAAoB;IAC9C,OAAO,CAAC,cAAc,CAAC,CAAiB;IACxC,OAAO,CAAC,cAAc,CAAC,CAAiB;IACxC,OAAO,CAAC,QAAQ,CAA8B;IAC9C,OAAO,CAAC,WAAW,CAA4B;IAC/C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAuB;gBAE5C,OAAO,EAAE,qBAAqB;IAkB1C;;OAEG;IACH,OAAO,CAAC,aAAa;IAIrB;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,cAAc,CAClB,EAAE,EAAE,SAAS,EACb,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,cAAc,CAAC,EAAE,cAAc,CAAC;QAChC,cAAc,CAAC,EAAE,qBAAqB,CAAC;KACxC,GACA,OAAO,CAAC,aAAa,CAAC;IA0DzB,OAAO,KAAK,MAAM,GAEjB;IAED;;;;;OAKG;IACH,eAAe,CAAC,EAAE,EAAE,SAAS,GAAG,OAAO;IAIvC;;;;;OAKG;IACH,eAAe,CAAC,EAAE,EAAE,SAAS,GAAG,MAAM,GAAG,SAAS;IAIlD;;;;;OAKG;IACH,cAAc,IAAI,UAAU,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC;IAIlE;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAWpD;;;;;;;;;;OAUG;IACH,aAAa,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO;IAShD;;;;;OAKG;YACW,WAAW;IAoBzB;;;;;OAKG;YACW,WAAW;CAW1B"}
|
|
@@ -5,14 +5,17 @@
|
|
|
5
5
|
* - Load/save context state from/to database
|
|
6
6
|
* - Cache active contexts in memory
|
|
7
7
|
* - Create ephemeral contexts for one-off operations
|
|
8
|
+
* - Ensure thread-safe exclusive context ownership
|
|
8
9
|
*
|
|
9
10
|
*/
|
|
11
|
+
import { getPrefixedLogger } from "../../../util/logger.js";
|
|
10
12
|
import { CacheStrategyFactory, } from "./caching/index.js";
|
|
11
13
|
import { loadContextConfigFromEnv } from "./config.js";
|
|
14
|
+
import { ContextHandle, tryAcquireWithRetries, } from "./context-handle.js";
|
|
15
|
+
import { ContextLockError, ContextLockManager } from "./context-lock.js";
|
|
12
16
|
import { Context } from "./context.js";
|
|
13
|
-
import { createPrefixedLogger } from "./logger.js";
|
|
14
17
|
/**
|
|
15
|
-
* Manager for creating and managing conversation contexts.
|
|
18
|
+
* Manager for creating and managing conversation contexts with thread-safe ownership.
|
|
16
19
|
*/
|
|
17
20
|
export class ContextManager {
|
|
18
21
|
storage;
|
|
@@ -20,6 +23,7 @@ export class ContextManager {
|
|
|
20
23
|
contextOptions;
|
|
21
24
|
loggingOptions;
|
|
22
25
|
contexts = new Map();
|
|
26
|
+
lockManager = new ContextLockManager();
|
|
23
27
|
static logPrefix = "[context-manager]";
|
|
24
28
|
constructor(options) {
|
|
25
29
|
this.storage = options.storage;
|
|
@@ -34,48 +38,120 @@ export class ContextManager {
|
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
/**
|
|
37
|
-
*
|
|
41
|
+
* Creates a unique key for a context ID.
|
|
42
|
+
*/
|
|
43
|
+
getContextKey(id) {
|
|
44
|
+
return `${id.appId}::${id.userId}::${id.name}`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Acquires exclusive access to a context.
|
|
38
48
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* 2. Loaded from storage if not cached
|
|
42
|
-
* 3. Created fresh if not found in storage
|
|
43
|
-
* 4. Cached for subsequent requests
|
|
44
|
-
* 5. Automatically persisted on changes
|
|
49
|
+
* This method ensures thread-safe access to contexts by requiring
|
|
50
|
+
* an owner ID and returning a handle that must be released when done.
|
|
45
51
|
*
|
|
46
52
|
* @param id - Unique context identifier
|
|
47
|
-
* @param
|
|
48
|
-
* @
|
|
53
|
+
* @param ownerId - Unique identifier of the entity acquiring the context
|
|
54
|
+
* @param options - Context and acquisition options
|
|
55
|
+
* @returns ContextHandle with exclusive access to the context
|
|
56
|
+
* @throws {ContextLockError} if the context is locked and acquisition fails
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const handle = await manager.acquireContext(contextId, "api-subagent-1");
|
|
61
|
+
* try {
|
|
62
|
+
* handle.context.startTurn(userMessage);
|
|
63
|
+
* // ... use the context
|
|
64
|
+
* } finally {
|
|
65
|
+
* handle.release();
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
49
68
|
*/
|
|
50
|
-
async
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
69
|
+
async acquireContext(id, ownerId, options) {
|
|
70
|
+
const key = this.getContextKey(id);
|
|
71
|
+
// Try to acquire the lock
|
|
72
|
+
const acquired = await tryAcquireWithRetries(this.lockManager, id, ownerId, options?.acquireOptions);
|
|
73
|
+
if (!acquired) {
|
|
74
|
+
const currentOwner = this.lockManager.getOwner(id);
|
|
75
|
+
throw new ContextLockError(id, currentOwner ?? "unknown", ownerId);
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
// Get or create the context while holding the lock
|
|
79
|
+
let context = this.contexts.get(key);
|
|
80
|
+
if (!context) {
|
|
81
|
+
const onPersist = async (record) => {
|
|
82
|
+
await this.saveContext(id, record);
|
|
83
|
+
};
|
|
84
|
+
// Create cache strategy
|
|
85
|
+
const cacheStrategy = CacheStrategyFactory.create(this.cacheStrategyType);
|
|
86
|
+
// Load from storage
|
|
87
|
+
const record = await this.loadContext(id);
|
|
88
|
+
// Create context with persistence callback, cache strategy, and logging
|
|
89
|
+
const params = {
|
|
90
|
+
id,
|
|
91
|
+
options: { ...options?.contextOptions, ...this.contextOptions },
|
|
92
|
+
logging: this.loggingOptions,
|
|
93
|
+
onPersist,
|
|
94
|
+
cacheStrategy,
|
|
95
|
+
};
|
|
96
|
+
context = record
|
|
97
|
+
? Context.fromRecord(record, params)
|
|
98
|
+
: new Context(params);
|
|
99
|
+
// Cache for future requests
|
|
100
|
+
this.contexts.set(key, context);
|
|
101
|
+
}
|
|
102
|
+
return new ContextHandle(context, id, ownerId, this.lockManager);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
// Release the lock if context creation fails
|
|
106
|
+
this.lockManager.release(id, ownerId);
|
|
107
|
+
throw error;
|
|
54
108
|
}
|
|
55
|
-
const onPersist = async (record) => {
|
|
56
|
-
await this.saveContext(id, record);
|
|
57
|
-
};
|
|
58
|
-
// Create cache strategy
|
|
59
|
-
const cacheStrategy = CacheStrategyFactory.create(this.cacheStrategyType);
|
|
60
|
-
// Load from storage
|
|
61
|
-
const record = await this.loadContext(id);
|
|
62
|
-
// Create context with persistence callback, cache strategy, and logging
|
|
63
|
-
const params = {
|
|
64
|
-
id,
|
|
65
|
-
options: { ...options, ...this.contextOptions },
|
|
66
|
-
logging: this.loggingOptions,
|
|
67
|
-
onPersist,
|
|
68
|
-
cacheStrategy,
|
|
69
|
-
};
|
|
70
|
-
const context = record
|
|
71
|
-
? Context.fromRecord(record, params)
|
|
72
|
-
: new Context(params);
|
|
73
|
-
// Cache for future requests
|
|
74
|
-
this.contexts.set(id, context);
|
|
75
|
-
return context;
|
|
76
109
|
}
|
|
77
110
|
get logger() {
|
|
78
|
-
return
|
|
111
|
+
return getPrefixedLogger(ContextManager.logPrefix);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Checks if a context is currently locked.
|
|
115
|
+
*
|
|
116
|
+
* @param id - Context identifier
|
|
117
|
+
* @returns true if the context is locked
|
|
118
|
+
*/
|
|
119
|
+
isContextLocked(id) {
|
|
120
|
+
return this.lockManager.isLocked(id);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Gets the current owner of a context.
|
|
124
|
+
*
|
|
125
|
+
* @param id - Context identifier
|
|
126
|
+
* @returns The owner ID if locked, undefined otherwise
|
|
127
|
+
*/
|
|
128
|
+
getContextOwner(id) {
|
|
129
|
+
return this.lockManager.getOwner(id);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Gets information about all active context locks.
|
|
133
|
+
* Useful for debugging and monitoring.
|
|
134
|
+
*
|
|
135
|
+
* @returns Array of active lock information
|
|
136
|
+
*/
|
|
137
|
+
getActiveLocks() {
|
|
138
|
+
return this.lockManager.getActiveLocks();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Releases a context lock if owned by the specified owner.
|
|
142
|
+
* This is useful for cleanup in error scenarios.
|
|
143
|
+
*
|
|
144
|
+
* @param id - Context identifier
|
|
145
|
+
* @param ownerId - The owner releasing the context
|
|
146
|
+
*/
|
|
147
|
+
releaseContext(id, ownerId) {
|
|
148
|
+
try {
|
|
149
|
+
this.lockManager.release(id, ownerId);
|
|
150
|
+
this.logger.debug(`Released context '${id.name}' for owner '${ownerId}'`);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
this.logger.warn(`Failed to release context '${id.name}' for owner '${ownerId}': ${error}`);
|
|
154
|
+
}
|
|
79
155
|
}
|
|
80
156
|
/**
|
|
81
157
|
* Creates an ephemeral context (no persistence or caching).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../../src/ai-service/llm/context/manager.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../../src/ai-service/llm/context/manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EACL,oBAAoB,GAErB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EACL,aAAa,EACb,qBAAqB,GAEtB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAiBvC;;GAEG;AACH,MAAM,OAAO,cAAc;IACjB,OAAO,CAAiB;IACxB,iBAAiB,CAAqB;IACtC,cAAc,CAAkB;IAChC,cAAc,CAAkB;IAChC,QAAQ,GAAG,IAAI,GAAG,EAAmB,CAAC;IACtC,WAAW,GAAG,IAAI,kBAAkB,EAAE,CAAC;IACvC,MAAM,CAAU,SAAS,GAAG,mBAAmB,C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M,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;YAClD,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;gBACpB,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,kCAAkC,EAAE,CAAC,IAAI,oBAAoB,CAC9D,CAAC;gBACF,OAAO,IAAI,CAAC;YACd,CAAC;YAED,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,gCAAgC,EAAE,CAAC,IAAI,iBAAiB,CACzD,CAAC;YACF,OAAO,MAAuB,CAAC;QACjC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,EAAE,CAAC,IAAI,MAAM,GAAG,EAAE,CAAC,CAAC;YACrE,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACK,KAAK,CAAC,WAAW,CACvB,EAAa,EACb,MAAqB;QAErB,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YAC3C,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,sBAAsB,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC;QACtD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,EAAE,MAAM,GAAG,EAAE,CAAC,CAAC;QAClE,CAAC;IACH,CAAC"}
|
|
@@ -8,7 +8,7 @@ import type { ContentPart } from "../internal-types.js";
|
|
|
8
8
|
import type { ReasoningPart } from "@ai-sdk/provider-utils";
|
|
9
9
|
import type { TextPart, ToolCallPart, ToolResultPart } from "ai";
|
|
10
10
|
/**
|
|
11
|
-
* Compacts content
|
|
11
|
+
* Compacts content by trimming tool calls and multimedia.
|
|
12
12
|
*
|
|
13
13
|
* Used when demoting turns to L2 (moderate compression):
|
|
14
14
|
* - Text parts: Preserved unchanged
|
|
@@ -22,5 +22,5 @@ import type { TextPart, ToolCallPart, ToolResultPart } from "ai";
|
|
|
22
22
|
* @param parts - Content parts to compact
|
|
23
23
|
* @returns Compacted content parts
|
|
24
24
|
*/
|
|
25
|
-
export declare function
|
|
25
|
+
export declare function compactContent(content: string | ContentPart[]): (TextPart | ReasoningPart | ToolCallPart | ToolResultPart)[];
|
|
26
26
|
//# sourceMappingURL=content-compaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-compaction.d.ts","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/content-compaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,IAAI,CAAC;AAEjE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,
|
|
1
|
+
{"version":3,"file":"content-compaction.d.ts","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/content-compaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,IAAI,CAAC;AAEjE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,MAAM,GAAG,WAAW,EAAE,GAC9B,CAAC,QAAQ,GAAG,aAAa,GAAG,YAAY,GAAG,cAAc,CAAC,EAAE,CA6B9D"}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* removing tool call inputs and results while preserving the conversation structure.
|
|
6
6
|
*/
|
|
7
7
|
/**
|
|
8
|
-
* Compacts content
|
|
8
|
+
* Compacts content by trimming tool calls and multimedia.
|
|
9
9
|
*
|
|
10
10
|
* Used when demoting turns to L2 (moderate compression):
|
|
11
11
|
* - Text parts: Preserved unchanged
|
|
@@ -19,8 +19,11 @@
|
|
|
19
19
|
* @param parts - Content parts to compact
|
|
20
20
|
* @returns Compacted content parts
|
|
21
21
|
*/
|
|
22
|
-
export function
|
|
23
|
-
|
|
22
|
+
export function compactContent(content) {
|
|
23
|
+
if (typeof content === "string") {
|
|
24
|
+
return [{ type: "text", text: content }];
|
|
25
|
+
}
|
|
26
|
+
return content.flatMap((part) => {
|
|
24
27
|
if (part.type === "text" || part.type === "reasoning") {
|
|
25
28
|
return part;
|
|
26
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-compaction.js","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/content-compaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"content-compaction.js","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/content-compaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,cAAc,CAC5B,OAA+B;IAE/B,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAC9B,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YACtD,OAAO,IAAI,CAAC;QACd,CAAC;aAAM,IAAI,IAAI,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YACrC,OAAO;gBACL,IAAI,EAAE,WAAW;gBACjB,UAAU,EAAE,IAAI,CAAC,UAAU;gBAC3B,QAAQ,EAAE,IAAI,CAAC,QAAQ;gBACvB,KAAK,EAAE,EAAE;aACa,CAAC;QAC3B,CAAC;aAAM,IAAI,IAAI,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACvC,OAAO;gBACL,IAAI,EAAE,aAAa;gBACnB,UAAU,EAAE,IAAI,CAAC,UAAU;gBAC3B,QAAQ,EAAE,IAAI,CAAC,QAAQ;gBACvB,MAAM,EAAE;oBACN,IAAI,EAAE,MAAM;oBACZ,KAAK,EAAE,uBAAuB;iBAC/B;aACuB,CAAC;QAC7B,CAAC;aAAM,CAAC;YACN,yBAAyB;YACzB,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Internal utilities used by Context and Turn classes.
|
|
5
5
|
*/
|
|
6
6
|
export { estimateTokenCount, countChars as countChars, } from "./token-estimation.js";
|
|
7
|
-
export {
|
|
7
|
+
export { compactContent } from "./content-compaction.js";
|
|
8
8
|
export { normalizeToolResultOutput } from "./message-utils.js";
|
|
9
9
|
export { renderContext } from "./visualization.js";
|
|
10
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,kBAAkB,EAClB,UAAU,IAAI,UAAU,GACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,kBAAkB,EAClB,UAAU,IAAI,UAAU,GACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Internal utilities used by Context and Turn classes.
|
|
5
5
|
*/
|
|
6
6
|
export { estimateTokenCount, countChars as countChars, } from "./token-estimation.js";
|
|
7
|
-
export {
|
|
7
|
+
export { compactContent } from "./content-compaction.js";
|
|
8
8
|
export { normalizeToolResultOutput } from "./message-utils.js";
|
|
9
9
|
export { renderContext } from "./visualization.js";
|
|
10
10
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,kBAAkB,EAClB,UAAU,IAAI,UAAU,GACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,kBAAkB,EAClB,UAAU,IAAI,UAAU,GACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* Ensures messages conform to expected formats, particularly for tool results
|
|
5
5
|
* which must match the LanguageModelV2ToolResultOutput schema.
|
|
6
6
|
*/
|
|
7
|
+
import type { TokenizedAssistantMessage } from "../internal-types.js";
|
|
7
8
|
import type { LanguageModelV2ToolResultOutput } from "@ai-sdk/provider";
|
|
8
|
-
import type {
|
|
9
|
+
import type { ModelMessage } from "ai";
|
|
9
10
|
/**
|
|
10
11
|
* Validates and normalizes a tool result output to ensure it conforms to
|
|
11
12
|
* LanguageModelV2ToolResultOutput.
|
|
@@ -21,14 +22,23 @@ import type { AssistantModelMessage } from "ai";
|
|
|
21
22
|
*/
|
|
22
23
|
export declare function normalizeToolResultOutput(output: unknown): LanguageModelV2ToolResultOutput;
|
|
23
24
|
/**
|
|
24
|
-
* Ensures an assistant message's content starts with
|
|
25
|
-
*
|
|
25
|
+
* Ensures an assistant message's content starts with reasoning blocks.
|
|
26
|
+
* Reorders content so all reasoning blocks are first, as required by AWS Bedrock.
|
|
27
|
+
* If no reasoning blocks exist, prepends an empty one.
|
|
26
28
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
+
* AWS Bedrock requirement: "Thinking blocks must be the first content blocks
|
|
30
|
+
* in an assistant message" when using extended thinking with tool use.
|
|
29
31
|
*
|
|
30
32
|
* @param message - Assistant message to check and modify if needed
|
|
31
|
-
* @returns Assistant message guaranteed to start with a reasoning block
|
|
32
33
|
*/
|
|
33
|
-
export declare function prependReasoningBlockIfNeeded(message:
|
|
34
|
+
export declare function prependReasoningBlockIfNeeded(message: TokenizedAssistantMessage): void;
|
|
35
|
+
/** Checks if a message is empty.
|
|
36
|
+
* - For string content: empty if trimmed string length is 0.
|
|
37
|
+
* - For array content: empty if all parts are empty (textual parts
|
|
38
|
+
* must have non-empty trimmed text; other part types are non-empty).
|
|
39
|
+
*
|
|
40
|
+
* @param message - Message to check
|
|
41
|
+
* @returns True if message is empty, false otherwise
|
|
42
|
+
*/
|
|
43
|
+
export declare function isEmpty(message: ModelMessage): boolean;
|
|
34
44
|
//# sourceMappingURL=message-utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-utils.d.ts","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/message-utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"message-utils.d.ts","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/message-utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AACtE,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAEvC;;;;;;;;;;;;GAYG;AACH,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,OAAO,GACd,+BAA+B,CAuBjC;AAED;;;;;;;;;GASG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,EAAE,yBAAyB,QAuBnC;AAED;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAYtD"}
|
|
@@ -37,14 +37,14 @@ export function normalizeToolResultOutput(output) {
|
|
|
37
37
|
return output;
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
|
-
* Ensures an assistant message's content starts with
|
|
41
|
-
*
|
|
40
|
+
* Ensures an assistant message's content starts with reasoning blocks.
|
|
41
|
+
* Reorders content so all reasoning blocks are first, as required by AWS Bedrock.
|
|
42
|
+
* If no reasoning blocks exist, prepends an empty one.
|
|
42
43
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
44
|
+
* AWS Bedrock requirement: "Thinking blocks must be the first content blocks
|
|
45
|
+
* in an assistant message" when using extended thinking with tool use.
|
|
45
46
|
*
|
|
46
47
|
* @param message - Assistant message to check and modify if needed
|
|
47
|
-
* @returns Assistant message guaranteed to start with a reasoning block
|
|
48
48
|
*/
|
|
49
49
|
export function prependReasoningBlockIfNeeded(message) {
|
|
50
50
|
if (typeof message.content == "string") {
|
|
@@ -55,19 +55,32 @@ export function prependReasoningBlockIfNeeded(message) {
|
|
|
55
55
|
},
|
|
56
56
|
];
|
|
57
57
|
}
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
const reasoningBlocks = message.content.filter((part) => part.type === "reasoning");
|
|
59
|
+
const otherBlocks = message.content.filter((part) => part.type !== "reasoning");
|
|
60
|
+
if (reasoningBlocks.length === 0) {
|
|
61
|
+
reasoningBlocks.push({ type: "reasoning", text: "" });
|
|
61
62
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
63
|
+
message.content = [...reasoningBlocks, ...otherBlocks];
|
|
64
|
+
}
|
|
65
|
+
/** Checks if a message is empty.
|
|
66
|
+
* - For string content: empty if trimmed string length is 0.
|
|
67
|
+
* - For array content: empty if all parts are empty (textual parts
|
|
68
|
+
* must have non-empty trimmed text; other part types are non-empty).
|
|
69
|
+
*
|
|
70
|
+
* @param message - Message to check
|
|
71
|
+
* @returns True if message is empty, false otherwise
|
|
72
|
+
*/
|
|
73
|
+
export function isEmpty(message) {
|
|
74
|
+
if (typeof message.content === "string") {
|
|
75
|
+
return message.content.trim().length === 0;
|
|
76
|
+
}
|
|
77
|
+
const nonEmptyParts = message.content.filter((part) => {
|
|
78
|
+
if ("text" in part) {
|
|
79
|
+
return part.text.trim().length > 0;
|
|
80
|
+
}
|
|
81
|
+
// non-textual content parts are never considered empty
|
|
82
|
+
return true;
|
|
83
|
+
});
|
|
84
|
+
return nonEmptyParts.length === 0;
|
|
72
85
|
}
|
|
73
86
|
//# sourceMappingURL=message-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-utils.js","sourceRoot":"","sources":["../../../../../src/ai-service/llm/context/utils/message-utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"message-utils.js","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.js";
|
|
3
3
|
import { LLMObsTracer } from "./tracer.js";
|
|
4
4
|
import type { LLMObsSpan } from "./types.js";
|
|
5
|
+
import type { RetryOptions } from "../util/retry-on-timeout.js";
|
|
5
6
|
export declare function traceWorkflow<T>(name: string, operation: (span: any) => Promise<T>, tags?: Record<string, string | boolean>, llmobs?: LLMObsTracer): Promise<T>;
|
|
6
7
|
export declare function traceTask<T>(name: string, operation: (span: any) => Promise<T>, tags?: Record<string, string | boolean>, llmobs?: LLMObsTracer): Promise<T>;
|
|
7
8
|
export declare function traceLLM<T>(name: string, modelName: string, modelProvider: string, operation: (span: LLMObsSpan) => Promise<T>, tags?: Record<string, string | boolean>, llmobs?: LLMObsTracer): Promise<T>;
|
|
8
9
|
export declare function getJwtTraceTags(jwt: string | undefined): Record<string, string>;
|
|
9
10
|
export declare function getCommonTraceTags(jwt: string | undefined, applicationId: string, organizationId: string, additionalTags?: Record<string, string | boolean>): Record<string, string | boolean>;
|
|
10
11
|
/**
|
|
11
|
-
* Instruments ai.streamText with LLMObs tracing.
|
|
12
|
+
* Instruments ai.streamText with LLMObs tracing and retry logic.
|
|
12
13
|
* Wraps user-provided callbacks to ensure spans are finished correctly.
|
|
13
14
|
* Captures input/output data, metrics, errors, and metadata for each step and the overall call.
|
|
15
|
+
* Automatically retries on timeout errors with exponential backoff.
|
|
16
|
+
*
|
|
17
|
+
* @param params - Parameters to pass to streamText
|
|
18
|
+
* @param llmobs - LLMObs tracer instance
|
|
19
|
+
* @param logger - Logger instance
|
|
20
|
+
* @param retryOptions - Optional retry configuration (defaults to 3 retries with exponential backoff)
|
|
14
21
|
*/
|
|
15
|
-
export declare function tracedStreamText<T extends ToolSet>(params: Parameters<typeof streamText<T>>[0], llmobs?: LLMObsTracer, logger?: Logger): import("ai").StreamTextResult<T, never>;
|
|
22
|
+
export declare function tracedStreamText<T extends ToolSet>(params: Parameters<typeof streamText<T>>[0], llmobs?: LLMObsTracer, logger?: Logger, retryOptions?: RetryOptions): import("ai").StreamTextResult<T, never>;
|
|
16
23
|
export declare const traceToolSet: <T extends ToolSet>(toolset: T, llmobs?: LLMObsTracer, hooks?: {
|
|
17
24
|
beforeExecute?: (name: string) => void;
|
|
18
25
|
afterExecute?: (name: string, span: LLMObsSpan) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/ai-service/llmobs/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,OAAO,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,EAAE,KAAK,MAAM,EAAa,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/ai-service/llmobs/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,OAAO,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,EAAE,KAAK,MAAM,EAAa,MAAM,sBAAsB,CAAC;AAK9D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAGhE,wBAAgB,aAAa,CAAC,CAAC,EAC7B,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,EACpC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,EACvC,MAAM,GAAE,YAAoC,GAC3C,OAAO,CAAC,CAAC,CAAC,CAaZ;AAGD,wBAAgB,SAAS,CAAC,CAAC,EACzB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,EACpC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAA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D,KACA,CA8CF,CAAC"}
|
|
@@ -2,6 +2,7 @@ import { streamText } from "ai";
|
|
|
2
2
|
import { getLogger } from "../../util/logger.js";
|
|
3
3
|
import { parseJwt } from "../util/parse-jwt.js";
|
|
4
4
|
import { safeJsonStringify } from "../util/safe-stringify.js";
|
|
5
|
+
import { RetryMiddleware } from "./middleware/retry.js";
|
|
5
6
|
import { LLMObsStreamTextMiddleware } from "./middleware/stream-text.js";
|
|
6
7
|
import { LLMObsTracer } from "./tracer.js";
|
|
7
8
|
// Helper function for workflow tracing with common patterns
|
|
@@ -69,13 +70,25 @@ export function getCommonTraceTags(jwt, applicationId, organizationId, additiona
|
|
|
69
70
|
return result;
|
|
70
71
|
}
|
|
71
72
|
/**
|
|
72
|
-
* Instruments ai.streamText with LLMObs tracing.
|
|
73
|
+
* Instruments ai.streamText with LLMObs tracing and retry logic.
|
|
73
74
|
* Wraps user-provided callbacks to ensure spans are finished correctly.
|
|
74
75
|
* Captures input/output data, metrics, errors, and metadata for each step and the overall call.
|
|
76
|
+
* Automatically retries on timeout errors with exponential backoff.
|
|
77
|
+
*
|
|
78
|
+
* @param params - Parameters to pass to streamText
|
|
79
|
+
* @param llmobs - LLMObs tracer instance
|
|
80
|
+
* @param logger - Logger instance
|
|
81
|
+
* @param retryOptions - Optional retry configuration (defaults to 3 retries with exponential backoff)
|
|
75
82
|
*/
|
|
76
|
-
export function tracedStreamText(params, llmobs = LLMObsTracer.instance, logger = getLogger()) {
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
export function tracedStreamText(params, llmobs = LLMObsTracer.instance, logger = getLogger(), retryOptions = {}) {
|
|
84
|
+
// Compose middlewares: retry first (inner), then LLMObs tracing (outer)
|
|
85
|
+
// This ensures retries happen before tracing sees the error
|
|
86
|
+
const retryMiddleware = new RetryMiddleware(retryOptions, logger);
|
|
87
|
+
const llmobsMiddleware = new LLMObsStreamTextMiddleware(llmobs, logger);
|
|
88
|
+
// Apply retry middleware first, then LLMObs tracing
|
|
89
|
+
const retriedStreamText = retryMiddleware.wrap(streamText);
|
|
90
|
+
const tracedStreamText = llmobsMiddleware.wrap(retriedStreamText);
|
|
91
|
+
return tracedStreamText(params);
|
|
79
92
|
}
|
|
80
93
|
export const traceToolSet = (toolset, llmobs = LLMObsTracer.instance, hooks) => {
|
|
81
94
|
const tracedToolset = {};
|