@strand-js/openai 0.1.5 → 0.1.7
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 +16 -3
- package/dist/index.mjs +16 -3
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -60,12 +60,25 @@ data: ${JSON.stringify(data)}
|
|
|
60
60
|
|
|
61
61
|
`);
|
|
62
62
|
}
|
|
63
|
+
function normalizeRequest(req) {
|
|
64
|
+
const rawHeaders = req.headers ?? {};
|
|
65
|
+
return {
|
|
66
|
+
...req,
|
|
67
|
+
headers: {
|
|
68
|
+
get: (name) => {
|
|
69
|
+
const val = rawHeaders[name.toLowerCase()];
|
|
70
|
+
return Array.isArray(val) ? val[0] ?? null : val ?? null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
63
75
|
function createStrandHandler(config) {
|
|
64
76
|
const client = new import_openai.default({ apiKey: config.apiKey });
|
|
65
77
|
const openAITools = (config.tools ?? []).map(toolToOpenAITool);
|
|
66
78
|
const maxSteps = config.maxSteps ?? 10;
|
|
67
79
|
const rateLimiter = config.rateLimit ? new import_core2.RateLimiter(config.rateLimit) : null;
|
|
68
80
|
return async (req, res) => {
|
|
81
|
+
const normalizedReq = normalizeRequest(req);
|
|
69
82
|
const body = req.body;
|
|
70
83
|
if (rateLimiter) {
|
|
71
84
|
const ip = req.ip ?? "unknown";
|
|
@@ -85,7 +98,7 @@ function createStrandHandler(config) {
|
|
|
85
98
|
}
|
|
86
99
|
if (config.authorize) {
|
|
87
100
|
try {
|
|
88
|
-
await config.authorize(
|
|
101
|
+
await config.authorize(normalizedReq);
|
|
89
102
|
} catch (err) {
|
|
90
103
|
const message = err instanceof Error ? err.message : "Unauthorized";
|
|
91
104
|
res.status(401).json({ error: message });
|
|
@@ -98,7 +111,7 @@ function createStrandHandler(config) {
|
|
|
98
111
|
emit(res, "strand:start", { sessionId: (0, import_core2.generateId)(), requestId: (0, import_core2.generateId)() });
|
|
99
112
|
try {
|
|
100
113
|
const messages = body.messages;
|
|
101
|
-
const system = typeof config.system === "function" ? await config.system(
|
|
114
|
+
const system = typeof config.system === "function" ? await config.system(normalizedReq) : config.system ?? "";
|
|
102
115
|
const conversation = [
|
|
103
116
|
...system ? [{ role: "system", content: system }] : [],
|
|
104
117
|
...messages.map((m) => ({ role: m.role, content: m.content }))
|
|
@@ -163,7 +176,7 @@ function createStrandHandler(config) {
|
|
|
163
176
|
completedTools.map(async (block) => {
|
|
164
177
|
emit(res, "strand:tool-input-done", { toolCallId: block.id, input: block.input });
|
|
165
178
|
try {
|
|
166
|
-
const result = await config.onToolCall?.(block.name, block.input, { request:
|
|
179
|
+
const result = await config.onToolCall?.(block.name, block.input, { request: normalizedReq });
|
|
167
180
|
emit(res, "strand:tool-result", { toolCallId: block.id, result });
|
|
168
181
|
return { role: "tool", tool_call_id: block.id, content: JSON.stringify(result ?? null) };
|
|
169
182
|
} catch (err) {
|
package/dist/index.mjs
CHANGED
|
@@ -23,12 +23,25 @@ data: ${JSON.stringify(data)}
|
|
|
23
23
|
|
|
24
24
|
`);
|
|
25
25
|
}
|
|
26
|
+
function normalizeRequest(req) {
|
|
27
|
+
const rawHeaders = req.headers ?? {};
|
|
28
|
+
return {
|
|
29
|
+
...req,
|
|
30
|
+
headers: {
|
|
31
|
+
get: (name) => {
|
|
32
|
+
const val = rawHeaders[name.toLowerCase()];
|
|
33
|
+
return Array.isArray(val) ? val[0] ?? null : val ?? null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
26
38
|
function createStrandHandler(config) {
|
|
27
39
|
const client = new OpenAI({ apiKey: config.apiKey });
|
|
28
40
|
const openAITools = (config.tools ?? []).map(toolToOpenAITool);
|
|
29
41
|
const maxSteps = config.maxSteps ?? 10;
|
|
30
42
|
const rateLimiter = config.rateLimit ? new RateLimiter(config.rateLimit) : null;
|
|
31
43
|
return async (req, res) => {
|
|
44
|
+
const normalizedReq = normalizeRequest(req);
|
|
32
45
|
const body = req.body;
|
|
33
46
|
if (rateLimiter) {
|
|
34
47
|
const ip = req.ip ?? "unknown";
|
|
@@ -48,7 +61,7 @@ function createStrandHandler(config) {
|
|
|
48
61
|
}
|
|
49
62
|
if (config.authorize) {
|
|
50
63
|
try {
|
|
51
|
-
await config.authorize(
|
|
64
|
+
await config.authorize(normalizedReq);
|
|
52
65
|
} catch (err) {
|
|
53
66
|
const message = err instanceof Error ? err.message : "Unauthorized";
|
|
54
67
|
res.status(401).json({ error: message });
|
|
@@ -61,7 +74,7 @@ function createStrandHandler(config) {
|
|
|
61
74
|
emit(res, "strand:start", { sessionId: generateId(), requestId: generateId() });
|
|
62
75
|
try {
|
|
63
76
|
const messages = body.messages;
|
|
64
|
-
const system = typeof config.system === "function" ? await config.system(
|
|
77
|
+
const system = typeof config.system === "function" ? await config.system(normalizedReq) : config.system ?? "";
|
|
65
78
|
const conversation = [
|
|
66
79
|
...system ? [{ role: "system", content: system }] : [],
|
|
67
80
|
...messages.map((m) => ({ role: m.role, content: m.content }))
|
|
@@ -126,7 +139,7 @@ function createStrandHandler(config) {
|
|
|
126
139
|
completedTools.map(async (block) => {
|
|
127
140
|
emit(res, "strand:tool-input-done", { toolCallId: block.id, input: block.input });
|
|
128
141
|
try {
|
|
129
|
-
const result = await config.onToolCall?.(block.name, block.input, { request:
|
|
142
|
+
const result = await config.onToolCall?.(block.name, block.input, { request: normalizedReq });
|
|
130
143
|
emit(res, "strand:tool-result", { toolCallId: block.id, result });
|
|
131
144
|
return { role: "tool", tool_call_id: block.id, content: JSON.stringify(result ?? null) };
|
|
132
145
|
} catch (err) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strand-js/openai",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "OpenAI provider adapter for Strand",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"openai": "^4.77.0",
|
|
21
|
-
"@strand-js/core": "0.1.
|
|
21
|
+
"@strand-js/core": "0.1.7"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/express": "^5.0.0",
|