-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathclient.ts
More file actions
89 lines (77 loc) · 3.04 KB
/
client.ts
File metadata and controls
89 lines (77 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import type { AgentCoreConfig, AgentPattern, ChunkParser, StreamCallback } from "./types"
import { parseStrandsChunk } from "./parsers/strands"
import { parseLanggraphChunk } from "./parsers/langgraph"
import { parseClaudeAgentSdkChunk } from "./parsers/claude-agent-sdk"
import { parseAguiChunk } from "./parsers/agui"
import { readSSEStream } from "./utils/sse"
/** Resolve parser from pattern prefix. Defaults to strands parser. */
function getParser(pattern: AgentPattern): ChunkParser {
if (pattern.startsWith("agui-")) return parseAguiChunk
if (pattern.startsWith("langgraph-")) return parseLanggraphChunk
if (pattern.startsWith("claude-")) return parseClaudeAgentSdkChunk
if (pattern.startsWith("strands-")) return parseStrandsChunk
return parseStrandsChunk
}
export class AgentCoreClient {
private runtimeArn: string
private region: string
private pattern: AgentPattern
private parser: ChunkParser
constructor(config: AgentCoreConfig) {
this.runtimeArn = config.runtimeArn
this.region = config.region ?? "us-east-1"
this.pattern = config.pattern
this.parser = getParser(config.pattern)
}
generateSessionId(): string {
return crypto.randomUUID()
}
async invoke(
query: string,
sessionId: string,
accessToken: string,
onEvent: StreamCallback
): Promise<void> {
if (!accessToken) throw new Error("No valid access token found.")
if (!this.runtimeArn) throw new Error("Agent Runtime ARN not configured.")
const endpoint = `https://bedrock-agentcore.${this.region}.amazonaws.com`
const escapedArn = encodeURIComponent(this.runtimeArn)
const url = `${endpoint}/runtimes/${escapedArn}/invocations?qualifier=DEFAULT`
const traceId = `1-${Math.floor(Date.now() / 1000).toString(16)}-${crypto.randomUUID()}`
// Build payload based on pattern — AG-UI protocol expects a different format
const body = this.pattern.startsWith("agui-")
? {
threadId: sessionId,
runId: crypto.randomUUID(),
messages: [{ id: crypto.randomUUID(), role: "user", content: query }],
state: {},
tools: [],
context: [],
forwardedProps: {},
}
: {
prompt: query,
runtimeSessionId: sessionId,
}
// User identity is extracted server-side from the validated JWT token
// (Authorization header), not sent in the payload body. This prevents
// impersonation via prompt injection.
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"X-Amzn-Trace-Id": traceId,
"Content-Type": "application/json",
"X-Amzn-Bedrock-AgentCore-Runtime-Session-Id": sessionId,
},
body: JSON.stringify(body),
})
if (!response.ok) {
const errorText = await response.text()
throw new Error(`HTTP ${response.status}: ${errorText}`)
}
await readSSEStream(response, this.parser, onEvent)
}
}