@inkeep/agents-run-api 0.39.4 → 0.40.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/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/system-prompt.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/thinking-preparation.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/tool.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-component.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-components.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/system-prompt.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact-retrieval-guidance.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact.js +5 -0
- package/dist/a2a/client.d.ts +184 -0
- package/dist/a2a/client.js +510 -0
- package/dist/a2a/handlers.d.ts +7 -0
- package/dist/a2a/handlers.js +560 -0
- package/dist/a2a/transfer.d.ts +22 -0
- package/dist/a2a/transfer.js +46 -0
- package/dist/a2a/types.d.ts +79 -0
- package/dist/a2a/types.js +22 -0
- package/dist/agents/Agent.d.ts +266 -0
- package/dist/agents/Agent.js +1927 -0
- package/dist/agents/ModelFactory.d.ts +63 -0
- package/dist/agents/ModelFactory.js +194 -0
- package/dist/agents/SystemPromptBuilder.d.ts +21 -0
- package/dist/agents/SystemPromptBuilder.js +48 -0
- package/dist/agents/ToolSessionManager.d.ts +63 -0
- package/dist/agents/ToolSessionManager.js +146 -0
- package/dist/agents/generateTaskHandler.d.ts +49 -0
- package/dist/agents/generateTaskHandler.js +521 -0
- package/dist/agents/relationTools.d.ts +57 -0
- package/dist/agents/relationTools.js +262 -0
- package/dist/agents/types.d.ts +28 -0
- package/dist/agents/types.js +1 -0
- package/dist/agents/versions/v1/Phase1Config.d.ts +27 -0
- package/dist/agents/versions/v1/Phase1Config.js +424 -0
- package/dist/agents/versions/v1/Phase2Config.d.ts +31 -0
- package/dist/agents/versions/v1/Phase2Config.js +330 -0
- package/dist/constants/execution-limits/defaults.d.ts +51 -0
- package/dist/constants/execution-limits/defaults.js +52 -0
- package/dist/constants/execution-limits/index.d.ts +6 -0
- package/dist/constants/execution-limits/index.js +21 -0
- package/dist/create-app.d.ts +9 -0
- package/dist/create-app.js +195 -0
- package/dist/data/agent.d.ts +7 -0
- package/dist/data/agent.js +72 -0
- package/dist/data/agents.d.ts +34 -0
- package/dist/data/agents.js +139 -0
- package/dist/data/conversations.d.ts +128 -0
- package/dist/data/conversations.js +522 -0
- package/dist/data/db/dbClient.d.ts +6 -0
- package/dist/data/db/dbClient.js +17 -0
- package/dist/env.d.ts +57 -0
- package/dist/env.js +1 -2
- package/dist/handlers/executionHandler.d.ts +39 -0
- package/dist/handlers/executionHandler.js +456 -0
- package/dist/index.d.ts +8 -29
- package/dist/index.js +5 -11235
- package/dist/instrumentation.d.ts +1 -2
- package/dist/instrumentation.js +66 -3
- package/dist/{logger2.js → logger.d.ts} +1 -2
- package/dist/logger.js +1 -1
- package/dist/middleware/api-key-auth.d.ts +26 -0
- package/dist/middleware/api-key-auth.js +240 -0
- package/dist/middleware/index.d.ts +2 -0
- package/dist/middleware/index.js +3 -0
- package/dist/openapi.d.ts +4 -0
- package/dist/openapi.js +54 -0
- package/dist/routes/agents.d.ts +12 -0
- package/dist/routes/agents.js +147 -0
- package/dist/routes/chat.d.ts +13 -0
- package/dist/routes/chat.js +293 -0
- package/dist/routes/chatDataStream.d.ts +13 -0
- package/dist/routes/chatDataStream.js +352 -0
- package/dist/routes/mcp.d.ts +13 -0
- package/dist/routes/mcp.js +495 -0
- package/dist/services/AgentSession.d.ts +356 -0
- package/dist/services/AgentSession.js +1208 -0
- package/dist/services/ArtifactParser.d.ts +105 -0
- package/dist/services/ArtifactParser.js +338 -0
- package/dist/services/ArtifactService.d.ts +123 -0
- package/dist/services/ArtifactService.js +612 -0
- package/dist/services/BaseCompressor.d.ts +183 -0
- package/dist/services/BaseCompressor.js +504 -0
- package/dist/services/ConversationCompressor.d.ts +32 -0
- package/dist/services/ConversationCompressor.js +91 -0
- package/dist/services/IncrementalStreamParser.d.ts +98 -0
- package/dist/services/IncrementalStreamParser.js +327 -0
- package/dist/services/MidGenerationCompressor.d.ts +63 -0
- package/dist/services/MidGenerationCompressor.js +104 -0
- package/dist/services/PendingToolApprovalManager.d.ts +62 -0
- package/dist/services/PendingToolApprovalManager.js +133 -0
- package/dist/services/ResponseFormatter.d.ts +39 -0
- package/dist/services/ResponseFormatter.js +152 -0
- package/dist/tools/NativeSandboxExecutor.d.ts +38 -0
- package/dist/tools/NativeSandboxExecutor.js +432 -0
- package/dist/tools/SandboxExecutorFactory.d.ts +36 -0
- package/dist/tools/SandboxExecutorFactory.js +80 -0
- package/dist/tools/VercelSandboxExecutor.d.ts +71 -0
- package/dist/tools/VercelSandboxExecutor.js +340 -0
- package/dist/tools/distill-conversation-history-tool.d.ts +62 -0
- package/dist/tools/distill-conversation-history-tool.js +206 -0
- package/dist/tools/distill-conversation-tool.d.ts +41 -0
- package/dist/tools/distill-conversation-tool.js +141 -0
- package/dist/tools/sandbox-utils.d.ts +18 -0
- package/dist/tools/sandbox-utils.js +53 -0
- package/dist/types/chat.d.ts +27 -0
- package/dist/types/chat.js +1 -0
- package/dist/types/execution-context.d.ts +46 -0
- package/dist/types/execution-context.js +27 -0
- package/dist/types/xml.d.ts +5 -0
- package/dist/utils/SchemaProcessor.d.ts +52 -0
- package/dist/utils/SchemaProcessor.js +182 -0
- package/dist/utils/agent-operations.d.ts +62 -0
- package/dist/utils/agent-operations.js +53 -0
- package/dist/utils/artifact-component-schema.d.ts +42 -0
- package/dist/utils/artifact-component-schema.js +186 -0
- package/dist/utils/cleanup.d.ts +21 -0
- package/dist/utils/cleanup.js +59 -0
- package/dist/utils/data-component-schema.d.ts +2 -0
- package/dist/utils/data-component-schema.js +3 -0
- package/dist/utils/default-status-schemas.d.ts +20 -0
- package/dist/utils/default-status-schemas.js +24 -0
- package/dist/utils/json-postprocessor.d.ts +13 -0
- package/dist/{json-postprocessor.cjs → utils/json-postprocessor.js} +1 -2
- package/dist/utils/model-context-utils.d.ts +39 -0
- package/dist/utils/model-context-utils.js +181 -0
- package/dist/utils/model-resolver.d.ts +6 -0
- package/dist/utils/model-resolver.js +34 -0
- package/dist/utils/schema-validation.d.ts +44 -0
- package/dist/utils/schema-validation.js +97 -0
- package/dist/utils/stream-helpers.d.ts +197 -0
- package/dist/utils/stream-helpers.js +518 -0
- package/dist/utils/stream-registry.d.ts +22 -0
- package/dist/utils/stream-registry.js +34 -0
- package/dist/utils/token-estimator.d.ts +69 -0
- package/dist/utils/token-estimator.js +53 -0
- package/dist/utils/tracer.d.ts +7 -0
- package/dist/utils/tracer.js +7 -0
- package/package.json +5 -20
- package/dist/SandboxExecutorFactory.cjs +0 -895
- package/dist/SandboxExecutorFactory.js +0 -893
- package/dist/SandboxExecutorFactory.js.map +0 -1
- package/dist/chunk-VBDAOXYI.cjs +0 -927
- package/dist/chunk-VBDAOXYI.js +0 -832
- package/dist/chunk-VBDAOXYI.js.map +0 -1
- package/dist/chunk.cjs +0 -34
- package/dist/conversations.cjs +0 -7
- package/dist/conversations.js +0 -7
- package/dist/conversations2.cjs +0 -209
- package/dist/conversations2.js +0 -180
- package/dist/conversations2.js.map +0 -1
- package/dist/dbClient.cjs +0 -9676
- package/dist/dbClient.js +0 -9670
- package/dist/dbClient.js.map +0 -1
- package/dist/dbClient2.cjs +0 -5
- package/dist/dbClient2.js +0 -5
- package/dist/env.cjs +0 -59
- package/dist/env.js.map +0 -1
- package/dist/execution-limits.cjs +0 -260
- package/dist/execution-limits.js +0 -63
- package/dist/execution-limits.js.map +0 -1
- package/dist/index.cjs +0 -11260
- package/dist/index.d.cts +0 -36
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/instrumentation.cjs +0 -12
- package/dist/instrumentation.d.cts +0 -18
- package/dist/instrumentation.d.cts.map +0 -1
- package/dist/instrumentation.d.ts.map +0 -1
- package/dist/instrumentation2.cjs +0 -116
- package/dist/instrumentation2.js +0 -69
- package/dist/instrumentation2.js.map +0 -1
- package/dist/json-postprocessor.js +0 -20
- package/dist/json-postprocessor.js.map +0 -1
- package/dist/logger.cjs +0 -5
- package/dist/logger2.cjs +0 -1
- package/dist/nodefs.cjs +0 -29
- package/dist/nodefs.js +0 -27
- package/dist/nodefs.js.map +0 -1
- package/dist/opfs-ahp.cjs +0 -367
- package/dist/opfs-ahp.js +0 -368
- package/dist/opfs-ahp.js.map +0 -1
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { ArtifactFullData, ArtifactService, ArtifactSummaryData } from "./ArtifactService.js";
|
|
2
|
+
import { ArtifactComponentApiInsert } from "@inkeep/agents-core";
|
|
3
|
+
|
|
4
|
+
//#region src/services/ArtifactParser.d.ts
|
|
5
|
+
interface StreamPart {
|
|
6
|
+
kind: 'text' | 'data';
|
|
7
|
+
text?: string;
|
|
8
|
+
data?: any;
|
|
9
|
+
}
|
|
10
|
+
type ArtifactData = {
|
|
11
|
+
artifactId: string;
|
|
12
|
+
toolCallId: string;
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
type: string;
|
|
16
|
+
summary: Record<string, any>;
|
|
17
|
+
};
|
|
18
|
+
interface ArtifactCreateAnnotation {
|
|
19
|
+
artifactId: string;
|
|
20
|
+
toolCallId: string;
|
|
21
|
+
type: string;
|
|
22
|
+
baseSelector: string;
|
|
23
|
+
detailsSelector?: Record<string, string>;
|
|
24
|
+
raw?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Artifact parser focused on parsing and text processing responsibilities
|
|
28
|
+
* Delegates business logic operations to ArtifactService
|
|
29
|
+
* Handles artifact tag detection, parsing, and text boundary detection
|
|
30
|
+
*/
|
|
31
|
+
declare class ArtifactParser {
|
|
32
|
+
private static readonly ARTIFACT_CHECK_REGEX;
|
|
33
|
+
private static readonly ATTR_REGEX;
|
|
34
|
+
private static readonly ARTIFACT_PATTERNS;
|
|
35
|
+
private static readonly INCOMPLETE_CREATE_REGEX;
|
|
36
|
+
private artifactService;
|
|
37
|
+
constructor(tenantId: string, options?: {
|
|
38
|
+
sessionId?: string;
|
|
39
|
+
taskId?: string;
|
|
40
|
+
projectId?: string;
|
|
41
|
+
contextId?: string;
|
|
42
|
+
artifactComponents?: ArtifactComponentApiInsert[];
|
|
43
|
+
streamRequestId?: string;
|
|
44
|
+
subAgentId?: string;
|
|
45
|
+
artifactService?: ArtifactService;
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* Check if text contains complete artifact markers (ref or create)
|
|
49
|
+
*/
|
|
50
|
+
hasArtifactMarkers(text: string): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Check if text has incomplete artifact marker (for streaming)
|
|
53
|
+
* More robust detection that handles streaming fragments
|
|
54
|
+
*/
|
|
55
|
+
hasIncompleteArtifact(text: string): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Find safe text boundary before incomplete artifacts (for streaming)
|
|
58
|
+
* Enhanced to handle streaming chunks that split in the middle of artifacts
|
|
59
|
+
*/
|
|
60
|
+
findSafeTextBoundary(text: string): number;
|
|
61
|
+
/**
|
|
62
|
+
* Get all artifacts for a context - delegates to service
|
|
63
|
+
*/
|
|
64
|
+
getContextArtifacts(contextId: string): Promise<Map<string, any>>;
|
|
65
|
+
/**
|
|
66
|
+
* Parse attributes from the artifact:create tag
|
|
67
|
+
*/
|
|
68
|
+
private parseCreateAttributes;
|
|
69
|
+
/**
|
|
70
|
+
* Parse artifact creation annotations from text
|
|
71
|
+
*/
|
|
72
|
+
private parseCreateAnnotations;
|
|
73
|
+
/**
|
|
74
|
+
* Extract artifact data from a create annotation - delegates to service
|
|
75
|
+
*/
|
|
76
|
+
private extractFromCreateAnnotation;
|
|
77
|
+
/**
|
|
78
|
+
* Parse text with artifact markers into parts array
|
|
79
|
+
* Handles both artifact:ref and artifact:create tags
|
|
80
|
+
* Can work with or without pre-fetched artifact map
|
|
81
|
+
*/
|
|
82
|
+
parseText(text: string, artifactMap?: Map<string, any>, subAgentId?: string): Promise<StreamPart[]>;
|
|
83
|
+
/**
|
|
84
|
+
* Process object/dataComponents for artifact components
|
|
85
|
+
*/
|
|
86
|
+
parseObject(obj: any, artifactMap?: Map<string, any>, subAgentId?: string): Promise<StreamPart[]>;
|
|
87
|
+
/**
|
|
88
|
+
* Check if object is an artifact component
|
|
89
|
+
*/
|
|
90
|
+
private isArtifactComponent;
|
|
91
|
+
/**
|
|
92
|
+
* Check if object is an artifact create component
|
|
93
|
+
*/
|
|
94
|
+
private isArtifactCreateComponent;
|
|
95
|
+
/**
|
|
96
|
+
* Extract artifact from ArtifactCreate component
|
|
97
|
+
*/
|
|
98
|
+
private extractFromArtifactCreateComponent;
|
|
99
|
+
/**
|
|
100
|
+
* Get artifact data - delegates to service
|
|
101
|
+
*/
|
|
102
|
+
private getArtifactData;
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
export { ArtifactCreateAnnotation, ArtifactData, type ArtifactFullData, ArtifactParser, type ArtifactSummaryData, StreamPart };
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { getLogger } from "../logger.js";
|
|
2
|
+
import { ArtifactService } from "./ArtifactService.js";
|
|
3
|
+
|
|
4
|
+
//#region src/services/ArtifactParser.ts
|
|
5
|
+
const logger = getLogger("ArtifactParser");
|
|
6
|
+
/**
|
|
7
|
+
* Artifact parser focused on parsing and text processing responsibilities
|
|
8
|
+
* Delegates business logic operations to ArtifactService
|
|
9
|
+
* Handles artifact tag detection, parsing, and text boundary detection
|
|
10
|
+
*/
|
|
11
|
+
var ArtifactParser = class ArtifactParser {
|
|
12
|
+
static ARTIFACT_CHECK_REGEX = /<artifact:ref\s+(?=.*id=['"][^'"]+['"])(?=.*tool=['"][^'"]+['"])[^>]*\/>/;
|
|
13
|
+
static ATTR_REGEX = /(\w+)="([^"]*)"|(\w+)='([^']*)'|(\w+)=({[^}]+})/g;
|
|
14
|
+
static ARTIFACT_PATTERNS = [
|
|
15
|
+
"<a",
|
|
16
|
+
"<ar",
|
|
17
|
+
"<art",
|
|
18
|
+
"<arti",
|
|
19
|
+
"<artif",
|
|
20
|
+
"<artifa",
|
|
21
|
+
"<artifac",
|
|
22
|
+
"<artifact",
|
|
23
|
+
"<artifact:",
|
|
24
|
+
"<artifact:r",
|
|
25
|
+
"<artifact:re",
|
|
26
|
+
"<artifact:ref",
|
|
27
|
+
"<artifact:c",
|
|
28
|
+
"<artifact:cr",
|
|
29
|
+
"<artifact:cre",
|
|
30
|
+
"<artifact:crea",
|
|
31
|
+
"<artifact:creat",
|
|
32
|
+
"<artifact:create"
|
|
33
|
+
];
|
|
34
|
+
static INCOMPLETE_CREATE_REGEX = /<artifact:create(?![^>]*(?:\/>|<\/artifact:create>))/;
|
|
35
|
+
artifactService;
|
|
36
|
+
constructor(tenantId, options) {
|
|
37
|
+
if (options?.artifactService) this.artifactService = options.artifactService;
|
|
38
|
+
else this.artifactService = new ArtifactService({
|
|
39
|
+
tenantId,
|
|
40
|
+
...options
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Check if text contains complete artifact markers (ref or create)
|
|
45
|
+
*/
|
|
46
|
+
hasArtifactMarkers(text) {
|
|
47
|
+
const refMatch = ArtifactParser.ARTIFACT_CHECK_REGEX.test(text);
|
|
48
|
+
const createMatch = /<artifact:create\s+([^>]+?)(?:\s*\/)?>(?:(.*?)<\/artifact:create>)?/gs.test(text);
|
|
49
|
+
return refMatch || createMatch;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check if text has incomplete artifact marker (for streaming)
|
|
53
|
+
* More robust detection that handles streaming fragments
|
|
54
|
+
*/
|
|
55
|
+
hasIncompleteArtifact(text) {
|
|
56
|
+
return ArtifactParser.ARTIFACT_PATTERNS.some((pattern) => text.endsWith(pattern)) || /<artifact:ref[^>]+$/.test(text) || /<artifact:create[^>]*$/.test(text) || ArtifactParser.INCOMPLETE_CREATE_REGEX.test(text) && !text.includes("</artifact:create>") || this.findSafeTextBoundary(text) < text.length;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Find safe text boundary before incomplete artifacts (for streaming)
|
|
60
|
+
* Enhanced to handle streaming chunks that split in the middle of artifacts
|
|
61
|
+
*/
|
|
62
|
+
findSafeTextBoundary(text) {
|
|
63
|
+
for (const pattern of [/<artifact:ref(?![^>]*\/>).*$/, /<artifact:create(?![^>]*(?:\/>|<\/artifact:create>)).*$/]) {
|
|
64
|
+
const match = text.match(pattern);
|
|
65
|
+
if (match && match.index !== void 0) return match.index;
|
|
66
|
+
}
|
|
67
|
+
for (const pattern of ArtifactParser.ARTIFACT_PATTERNS) {
|
|
68
|
+
const lastIndex = text.lastIndexOf(pattern);
|
|
69
|
+
if (lastIndex !== -1) {
|
|
70
|
+
const textAfterPattern = text.slice(lastIndex);
|
|
71
|
+
if (!textAfterPattern.includes("/>") && !textAfterPattern.includes("</artifact:")) return lastIndex;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return text.length;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get all artifacts for a context - delegates to service
|
|
78
|
+
*/
|
|
79
|
+
async getContextArtifacts(contextId) {
|
|
80
|
+
return this.artifactService.getContextArtifacts(contextId);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Parse attributes from the artifact:create tag
|
|
84
|
+
*/
|
|
85
|
+
parseCreateAttributes(attrString) {
|
|
86
|
+
const attrs = {};
|
|
87
|
+
let match;
|
|
88
|
+
ArtifactParser.ATTR_REGEX.lastIndex = 0;
|
|
89
|
+
while ((match = ArtifactParser.ATTR_REGEX.exec(attrString)) !== null) {
|
|
90
|
+
const key = match[1] || match[3] || match[5];
|
|
91
|
+
let value = match[2] || match[4] || match[6];
|
|
92
|
+
if (value?.startsWith("{")) try {
|
|
93
|
+
value = JSON.parse(value);
|
|
94
|
+
} catch {}
|
|
95
|
+
attrs[key] = value;
|
|
96
|
+
}
|
|
97
|
+
if (!attrs.id || !attrs.tool || !attrs.type || !attrs.base) {
|
|
98
|
+
logger.warn({
|
|
99
|
+
attrs,
|
|
100
|
+
attrString
|
|
101
|
+
}, "Missing required attributes in artifact annotation");
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
artifactId: attrs.id,
|
|
106
|
+
toolCallId: attrs.tool,
|
|
107
|
+
type: attrs.type,
|
|
108
|
+
baseSelector: attrs.base,
|
|
109
|
+
detailsSelector: attrs.details || {}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Parse artifact creation annotations from text
|
|
114
|
+
*/
|
|
115
|
+
parseCreateAnnotations(text) {
|
|
116
|
+
const annotations = [];
|
|
117
|
+
const matches = [...text.matchAll(/<artifact:create\s+([^>]+?)(?:\s*\/)?>(?:(.*?)<\/artifact:create>)?/gs)];
|
|
118
|
+
for (const match of matches) {
|
|
119
|
+
const [fullMatch, attributes] = match;
|
|
120
|
+
const annotation = this.parseCreateAttributes(attributes);
|
|
121
|
+
if (annotation) {
|
|
122
|
+
annotation.raw = fullMatch;
|
|
123
|
+
annotations.push(annotation);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return annotations;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Extract artifact data from a create annotation - delegates to service
|
|
130
|
+
*/
|
|
131
|
+
async extractFromCreateAnnotation(annotation, subAgentId) {
|
|
132
|
+
const request = {
|
|
133
|
+
artifactId: annotation.artifactId,
|
|
134
|
+
toolCallId: annotation.toolCallId,
|
|
135
|
+
type: annotation.type,
|
|
136
|
+
baseSelector: annotation.baseSelector,
|
|
137
|
+
detailsSelector: annotation.detailsSelector
|
|
138
|
+
};
|
|
139
|
+
return this.artifactService.createArtifact(request, subAgentId);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Parse text with artifact markers into parts array
|
|
143
|
+
* Handles both artifact:ref and artifact:create tags
|
|
144
|
+
* Can work with or without pre-fetched artifact map
|
|
145
|
+
*/
|
|
146
|
+
async parseText(text, artifactMap, subAgentId) {
|
|
147
|
+
let processedText = text;
|
|
148
|
+
const createAnnotations = this.parseCreateAnnotations(text);
|
|
149
|
+
const createdArtifactData = /* @__PURE__ */ new Map();
|
|
150
|
+
const failedAnnotations = [];
|
|
151
|
+
for (const annotation of createAnnotations) try {
|
|
152
|
+
const artifactData = await this.extractFromCreateAnnotation(annotation, subAgentId);
|
|
153
|
+
if (artifactData && annotation.raw) createdArtifactData.set(annotation.raw, artifactData);
|
|
154
|
+
else if (annotation.raw) {
|
|
155
|
+
failedAnnotations.push(`Failed to create artifact "${annotation.artifactId}": Missing or invalid data`);
|
|
156
|
+
processedText = processedText.replace(annotation.raw, "");
|
|
157
|
+
logger.warn({
|
|
158
|
+
annotation,
|
|
159
|
+
artifactData
|
|
160
|
+
}, "Removed failed artifact:create annotation from output");
|
|
161
|
+
}
|
|
162
|
+
} catch (error) {
|
|
163
|
+
const errorMsg = error instanceof Error ? error.message : "Unknown error";
|
|
164
|
+
failedAnnotations.push(`Failed to create artifact "${annotation.artifactId}": ${errorMsg}`);
|
|
165
|
+
if (annotation.raw) processedText = processedText.replace(annotation.raw, "");
|
|
166
|
+
logger.error({
|
|
167
|
+
annotation,
|
|
168
|
+
error
|
|
169
|
+
}, "Failed to extract artifact from create annotation");
|
|
170
|
+
}
|
|
171
|
+
if (failedAnnotations.length > 0) logger.warn({
|
|
172
|
+
failedCount: failedAnnotations.length,
|
|
173
|
+
failures: failedAnnotations
|
|
174
|
+
}, "Some artifact creation attempts failed");
|
|
175
|
+
const parts = [];
|
|
176
|
+
const createRegex = /<artifact:create\s+([^>]+?)(?:\s*\/)?>(?:(.*?)<\/artifact:create>)?/gs;
|
|
177
|
+
const refRegex = /<artifact:ref\s+id=(["'])([^"']*?)\1\s+tool=(["'])([^"']*?)\3\s*\/>/gs;
|
|
178
|
+
const createMatches = [...text.matchAll(createRegex)];
|
|
179
|
+
const refMatches = [...processedText.matchAll(refRegex)];
|
|
180
|
+
const allMatches = [...createMatches.map((match) => ({
|
|
181
|
+
match,
|
|
182
|
+
type: "create"
|
|
183
|
+
})), ...refMatches.map((match) => ({
|
|
184
|
+
match,
|
|
185
|
+
type: "ref"
|
|
186
|
+
}))].sort((a, b) => (a.match.index || 0) - (b.match.index || 0));
|
|
187
|
+
if (allMatches.length === 0) return [{
|
|
188
|
+
kind: "text",
|
|
189
|
+
text: processedText
|
|
190
|
+
}];
|
|
191
|
+
let lastIndex = 0;
|
|
192
|
+
for (const { match, type } of allMatches) {
|
|
193
|
+
if (match.index === void 0) continue;
|
|
194
|
+
const matchStart = match.index;
|
|
195
|
+
const fullMatch = match[0];
|
|
196
|
+
if (matchStart > lastIndex) {
|
|
197
|
+
const textBefore = text.slice(lastIndex, matchStart);
|
|
198
|
+
if (textBefore) parts.push({
|
|
199
|
+
kind: "text",
|
|
200
|
+
text: textBefore
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
let artifactData = null;
|
|
204
|
+
if (type === "create") artifactData = createdArtifactData.get(fullMatch) || null;
|
|
205
|
+
else {
|
|
206
|
+
const [, , artifactId, , toolCallId] = match;
|
|
207
|
+
artifactData = await this.getArtifactData(artifactId, toolCallId, artifactMap);
|
|
208
|
+
}
|
|
209
|
+
if (artifactData) parts.push({
|
|
210
|
+
kind: "data",
|
|
211
|
+
data: {
|
|
212
|
+
artifactId: artifactData.artifactId,
|
|
213
|
+
toolCallId: artifactData.toolCallId,
|
|
214
|
+
name: artifactData.name,
|
|
215
|
+
description: artifactData.description,
|
|
216
|
+
type: artifactData.type,
|
|
217
|
+
artifactSummary: artifactData.data
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
lastIndex = matchStart + fullMatch.length;
|
|
221
|
+
}
|
|
222
|
+
if (lastIndex < text.length) {
|
|
223
|
+
const remainingText = text.slice(lastIndex);
|
|
224
|
+
if (remainingText) parts.push({
|
|
225
|
+
kind: "text",
|
|
226
|
+
text: remainingText
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
return parts;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Process object/dataComponents for artifact components
|
|
233
|
+
*/
|
|
234
|
+
async parseObject(obj, artifactMap, subAgentId) {
|
|
235
|
+
if (obj?.dataComponents && Array.isArray(obj.dataComponents)) {
|
|
236
|
+
const parts = [];
|
|
237
|
+
for (const component of obj.dataComponents) if (this.isArtifactComponent(component)) {
|
|
238
|
+
const artifactData = await this.getArtifactData(component.props.artifact_id, component.props.tool_call_id, artifactMap);
|
|
239
|
+
if (artifactData) parts.push({
|
|
240
|
+
kind: "data",
|
|
241
|
+
data: {
|
|
242
|
+
artifactId: artifactData.artifactId,
|
|
243
|
+
toolCallId: artifactData.toolCallId,
|
|
244
|
+
name: artifactData.name,
|
|
245
|
+
description: artifactData.description,
|
|
246
|
+
type: artifactData.type,
|
|
247
|
+
artifactSummary: artifactData.data
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
} else if (this.isArtifactCreateComponent(component)) {
|
|
251
|
+
const createData = await this.extractFromArtifactCreateComponent(component, subAgentId);
|
|
252
|
+
if (createData) parts.push({
|
|
253
|
+
kind: "data",
|
|
254
|
+
data: {
|
|
255
|
+
artifactId: createData.artifactId,
|
|
256
|
+
toolCallId: createData.toolCallId,
|
|
257
|
+
name: createData.name,
|
|
258
|
+
description: createData.description,
|
|
259
|
+
type: createData.type,
|
|
260
|
+
artifactSummary: createData.data
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
} else parts.push({
|
|
264
|
+
kind: "data",
|
|
265
|
+
data: component
|
|
266
|
+
});
|
|
267
|
+
return parts;
|
|
268
|
+
}
|
|
269
|
+
if (this.isArtifactComponent(obj)) {
|
|
270
|
+
const artifactData = await this.getArtifactData(obj.props.artifact_id, obj.props.tool_call_id, artifactMap);
|
|
271
|
+
return artifactData ? [{
|
|
272
|
+
kind: "data",
|
|
273
|
+
data: {
|
|
274
|
+
artifactId: artifactData.artifactId,
|
|
275
|
+
toolCallId: artifactData.toolCallId,
|
|
276
|
+
name: artifactData.name,
|
|
277
|
+
description: artifactData.description,
|
|
278
|
+
type: artifactData.type,
|
|
279
|
+
artifactSummary: artifactData.data
|
|
280
|
+
}
|
|
281
|
+
}] : [];
|
|
282
|
+
}
|
|
283
|
+
if (this.isArtifactCreateComponent(obj)) {
|
|
284
|
+
const createData = await this.extractFromArtifactCreateComponent(obj, subAgentId);
|
|
285
|
+
return createData ? [{
|
|
286
|
+
kind: "data",
|
|
287
|
+
data: {
|
|
288
|
+
artifactId: createData.artifactId,
|
|
289
|
+
toolCallId: createData.toolCallId,
|
|
290
|
+
name: createData.name,
|
|
291
|
+
description: createData.description,
|
|
292
|
+
type: createData.type,
|
|
293
|
+
artifactSummary: createData.data
|
|
294
|
+
}
|
|
295
|
+
}] : [];
|
|
296
|
+
}
|
|
297
|
+
return [{
|
|
298
|
+
kind: "data",
|
|
299
|
+
data: obj
|
|
300
|
+
}];
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Check if object is an artifact component
|
|
304
|
+
*/
|
|
305
|
+
isArtifactComponent(obj) {
|
|
306
|
+
return obj?.name === "Artifact" && obj?.props?.artifact_id && obj?.props?.tool_call_id;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Check if object is an artifact create component
|
|
310
|
+
*/
|
|
311
|
+
isArtifactCreateComponent(obj) {
|
|
312
|
+
return obj?.name?.startsWith("ArtifactCreate_") && obj?.props?.id && obj?.props?.tool_call_id;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Extract artifact from ArtifactCreate component
|
|
316
|
+
*/
|
|
317
|
+
async extractFromArtifactCreateComponent(component, subAgentId) {
|
|
318
|
+
const props = component.props;
|
|
319
|
+
if (!props) return null;
|
|
320
|
+
const annotation = {
|
|
321
|
+
artifactId: props.id,
|
|
322
|
+
toolCallId: props.tool_call_id,
|
|
323
|
+
type: props.type,
|
|
324
|
+
baseSelector: props.base_selector,
|
|
325
|
+
detailsSelector: props.details_selector || {}
|
|
326
|
+
};
|
|
327
|
+
return await this.extractFromCreateAnnotation(annotation, subAgentId);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Get artifact data - delegates to service
|
|
331
|
+
*/
|
|
332
|
+
async getArtifactData(artifactId, toolCallId, artifactMap) {
|
|
333
|
+
return await this.artifactService.getArtifactSummary(artifactId, toolCallId, artifactMap);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
//#endregion
|
|
338
|
+
export { ArtifactParser };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ArtifactComponentApiInsert } from "@inkeep/agents-core";
|
|
2
|
+
|
|
3
|
+
//#region src/services/ArtifactService.d.ts
|
|
4
|
+
interface ArtifactSummaryData {
|
|
5
|
+
artifactId: string;
|
|
6
|
+
toolCallId: string;
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
type?: string;
|
|
10
|
+
data: any;
|
|
11
|
+
}
|
|
12
|
+
interface ArtifactFullData {
|
|
13
|
+
artifactId: string;
|
|
14
|
+
toolCallId: string;
|
|
15
|
+
name: string;
|
|
16
|
+
description: string;
|
|
17
|
+
type?: string;
|
|
18
|
+
data: any;
|
|
19
|
+
}
|
|
20
|
+
interface ArtifactCreateRequest {
|
|
21
|
+
artifactId: string;
|
|
22
|
+
toolCallId: string;
|
|
23
|
+
type: string;
|
|
24
|
+
baseSelector: string;
|
|
25
|
+
detailsSelector?: Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
interface ArtifactServiceContext {
|
|
28
|
+
tenantId: string;
|
|
29
|
+
sessionId?: string;
|
|
30
|
+
taskId?: string;
|
|
31
|
+
projectId?: string;
|
|
32
|
+
contextId?: string;
|
|
33
|
+
artifactComponents?: ArtifactComponentApiInsert[];
|
|
34
|
+
streamRequestId?: string;
|
|
35
|
+
subAgentId?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Service class responsible for artifact business logic operations
|
|
39
|
+
* Handles database persistence, tool result extraction, and artifact management
|
|
40
|
+
* Separated from parsing concerns for better architecture
|
|
41
|
+
*/
|
|
42
|
+
declare class ArtifactService {
|
|
43
|
+
private context;
|
|
44
|
+
private createdArtifacts;
|
|
45
|
+
private static selectorCache;
|
|
46
|
+
constructor(context: ArtifactServiceContext);
|
|
47
|
+
/**
|
|
48
|
+
* Clear static caches to prevent memory leaks between sessions
|
|
49
|
+
*/
|
|
50
|
+
static clearCaches(): void;
|
|
51
|
+
/**
|
|
52
|
+
* Update artifact components in the context
|
|
53
|
+
*/
|
|
54
|
+
updateArtifactComponents(artifactComponents: ArtifactComponentApiInsert[]): void;
|
|
55
|
+
/**
|
|
56
|
+
* Get all artifacts for a context from database
|
|
57
|
+
*/
|
|
58
|
+
getContextArtifacts(contextId: string): Promise<Map<string, any>>;
|
|
59
|
+
/**
|
|
60
|
+
* Create artifact from tool result and request data
|
|
61
|
+
*/
|
|
62
|
+
createArtifact(request: ArtifactCreateRequest, subAgentId?: string): Promise<ArtifactSummaryData | null>;
|
|
63
|
+
/**
|
|
64
|
+
* Get artifact summary data by ID and tool call ID
|
|
65
|
+
*/
|
|
66
|
+
getArtifactSummary(artifactId: string, toolCallId: string, artifactMap?: Map<string, any>): Promise<ArtifactSummaryData | null>;
|
|
67
|
+
/**
|
|
68
|
+
* Get artifact full data by ID and tool call ID
|
|
69
|
+
*/
|
|
70
|
+
getArtifactFull(artifactId: string, toolCallId: string, artifactMap?: Map<string, any>): Promise<ArtifactFullData | null>;
|
|
71
|
+
/**
|
|
72
|
+
* Format raw artifact to standardized summary data format
|
|
73
|
+
*/
|
|
74
|
+
private formatArtifactSummaryData;
|
|
75
|
+
/**
|
|
76
|
+
* Format raw artifact to standardized full data format
|
|
77
|
+
*/
|
|
78
|
+
private formatArtifactFullData;
|
|
79
|
+
/**
|
|
80
|
+
* Validate extracted data against the schemas used for extraction
|
|
81
|
+
*/
|
|
82
|
+
private validateExtractedData;
|
|
83
|
+
/**
|
|
84
|
+
* Persist artifact to database vian agent session
|
|
85
|
+
*/
|
|
86
|
+
private persistArtifact;
|
|
87
|
+
/**
|
|
88
|
+
* Cache artifact for immediate access
|
|
89
|
+
*/
|
|
90
|
+
private cacheArtifact;
|
|
91
|
+
/**
|
|
92
|
+
* Sanitize JMESPath selector to fix common syntax issues (with caching)
|
|
93
|
+
*/
|
|
94
|
+
private sanitizeJMESPathSelector;
|
|
95
|
+
/**
|
|
96
|
+
* Save an already-created artifact directly to the database
|
|
97
|
+
* Used by AgentSession to save artifacts after name/description generation
|
|
98
|
+
*/
|
|
99
|
+
saveArtifact(artifact: {
|
|
100
|
+
artifactId: string;
|
|
101
|
+
name: string;
|
|
102
|
+
description: string;
|
|
103
|
+
type: string;
|
|
104
|
+
data: Record<string, any>;
|
|
105
|
+
summaryData?: Record<string, any>;
|
|
106
|
+
metadata?: Record<string, any>;
|
|
107
|
+
toolCallId?: string;
|
|
108
|
+
}): Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* Clean up over-escaped strings that have been through multiple JSON serialization cycles
|
|
111
|
+
*/
|
|
112
|
+
cleanEscapedContent(value: any): any;
|
|
113
|
+
/**
|
|
114
|
+
* Extract properties from data using schema-defined fields and custom selectors
|
|
115
|
+
*/
|
|
116
|
+
private extractPropsFromSchema;
|
|
117
|
+
/**
|
|
118
|
+
* Filter extracted props based on schema
|
|
119
|
+
*/
|
|
120
|
+
private filterBySchema;
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
export { ArtifactCreateRequest, ArtifactFullData, ArtifactService, ArtifactServiceContext, ArtifactSummaryData };
|