@modelprofile.com/flexharness-providers 9.1.0 → 9.2.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_ts_anthropic_messages/errors.d.ts +22 -0
- package/dist_ts_anthropic_messages/errors.js +100 -0
- package/dist_ts_anthropic_messages/index.d.ts +4 -0
- package/dist_ts_anthropic_messages/index.js +4 -0
- package/dist_ts_anthropic_messages/interfaces.d.ts +185 -0
- package/dist_ts_anthropic_messages/interfaces.js +2 -0
- package/dist_ts_anthropic_messages/openai.d.ts +19 -0
- package/dist_ts_anthropic_messages/openai.js +48 -0
- package/dist_ts_anthropic_messages/plugins.d.ts +4 -0
- package/dist_ts_anthropic_messages/plugins.js +4 -0
- package/dist_ts_anthropic_messages/request.d.ts +109 -0
- package/dist_ts_anthropic_messages/request.js +554 -0
- package/dist_ts_anthropic_messages/service.d.ts +8 -0
- package/dist_ts_anthropic_messages/service.js +274 -0
- package/dist_ts_anthropic_messages/signature.d.ts +16 -0
- package/dist_ts_anthropic_messages/signature.js +91 -0
- package/dist_ts_anthropic_messages/stream.d.ts +48 -0
- package/dist_ts_anthropic_messages/stream.js +220 -0
- package/dist_ts_anthropic_messages_node/index.d.ts +8 -0
- package/dist_ts_anthropic_messages_node/index.js +93 -0
- package/dist_ts_anthropic_messages_node/plugins.d.ts +6 -0
- package/dist_ts_anthropic_messages_node/plugins.js +4 -0
- package/dist_ts_providers/anthropic/index.d.ts +1 -1
- package/dist_ts_providers/anthropic/index.js +1 -1
- package/dist_ts_providers/anthropic/plugins.d.ts +1 -1
- package/dist_ts_providers/google/index.d.ts +1 -1
- package/dist_ts_providers/google/index.js +1 -1
- package/dist_ts_providers/google/plugins.d.ts +1 -1
- package/dist_ts_providers/groq/index.d.ts +1 -1
- package/dist_ts_providers/groq/index.js +1 -1
- package/dist_ts_providers/groq/plugins.d.ts +1 -1
- package/dist_ts_providers/mistral/index.d.ts +1 -1
- package/dist_ts_providers/mistral/index.js +1 -1
- package/dist_ts_providers/mistral/plugins.d.ts +1 -1
- package/dist_ts_providers/openai/index.d.ts +1 -1
- package/dist_ts_providers/openai/index.js +1 -1
- package/dist_ts_providers/openai/plugins.d.ts +1 -1
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +2 -0
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +8 -1
- package/dist_ts_providers/openai/smartai.chatgpt.middleware.d.ts +5 -1
- package/dist_ts_providers/openai/smartai.chatgpt.middleware.js +25 -12
- package/dist_ts_providers/perplexity/index.d.ts +1 -1
- package/dist_ts_providers/perplexity/index.js +1 -1
- package/dist_ts_providers/perplexity/plugins.d.ts +1 -1
- package/dist_ts_providers/xai/index.d.ts +1 -1
- package/dist_ts_providers/xai/index.js +1 -1
- package/dist_ts_providers/xai/plugins.d.ts +1 -1
- package/package.json +14 -2
- package/readme.md +1 -0
- package/ts_anthropic_messages/errors.ts +124 -0
- package/ts_anthropic_messages/index.ts +24 -0
- package/ts_anthropic_messages/interfaces.ts +173 -0
- package/ts_anthropic_messages/openai.ts +58 -0
- package/ts_anthropic_messages/plugins.ts +26 -0
- package/ts_anthropic_messages/readme.md +129 -0
- package/ts_anthropic_messages/request.ts +652 -0
- package/ts_anthropic_messages/service.ts +312 -0
- package/ts_anthropic_messages/signature.ts +107 -0
- package/ts_anthropic_messages/stream.ts +260 -0
- package/ts_anthropic_messages_node/index.ts +99 -0
- package/ts_anthropic_messages_node/plugins.ts +7 -0
- package/ts_anthropic_messages_node/readme.md +50 -0
- package/ts_providers/anthropic/index.ts +1 -1
- package/ts_providers/anthropic/plugins.ts +1 -0
- package/ts_providers/google/index.ts +1 -1
- package/ts_providers/google/plugins.ts +1 -1
- package/ts_providers/groq/index.ts +1 -1
- package/ts_providers/groq/plugins.ts +1 -1
- package/ts_providers/mistral/index.ts +1 -1
- package/ts_providers/mistral/plugins.ts +1 -1
- package/ts_providers/openai/index.ts +1 -1
- package/ts_providers/openai/plugins.ts +1 -0
- package/ts_providers/openai/readme.md +6 -4
- package/ts_providers/openai/smartai.chatgpt.catalog.ts +11 -0
- package/ts_providers/openai/smartai.chatgpt.middleware.ts +27 -11
- package/ts_providers/perplexity/index.ts +1 -1
- package/ts_providers/perplexity/plugins.ts +1 -1
- package/ts_providers/readme.md +1 -0
- package/ts_providers/xai/index.ts +1 -1
- package/ts_providers/xai/plugins.ts +1 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
|
|
3
|
+
const bodylessMethods: ReadonlySet<string> = new Set(['GET', 'HEAD']);
|
|
4
|
+
|
|
5
|
+
const toRequest = (incomingArg: plugins.http.IncomingMessage, signalArg: AbortSignal): Request => {
|
|
6
|
+
const headers = new Headers();
|
|
7
|
+
for (let index = 0; index + 1 < incomingArg.rawHeaders.length; index += 2) {
|
|
8
|
+
headers.append(incomingArg.rawHeaders[index], incomingArg.rawHeaders[index + 1]);
|
|
9
|
+
}
|
|
10
|
+
const method = incomingArg.method ?? 'GET';
|
|
11
|
+
// Only the path and query are taken from the request line; the Host header stays a header, for the
|
|
12
|
+
// service's `authenticate` to judge.
|
|
13
|
+
const url = new URL(incomingArg.url ?? '/', 'http://localhost');
|
|
14
|
+
return new Request(url, {
|
|
15
|
+
method,
|
|
16
|
+
headers,
|
|
17
|
+
signal: signalArg,
|
|
18
|
+
...(bodylessMethods.has(method)
|
|
19
|
+
? {}
|
|
20
|
+
// `Readable.toWeb` is typed with Node's own stream/web types, which the global `BodyInit` does not name.
|
|
21
|
+
: { body: plugins.Readable.toWeb(incomingArg) as ReadableStream<Uint8Array>, duplex: 'half' as const }),
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const writeResponse = async (
|
|
26
|
+
responseArg: Response,
|
|
27
|
+
outgoingArg: plugins.http.ServerResponse,
|
|
28
|
+
signalArg: AbortSignal,
|
|
29
|
+
): Promise<void> => {
|
|
30
|
+
if (signalArg.aborted) {
|
|
31
|
+
// The client left while the answer was prepared; cancelling the body releases what it holds, such as the
|
|
32
|
+
// model call and the ping timer of a stream.
|
|
33
|
+
await responseArg.body?.cancel(signalArg.reason);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
outgoingArg.writeHead(responseArg.status, Object.fromEntries(responseArg.headers));
|
|
37
|
+
outgoingArg.flushHeaders();
|
|
38
|
+
if (!responseArg.body) {
|
|
39
|
+
outgoingArg.end();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const reader = responseArg.body.getReader();
|
|
43
|
+
const onAbort = (): void => {
|
|
44
|
+
void reader.cancel(signalArg.reason).catch(() => undefined);
|
|
45
|
+
};
|
|
46
|
+
signalArg.addEventListener('abort', onAbort, { once: true });
|
|
47
|
+
let finished = false;
|
|
48
|
+
try {
|
|
49
|
+
while (!signalArg.aborted) {
|
|
50
|
+
const { done, value } = await reader.read();
|
|
51
|
+
if (done) {
|
|
52
|
+
finished = true;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
// A socket that closes while full aborts the signal, which ends this wait.
|
|
56
|
+
if (!outgoingArg.write(value)) await plugins.once(outgoingArg, 'drain', { signal: signalArg });
|
|
57
|
+
}
|
|
58
|
+
} finally {
|
|
59
|
+
signalArg.removeEventListener('abort', onAbort);
|
|
60
|
+
// A write that failed leaves the body unread; cancelling it releases the model call and the ping timer.
|
|
61
|
+
if (!finished) await reader.cancel(signalArg.reason).catch(() => undefined);
|
|
62
|
+
reader.releaseLock();
|
|
63
|
+
}
|
|
64
|
+
if (!signalArg.aborted) outgoingArg.end();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Hosts an Anthropic Messages service on a Node HTTP server: `http.createServer(listener)`.
|
|
69
|
+
*
|
|
70
|
+
* A streamed answer is written as it is produced and waits for the socket when it is full. A client that
|
|
71
|
+
* disconnects aborts its request, which stops the model call behind it.
|
|
72
|
+
*/
|
|
73
|
+
export const createAnthropicMessagesNodeListener = (
|
|
74
|
+
serviceArg: plugins.IAnthropicMessagesService,
|
|
75
|
+
): ((incomingArg: plugins.http.IncomingMessage, outgoingArg: plugins.http.ServerResponse) => void) => (
|
|
76
|
+
incomingArg,
|
|
77
|
+
outgoingArg,
|
|
78
|
+
) => {
|
|
79
|
+
const abort = new AbortController();
|
|
80
|
+
outgoingArg.once('close', () => {
|
|
81
|
+
if (!outgoingArg.writableFinished) abort.abort(new Error('The client closed the connection.'));
|
|
82
|
+
});
|
|
83
|
+
void (async () => {
|
|
84
|
+
const response = await serviceArg.handle(toRequest(incomingArg, abort.signal));
|
|
85
|
+
await writeResponse(response, outgoingArg, abort.signal);
|
|
86
|
+
})().catch((errorArg: unknown) => {
|
|
87
|
+
// A client that left has no one to tell; its abort already ended the answer.
|
|
88
|
+
if (abort.signal.aborted) return;
|
|
89
|
+
if (!outgoingArg.headersSent) {
|
|
90
|
+
outgoingArg.writeHead(500, { 'content-type': 'application/json' });
|
|
91
|
+
outgoingArg.end(JSON.stringify({
|
|
92
|
+
type: 'error',
|
|
93
|
+
error: { type: 'api_error', message: `The request could not be served: ${errorArg instanceof Error ? errorArg.message : String(errorArg)}` },
|
|
94
|
+
}));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
outgoingArg.destroy(errorArg instanceof Error ? errorArg : new Error(String(errorArg)));
|
|
98
|
+
});
|
|
99
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type * as http from 'node:http';
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
|
|
5
|
+
export { once, Readable };
|
|
6
|
+
export type { http };
|
|
7
|
+
export type { IAnthropicMessagesService } from '@modelprofile.com/flexharness-providers/anthropic-messages';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Hosts an Anthropic Messages service on a Node HTTP server.
|
|
2
|
+
|
|
3
|
+
Part of the [FlexHarness toolbox](https://code.foss.global/modelprofile.com/flexharness).
|
|
4
|
+
Install the components your project uses. Provider adapters are registered explicitly;
|
|
5
|
+
the agent and harness do not install or register vendor SDKs.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @modelprofile.com/flexharness-providers
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
See the [toolbox usage and migration guide](https://code.foss.global/modelprofile.com/flexharness#readme)
|
|
12
|
+
for composition examples and the SmartAI/SmartAgent migration map.
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import * as http from 'node:http';
|
|
16
|
+
import { createAnthropicMessagesNodeListener } from '@modelprofile.com/flexharness-providers/anthropic-messages/node';
|
|
17
|
+
|
|
18
|
+
const server = http.createServer(createAnthropicMessagesNodeListener(service));
|
|
19
|
+
server.listen(0, '127.0.0.1');
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
A streamed answer is written as the model produces it and waits for the socket when it is full. A client that
|
|
23
|
+
disconnects aborts its request, which stops the model call behind it; one that leaves before the answer
|
|
24
|
+
begins gets no model call, or has the answer that was already prepared cancelled with its model call. The request line gives the path and
|
|
25
|
+
query only; the `Host` header stays a header for the service's `authenticate` to judge.
|
|
26
|
+
|
|
27
|
+
## Issue Reporting and Security
|
|
28
|
+
|
|
29
|
+
Report issues and security concerns through [community.foss.global](https://community.foss.global).
|
|
30
|
+
|
|
31
|
+
## License and Legal Information
|
|
32
|
+
|
|
33
|
+
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
|
|
34
|
+
|
|
35
|
+
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
|
36
|
+
|
|
37
|
+
### Trademarks
|
|
38
|
+
|
|
39
|
+
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
|
40
|
+
|
|
41
|
+
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
|
42
|
+
|
|
43
|
+
### Company Information
|
|
44
|
+
|
|
45
|
+
Task Venture Capital GmbH<br>
|
|
46
|
+
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
47
|
+
|
|
48
|
+
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
|
49
|
+
|
|
50
|
+
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
|
@@ -7,7 +7,7 @@ export interface IAnthropicModelOptions extends plugins.ISmartAiOptions {
|
|
|
7
7
|
promptCaching?: boolean | plugins.ISmartAiCacheOptions;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export const createAnthropicModelProvider = (): plugins.IModelProvider<IAnthropicModelOptions> => ({
|
|
10
|
+
export const createAnthropicModelProvider = (): plugins.IModelProvider<IAnthropicModelOptions, plugins.LanguageModelV4> => ({
|
|
11
11
|
id: 'anthropic',
|
|
12
12
|
getModel: (options) => {
|
|
13
13
|
const model = plugins.createAnthropic({ apiKey: options.apiKey })(options.model);
|
|
@@ -2,7 +2,7 @@ import * as plugins from './plugins.js';
|
|
|
2
2
|
|
|
3
3
|
export interface IGoogleModelOptions extends plugins.ISmartAiOptions { provider: 'google'; }
|
|
4
4
|
|
|
5
|
-
export const createGoogleModelProvider = (): plugins.IModelProvider<IGoogleModelOptions> => ({
|
|
5
|
+
export const createGoogleModelProvider = (): plugins.IModelProvider<IGoogleModelOptions, plugins.LanguageModelV4> => ({
|
|
6
6
|
id: 'google',
|
|
7
7
|
getModel: (options) => plugins.createGoogle({ apiKey: options.apiKey })(options.model),
|
|
8
8
|
});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { createGoogle } from '@ai-sdk/google';
|
|
2
2
|
export { createGoogle };
|
|
3
|
-
export type { IModelProvider, ISmartAiOptions } from '@modelprofile.com/flexharness-models';
|
|
3
|
+
export type { IModelProvider, ISmartAiOptions, LanguageModelV4 } from '@modelprofile.com/flexharness-models';
|
|
@@ -2,7 +2,7 @@ import * as plugins from './plugins.js';
|
|
|
2
2
|
|
|
3
3
|
export interface IGroqModelOptions extends plugins.ISmartAiOptions { provider: 'groq'; }
|
|
4
4
|
|
|
5
|
-
export const createGroqModelProvider = (): plugins.IModelProvider<IGroqModelOptions> => ({
|
|
5
|
+
export const createGroqModelProvider = (): plugins.IModelProvider<IGroqModelOptions, plugins.LanguageModelV4> => ({
|
|
6
6
|
id: 'groq',
|
|
7
7
|
getModel: (options) => plugins.createGroq({ apiKey: options.apiKey })(options.model),
|
|
8
8
|
});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { createGroq } from '@ai-sdk/groq';
|
|
2
2
|
export { createGroq };
|
|
3
|
-
export type { IModelProvider, ISmartAiOptions } from '@modelprofile.com/flexharness-models';
|
|
3
|
+
export type { IModelProvider, ISmartAiOptions, LanguageModelV4 } from '@modelprofile.com/flexharness-models';
|
|
@@ -2,7 +2,7 @@ import * as plugins from './plugins.js';
|
|
|
2
2
|
|
|
3
3
|
export interface IMistralModelOptions extends plugins.ISmartAiOptions { provider: 'mistral'; }
|
|
4
4
|
|
|
5
|
-
export const createMistralModelProvider = (): plugins.IModelProvider<IMistralModelOptions> => ({
|
|
5
|
+
export const createMistralModelProvider = (): plugins.IModelProvider<IMistralModelOptions, plugins.LanguageModelV4> => ({
|
|
6
6
|
id: 'mistral',
|
|
7
7
|
getModel: (options) => plugins.createMistral({ apiKey: options.apiKey })(options.model),
|
|
8
8
|
});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { createMistral } from '@ai-sdk/mistral';
|
|
2
2
|
export { createMistral };
|
|
3
|
-
export type { IModelProvider, ISmartAiOptions } from '@modelprofile.com/flexharness-models';
|
|
3
|
+
export type { IModelProvider, ISmartAiOptions, LanguageModelV4 } from '@modelprofile.com/flexharness-models';
|
|
@@ -62,7 +62,7 @@ export interface IOpenAiModelOptions extends plugins.ISmartAiOptions {
|
|
|
62
62
|
api?: TOpenAiApi;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export const createOpenAiModelProvider = (): plugins.IModelProvider<IOpenAiModelOptions> => ({
|
|
65
|
+
export const createOpenAiModelProvider = (): plugins.IModelProvider<IOpenAiModelOptions, plugins.LanguageModelV4> => ({
|
|
66
66
|
id: 'openai',
|
|
67
67
|
getModel: (options) => {
|
|
68
68
|
const api = options.api ?? 'responses';
|
|
@@ -93,11 +93,13 @@ included, in the catalog's priority order, with the first listed model marked `i
|
|
|
93
93
|
rejected token fails with an `OpenAiChatGptAuthError` whose `status` is 401, so a caller resolves
|
|
94
94
|
again with `rejectedGeneration` and repeats the read; an abort rejects with the signal's reason;
|
|
95
95
|
anything else is an `OpenAiChatGptModelListError` whose `code` is `NETWORK_FAILED`, `HTTP_ERROR`
|
|
96
|
-
(with `status`) or `PROTOCOL_ERROR`.
|
|
96
|
+
(with `status`) or `PROTOCOL_ERROR`. A model whose catalog entry states its context window carries it as
|
|
97
|
+
`contextWindow`, in tokens.
|
|
97
98
|
|
|
98
|
-
Both ChatGPT connections classify Codex limits with `classifyOpenAiChatGptLimit` and move system
|
|
99
|
-
|
|
100
|
-
|
|
99
|
+
Both ChatGPT connections classify Codex limits with `classifyOpenAiChatGptLimit` and move the system
|
|
100
|
+
messages a prompt opens with into the Responses `instructions` with
|
|
101
|
+
`createOpenAiChatGptInstructionsMiddleware()`; a system message later in the conversation stays in its
|
|
102
|
+
place as a `developer` input item. Both are exported here for connections of their own.
|
|
101
103
|
|
|
102
104
|
The `/auth` subpath keeps its connections, which derive identity from the token itself, and
|
|
103
105
|
re-exports the ChatGPT limit classifier, the instructions middleware, the Codex base URL, the
|
|
@@ -40,6 +40,8 @@ export interface IOpenAiChatGptModel {
|
|
|
40
40
|
supportsPersonality: boolean;
|
|
41
41
|
serviceTiers: IOpenAiChatGptModelServiceTier[];
|
|
42
42
|
defaultServiceTier?: string;
|
|
43
|
+
/** The context window in tokens, as the catalog states it; absent when the catalog states none. */
|
|
44
|
+
contextWindow?: number;
|
|
43
45
|
/** The first listed model, or the first model when none is listed. */
|
|
44
46
|
isDefault: boolean;
|
|
45
47
|
}
|
|
@@ -193,6 +195,14 @@ const projectCatalog = (valueArg: unknown): IOpenAiChatGptModel[] => {
|
|
|
193
195
|
description: boundedString(rawTier.description, 1024, true),
|
|
194
196
|
};
|
|
195
197
|
});
|
|
198
|
+
const contextWindow = rawModel.context_window;
|
|
199
|
+
if (
|
|
200
|
+
contextWindow !== null
|
|
201
|
+
&& contextWindow !== undefined
|
|
202
|
+
&& (!Number.isSafeInteger(contextWindow) || (contextWindow as number) < 1)
|
|
203
|
+
) {
|
|
204
|
+
throw protocolError();
|
|
205
|
+
}
|
|
196
206
|
const modelMessages = isRecord(rawModel.model_messages) ? rawModel.model_messages : undefined;
|
|
197
207
|
const variables = isRecord(modelMessages?.instructions_variables)
|
|
198
208
|
? modelMessages.instructions_variables
|
|
@@ -225,6 +235,7 @@ const projectCatalog = (valueArg: unknown): IOpenAiChatGptModel[] => {
|
|
|
225
235
|
...(rawModel.default_service_tier === null || rawModel.default_service_tier === undefined
|
|
226
236
|
? {}
|
|
227
237
|
: { defaultServiceTier: boundedString(rawModel.default_service_tier, 128) }),
|
|
238
|
+
...(contextWindow === null || contextWindow === undefined ? {} : { contextWindow: contextWindow as number }),
|
|
228
239
|
isDefault: false,
|
|
229
240
|
},
|
|
230
241
|
};
|
|
@@ -2,9 +2,16 @@ import type * as plugins from './plugins.js';
|
|
|
2
2
|
|
|
3
3
|
const isNonEmptyString = (value: unknown): value is string => typeof value === 'string' && value.trim().length > 0;
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
type TPrompt = plugins.LanguageModelV4CallOptions['prompt'];
|
|
6
|
+
|
|
7
|
+
/** The number of system messages the prompt opens with, before its first other message. */
|
|
8
|
+
const countLeadingSystemMessages = (prompt: TPrompt): number => {
|
|
9
|
+
const firstOther = prompt.findIndex((message) => message.role !== 'system');
|
|
10
|
+
return firstOther === -1 ? prompt.length : firstOther;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const joinSystemInstructions = (messages: TPrompt): string | undefined => {
|
|
14
|
+
const instructions = messages
|
|
8
15
|
.map((message) => message.content)
|
|
9
16
|
.filter(isNonEmptyString);
|
|
10
17
|
|
|
@@ -17,34 +24,43 @@ const hasUnsupportedChatGptCacheRetention = (options: plugins.JSONObject): boole
|
|
|
17
24
|
|
|
18
25
|
/**
|
|
19
26
|
* ChatGPT's Codex backend requires top-level Responses API instructions.
|
|
20
|
-
*
|
|
27
|
+
*
|
|
28
|
+
* The system messages a prompt opens with become those instructions. A system message later in the
|
|
29
|
+
* conversation keeps its place and is sent as a `developer` input item, the role the Codex backend reads
|
|
30
|
+
* mid-conversation instructions in, so moving it would neither reorder the conversation nor change the
|
|
31
|
+
* instructions prefix a prompt cache keys on. A caller's own `instructions` or `systemMessageMode` is kept.
|
|
21
32
|
*/
|
|
22
33
|
export function createOpenAiChatGptInstructionsMiddleware(): plugins.LanguageModelV4Middleware {
|
|
23
34
|
return {
|
|
24
35
|
specificationVersion: 'v4',
|
|
25
36
|
transformParams: async ({ params }) => {
|
|
26
|
-
const instructions = getSystemInstructions(params.prompt);
|
|
27
37
|
const providerOptions = params.providerOptions ?? {};
|
|
28
38
|
const openAiProviderOptions = providerOptions.openai ?? {};
|
|
29
|
-
const
|
|
39
|
+
const leadingCount = countLeadingSystemMessages(params.prompt);
|
|
40
|
+
const shouldHoist = leadingCount > 0 && !isNonEmptyString(openAiProviderOptions.instructions);
|
|
41
|
+
const instructions = shouldHoist ? joinSystemInstructions(params.prompt.slice(0, leadingCount)) : undefined;
|
|
42
|
+
const prompt = shouldHoist ? params.prompt.slice(leadingCount) : params.prompt;
|
|
43
|
+
const keepsSystemMessages = prompt.some((message) => message.role === 'system');
|
|
44
|
+
const shouldSetDeveloperMode = keepsSystemMessages && openAiProviderOptions.systemMessageMode === undefined;
|
|
30
45
|
const shouldStripCacheRetention = hasUnsupportedChatGptCacheRetention(openAiProviderOptions);
|
|
31
46
|
|
|
32
|
-
if (!
|
|
47
|
+
if (!shouldHoist && !shouldSetDeveloperMode && !shouldStripCacheRetention) {
|
|
33
48
|
return params;
|
|
34
49
|
}
|
|
35
50
|
|
|
36
51
|
const nextOpenAiProviderOptions: plugins.JSONObject = { ...openAiProviderOptions };
|
|
37
52
|
delete nextOpenAiProviderOptions.promptCacheRetention;
|
|
38
53
|
delete nextOpenAiProviderOptions.prompt_cache_retention;
|
|
39
|
-
if (
|
|
54
|
+
if (instructions) {
|
|
40
55
|
nextOpenAiProviderOptions.instructions = instructions;
|
|
41
56
|
}
|
|
57
|
+
if (shouldSetDeveloperMode) {
|
|
58
|
+
nextOpenAiProviderOptions.systemMessageMode = 'developer';
|
|
59
|
+
}
|
|
42
60
|
|
|
43
61
|
return {
|
|
44
62
|
...params,
|
|
45
|
-
prompt
|
|
46
|
-
? params.prompt.filter((message) => message.role !== 'system')
|
|
47
|
-
: params.prompt,
|
|
63
|
+
prompt,
|
|
48
64
|
providerOptions: {
|
|
49
65
|
...providerOptions,
|
|
50
66
|
openai: nextOpenAiProviderOptions,
|
|
@@ -2,7 +2,7 @@ import * as plugins from './plugins.js';
|
|
|
2
2
|
|
|
3
3
|
export interface IPerplexityModelOptions extends plugins.ISmartAiOptions { provider: 'perplexity'; }
|
|
4
4
|
|
|
5
|
-
export const createPerplexityModelProvider = (): plugins.IModelProvider<IPerplexityModelOptions> => ({
|
|
5
|
+
export const createPerplexityModelProvider = (): plugins.IModelProvider<IPerplexityModelOptions, plugins.LanguageModelV4> => ({
|
|
6
6
|
id: 'perplexity',
|
|
7
7
|
getModel: (options) => plugins.createPerplexity({ apiKey: options.apiKey })(options.model),
|
|
8
8
|
});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { createPerplexity } from '@ai-sdk/perplexity';
|
|
2
2
|
export { createPerplexity };
|
|
3
|
-
export type { IModelProvider, ISmartAiOptions } from '@modelprofile.com/flexharness-models';
|
|
3
|
+
export type { IModelProvider, ISmartAiOptions, LanguageModelV4 } from '@modelprofile.com/flexharness-models';
|
package/ts_providers/readme.md
CHANGED
|
@@ -31,6 +31,7 @@ factories; use a provider subpath to import just that implementation.
|
|
|
31
31
|
| `/auth/files` | Explicit interoperability with external credential files |
|
|
32
32
|
| `/media` | OpenAI audio and image generation |
|
|
33
33
|
| `/research` | Anthropic research utilities |
|
|
34
|
+
| `/anthropic-messages`, `/anthropic-messages/node` | Serves the Anthropic Messages API over FlexHarness models, for Anthropic clients such as Claude Code |
|
|
34
35
|
|
|
35
36
|
Provider SDKs are installed together. Authentication, file access and vendor media
|
|
36
37
|
are separate entrypoints and are not imported by the provider factory root. For
|
|
@@ -2,7 +2,7 @@ import * as plugins from './plugins.js';
|
|
|
2
2
|
|
|
3
3
|
export interface IXaiModelOptions extends plugins.ISmartAiOptions { provider: 'xai'; }
|
|
4
4
|
|
|
5
|
-
export const createXaiModelProvider = (): plugins.IModelProvider<IXaiModelOptions> => ({
|
|
5
|
+
export const createXaiModelProvider = (): plugins.IModelProvider<IXaiModelOptions, plugins.LanguageModelV4> => ({
|
|
6
6
|
id: 'xai',
|
|
7
7
|
getModel: (options) => plugins.createXai({ apiKey: options.apiKey })(options.model),
|
|
8
8
|
});
|