@bedrockio/ai 0.4.2 → 0.5.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/CHANGELOG.md +8 -0
- package/dist/cjs/McpServer.js +211 -0
- package/dist/cjs/index.js +6 -0
- package/dist/cjs/openai.js +17 -1
- package/dist/esm/McpServer.js +208 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/openai.js +17 -1
- package/package.json +9 -3
- package/types/McpServer.d.ts +98 -0
- package/types/McpServer.d.ts.map +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/openai.d.ts.map +1 -1
- package/.claude/settings.local.json +0 -11
- package/.prettierignore +0 -1
- package/.prettierrc.cjs +0 -1
- package/__mocks__/@anthropic-ai/sdk.js +0 -37
- package/__mocks__/@google/generative-ai.js +0 -59
- package/__mocks__/openai.js +0 -53
- package/eslint.config.js +0 -2
- package/src/BaseClient.js +0 -288
- package/src/anthropic.js +0 -137
- package/src/google.js +0 -88
- package/src/index.js +0 -24
- package/src/openai.js +0 -134
- package/src/utils/code.js +0 -9
- package/src/utils/json.js +0 -58
- package/src/utils/templates.js +0 -87
- package/src/xai.js +0 -12
- package/tsconfig.cjs.json +0 -8
- package/tsconfig.esm.json +0 -8
- package/tsconfig.types.json +0 -9
- package/vitest.config.js +0 -10
package/src/openai.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import OpenAI from 'openai';
|
|
2
|
-
|
|
3
|
-
import BaseClient from './BaseClient.js';
|
|
4
|
-
|
|
5
|
-
export class OpenAiClient extends BaseClient {
|
|
6
|
-
static DEFAULT_MODEL = 'gpt-5-nano';
|
|
7
|
-
|
|
8
|
-
constructor(options) {
|
|
9
|
-
super(options);
|
|
10
|
-
this.client = new OpenAI(options);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Lists available models.
|
|
15
|
-
* {@link https://platform.openai.com/docs/models Documentation}
|
|
16
|
-
*/
|
|
17
|
-
async models() {
|
|
18
|
-
const { data } = await this.client.models.list();
|
|
19
|
-
return data.map((o) => o.id);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async runPrompt(options) {
|
|
23
|
-
const {
|
|
24
|
-
input,
|
|
25
|
-
model,
|
|
26
|
-
tools,
|
|
27
|
-
verbosity,
|
|
28
|
-
temperature,
|
|
29
|
-
instructions,
|
|
30
|
-
prevResponseId,
|
|
31
|
-
stream = false,
|
|
32
|
-
} = options;
|
|
33
|
-
|
|
34
|
-
const params = {
|
|
35
|
-
model,
|
|
36
|
-
input,
|
|
37
|
-
tools,
|
|
38
|
-
stream,
|
|
39
|
-
temperature,
|
|
40
|
-
instructions,
|
|
41
|
-
previous_response_id: prevResponseId,
|
|
42
|
-
|
|
43
|
-
text: {
|
|
44
|
-
format: this.getOutputFormat(options),
|
|
45
|
-
verbosity,
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
this.debug('Params:', params);
|
|
50
|
-
|
|
51
|
-
// @ts-ignore
|
|
52
|
-
return await this.client.responses.create(params);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async runStream(options) {
|
|
56
|
-
return await this.runPrompt({
|
|
57
|
-
...options,
|
|
58
|
-
stream: true,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
getTextResponse(response) {
|
|
63
|
-
return response.output_text;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
getStructuredResponse(response) {
|
|
67
|
-
return JSON.parse(response.output_text);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
getMessagesResponse(input, response) {
|
|
71
|
-
return {
|
|
72
|
-
messages: [
|
|
73
|
-
...input,
|
|
74
|
-
{
|
|
75
|
-
role: 'assistant',
|
|
76
|
-
content: response.output_text,
|
|
77
|
-
},
|
|
78
|
-
],
|
|
79
|
-
// Note that this ability currently only
|
|
80
|
-
// exists for OpenAI compatible providers.
|
|
81
|
-
prevResponseId: response.id,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Private
|
|
86
|
-
|
|
87
|
-
getOutputFormat(options) {
|
|
88
|
-
let { output, schema } = options;
|
|
89
|
-
if (output === 'json') {
|
|
90
|
-
return {
|
|
91
|
-
type: 'json_object',
|
|
92
|
-
};
|
|
93
|
-
} else if (schema) {
|
|
94
|
-
return {
|
|
95
|
-
type: 'json_schema',
|
|
96
|
-
// Name is required but arbitrary.
|
|
97
|
-
name: 'schema',
|
|
98
|
-
strict: true,
|
|
99
|
-
schema,
|
|
100
|
-
};
|
|
101
|
-
} else {
|
|
102
|
-
return {
|
|
103
|
-
type: 'text',
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
normalizeStreamEvent(event) {
|
|
109
|
-
const { type } = event;
|
|
110
|
-
|
|
111
|
-
if (type === 'response.created') {
|
|
112
|
-
return {
|
|
113
|
-
type: 'start',
|
|
114
|
-
id: event.response.id,
|
|
115
|
-
};
|
|
116
|
-
} else if (type === 'response.completed') {
|
|
117
|
-
return {
|
|
118
|
-
type: 'stop',
|
|
119
|
-
id: event.response.id,
|
|
120
|
-
usage: event.response.usage,
|
|
121
|
-
};
|
|
122
|
-
} else if (type === 'response.output_text.delta') {
|
|
123
|
-
return {
|
|
124
|
-
type: 'delta',
|
|
125
|
-
delta: event.delta,
|
|
126
|
-
};
|
|
127
|
-
} else if (type === 'response.output_text.done') {
|
|
128
|
-
return {
|
|
129
|
-
type: 'done',
|
|
130
|
-
text: event.text,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
package/src/utils/code.js
DELETED
package/src/utils/json.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { OBJ, STR, parse } from 'partial-json';
|
|
2
|
-
|
|
3
|
-
export function createMessageExtractor(keys) {
|
|
4
|
-
let buffer = '';
|
|
5
|
-
const extractors = keys.map((key) => {
|
|
6
|
-
return createExtractor(key);
|
|
7
|
-
});
|
|
8
|
-
return (delta) => {
|
|
9
|
-
buffer += delta;
|
|
10
|
-
return extractors
|
|
11
|
-
.map((extractor) => {
|
|
12
|
-
return extractor(buffer);
|
|
13
|
-
})
|
|
14
|
-
.filter((extracted) => {
|
|
15
|
-
return extracted;
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function createExtractor(key) {
|
|
21
|
-
let lastText = '';
|
|
22
|
-
let done = false;
|
|
23
|
-
return (buffer) => {
|
|
24
|
-
if (done) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const text = extractText(buffer, key);
|
|
29
|
-
|
|
30
|
-
if (!text) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Don't finish while the buffer has whitespace as it
|
|
35
|
-
// may be in the middle of trying to extract.
|
|
36
|
-
if (text === lastText && !buffer.endsWith(' ')) {
|
|
37
|
-
done = true;
|
|
38
|
-
}
|
|
39
|
-
const delta = text.slice(lastText.length);
|
|
40
|
-
|
|
41
|
-
lastText = text;
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
key,
|
|
45
|
-
text,
|
|
46
|
-
delta,
|
|
47
|
-
done,
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function extractText(input, key) {
|
|
53
|
-
if (!input) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
const parsed = parse(input, STR | OBJ);
|
|
57
|
-
return parsed?.[key] || '';
|
|
58
|
-
}
|
package/src/utils/templates.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import fs from 'fs/promises';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
import { glob } from 'glob';
|
|
5
|
-
import Mustache from 'mustache';
|
|
6
|
-
|
|
7
|
-
export async function loadTemplates(dir) {
|
|
8
|
-
const result = {};
|
|
9
|
-
const files = await glob(path.join(dir, '*.md'));
|
|
10
|
-
|
|
11
|
-
if (!files.length) {
|
|
12
|
-
throw new Error(`No templates found in: ${dir}.`);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
for (let file of files) {
|
|
16
|
-
const base = path.basename(file, '.md');
|
|
17
|
-
result[base] = await loadTemplate(file);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function renderTemplate(template, options) {
|
|
24
|
-
let params = {
|
|
25
|
-
...options,
|
|
26
|
-
...options.params,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
params = mapObjects(params);
|
|
30
|
-
params = wrapProxy(params);
|
|
31
|
-
return Mustache.render(template, params);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Utils
|
|
35
|
-
|
|
36
|
-
async function loadTemplate(file) {
|
|
37
|
-
return await fs.readFile(file, 'utf-8');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Transform arrays and object to versions
|
|
41
|
-
// that are more understandable in the context
|
|
42
|
-
// of a template that may have meaningful whitespace.
|
|
43
|
-
function mapObjects(params) {
|
|
44
|
-
const result = {};
|
|
45
|
-
for (let [key, value] of Object.entries(params)) {
|
|
46
|
-
if (Array.isArray(value)) {
|
|
47
|
-
value = mapArray(value);
|
|
48
|
-
} else if (typeof value === 'object') {
|
|
49
|
-
value = JSON.stringify(value, null, 2);
|
|
50
|
-
}
|
|
51
|
-
result[key] = value;
|
|
52
|
-
}
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function mapArray(arr) {
|
|
57
|
-
// Only map simple arrays of primitives.
|
|
58
|
-
if (typeof arr[0] === 'string') {
|
|
59
|
-
arr = arr
|
|
60
|
-
.map((el) => {
|
|
61
|
-
return `- ${el}`;
|
|
62
|
-
})
|
|
63
|
-
.join('\n');
|
|
64
|
-
}
|
|
65
|
-
return arr;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Wrap params with a proxy object that reports
|
|
69
|
-
// as having all properties. If one is accessed
|
|
70
|
-
// that does not exist then return the original
|
|
71
|
-
// token. This way templates can be partially
|
|
72
|
-
// interpolated and re-interpolated later.
|
|
73
|
-
function wrapProxy(params) {
|
|
74
|
-
return new Proxy(params, {
|
|
75
|
-
has() {
|
|
76
|
-
return true;
|
|
77
|
-
},
|
|
78
|
-
|
|
79
|
-
get(target, prop) {
|
|
80
|
-
if (prop in target) {
|
|
81
|
-
return target[prop];
|
|
82
|
-
} else {
|
|
83
|
-
return `{{{${prop.toString()}}}}`;
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
}
|
package/src/xai.js
DELETED
package/tsconfig.cjs.json
DELETED
package/tsconfig.esm.json
DELETED
package/tsconfig.types.json
DELETED