@strand-js/anthropic 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -57,12 +57,26 @@ data: ${JSON.stringify(data)}
57
57
 
58
58
  `);
59
59
  }
60
+ function normalizeRequest(req) {
61
+ const rawHeaders = req.headers ?? {};
62
+ const normalized = {
63
+ ...req,
64
+ headers: {
65
+ get: (name) => {
66
+ const val = rawHeaders[name.toLowerCase()];
67
+ return Array.isArray(val) ? val[0] ?? null : val ?? null;
68
+ }
69
+ }
70
+ };
71
+ return normalized;
72
+ }
60
73
  function createStrandHandler(config) {
61
74
  const client = new import_sdk.default({ apiKey: config.apiKey });
62
75
  const anthropicTools = (config.tools ?? []).map(toolToAnthropicTool);
63
76
  const maxSteps = config.maxSteps ?? 10;
64
77
  const rateLimiter = config.rateLimit ? new import_core2.RateLimiter(config.rateLimit) : null;
65
78
  return async (req, res) => {
79
+ const normalizedReq = normalizeRequest(req);
66
80
  const body = req.body;
67
81
  if (rateLimiter) {
68
82
  const ip = req.ip ?? "unknown";
@@ -85,7 +99,7 @@ function createStrandHandler(config) {
85
99
  }
86
100
  if (config.authorize) {
87
101
  try {
88
- await config.authorize(req);
102
+ await config.authorize(normalizedReq);
89
103
  } catch (err) {
90
104
  const message = err instanceof Error ? err.message : "Unauthorized";
91
105
  res.status(401).json({ error: message });
@@ -98,7 +112,7 @@ function createStrandHandler(config) {
98
112
  emit(res, "strand:start", { sessionId: (0, import_core2.generateId)(), requestId: (0, import_core2.generateId)() });
99
113
  try {
100
114
  const messages = body.messages;
101
- const system = typeof config.system === "function" ? await config.system(req) : config.system ?? "";
115
+ const system = typeof config.system === "function" ? await config.system(normalizedReq) : config.system ?? "";
102
116
  const conversation = messages.map((m) => ({
103
117
  role: m.role,
104
118
  content: m.content
@@ -164,7 +178,7 @@ function createStrandHandler(config) {
164
178
  completedTools.map(async (block) => {
165
179
  emit(res, "strand:tool-input-done", { toolCallId: block.id, input: block.input });
166
180
  try {
167
- const result = await config.onToolCall?.(block.name, block.input, { request: req });
181
+ const result = await config.onToolCall?.(block.name, block.input, { request: normalizedReq });
168
182
  emit(res, "strand:tool-result", { toolCallId: block.id, result });
169
183
  return { type: "tool_result", tool_use_id: block.id, content: JSON.stringify(result ?? null) };
170
184
  } catch (err) {
package/dist/index.mjs CHANGED
@@ -20,12 +20,26 @@ data: ${JSON.stringify(data)}
20
20
 
21
21
  `);
22
22
  }
23
+ function normalizeRequest(req) {
24
+ const rawHeaders = req.headers ?? {};
25
+ const normalized = {
26
+ ...req,
27
+ headers: {
28
+ get: (name) => {
29
+ const val = rawHeaders[name.toLowerCase()];
30
+ return Array.isArray(val) ? val[0] ?? null : val ?? null;
31
+ }
32
+ }
33
+ };
34
+ return normalized;
35
+ }
23
36
  function createStrandHandler(config) {
24
37
  const client = new Anthropic({ apiKey: config.apiKey });
25
38
  const anthropicTools = (config.tools ?? []).map(toolToAnthropicTool);
26
39
  const maxSteps = config.maxSteps ?? 10;
27
40
  const rateLimiter = config.rateLimit ? new RateLimiter(config.rateLimit) : null;
28
41
  return async (req, res) => {
42
+ const normalizedReq = normalizeRequest(req);
29
43
  const body = req.body;
30
44
  if (rateLimiter) {
31
45
  const ip = req.ip ?? "unknown";
@@ -48,7 +62,7 @@ function createStrandHandler(config) {
48
62
  }
49
63
  if (config.authorize) {
50
64
  try {
51
- await config.authorize(req);
65
+ await config.authorize(normalizedReq);
52
66
  } catch (err) {
53
67
  const message = err instanceof Error ? err.message : "Unauthorized";
54
68
  res.status(401).json({ error: message });
@@ -61,7 +75,7 @@ function createStrandHandler(config) {
61
75
  emit(res, "strand:start", { sessionId: generateId(), requestId: generateId() });
62
76
  try {
63
77
  const messages = body.messages;
64
- const system = typeof config.system === "function" ? await config.system(req) : config.system ?? "";
78
+ const system = typeof config.system === "function" ? await config.system(normalizedReq) : config.system ?? "";
65
79
  const conversation = messages.map((m) => ({
66
80
  role: m.role,
67
81
  content: m.content
@@ -127,7 +141,7 @@ function createStrandHandler(config) {
127
141
  completedTools.map(async (block) => {
128
142
  emit(res, "strand:tool-input-done", { toolCallId: block.id, input: block.input });
129
143
  try {
130
- const result = await config.onToolCall?.(block.name, block.input, { request: req });
144
+ const result = await config.onToolCall?.(block.name, block.input, { request: normalizedReq });
131
145
  emit(res, "strand:tool-result", { toolCallId: block.id, result });
132
146
  return { type: "tool_result", tool_use_id: block.id, content: JSON.stringify(result ?? null) };
133
147
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strand-js/anthropic",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "license": "MIT",
5
5
  "description": "Anthropic provider adapter for Strand",
6
6
  "main": "./dist/index.js",
@@ -18,7 +18,7 @@
18
18
  ],
19
19
  "dependencies": {
20
20
  "@anthropic-ai/sdk": "^0.40.0",
21
- "@strand-js/core": "0.1.5"
21
+ "@strand-js/core": "0.1.6"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/express": "^5.0.0",