@mappies/miniwat-provider-mock 0.0.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/LICENSE +131 -0
- package/README.md +121 -0
- package/dist/index.cjs +160 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +40 -0
- package/dist/index.d.mts +40 -0
- package/dist/index.mjs +159 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# PolyForm Noncommercial License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree
|
|
8
|
+
to them as both strict obligations and conditions to all
|
|
9
|
+
your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the
|
|
14
|
+
software to do everything you might do with the software
|
|
15
|
+
that would otherwise infringe the licensor's copyright
|
|
16
|
+
in it for any permitted purpose. However, you may
|
|
17
|
+
only distribute the software according to [Distribution
|
|
18
|
+
License](#distribution-license) and make changes or new works
|
|
19
|
+
based on the software according to [Changes and New Works
|
|
20
|
+
License](#changes-and-new-works-license).
|
|
21
|
+
|
|
22
|
+
## Distribution License
|
|
23
|
+
|
|
24
|
+
The licensor grants you an additional copyright license
|
|
25
|
+
to distribute copies of the software. Your license
|
|
26
|
+
to distribute covers distributing the software with
|
|
27
|
+
changes and new works permitted by [Changes and New Works
|
|
28
|
+
License](#changes-and-new-works-license).
|
|
29
|
+
|
|
30
|
+
## Notices
|
|
31
|
+
|
|
32
|
+
You must ensure that anyone who gets a copy of any part of
|
|
33
|
+
the software from you also gets a copy of these terms or the
|
|
34
|
+
URL for them above, as well as copies of any plain-text lines
|
|
35
|
+
beginning with `Required Notice:` that the licensor provided
|
|
36
|
+
with the software. For example:
|
|
37
|
+
|
|
38
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
39
|
+
|
|
40
|
+
## Changes and New Works License
|
|
41
|
+
|
|
42
|
+
The licensor grants you an additional copyright license to
|
|
43
|
+
make changes and new works based on the software for any
|
|
44
|
+
permitted purpose.
|
|
45
|
+
|
|
46
|
+
## Patent License
|
|
47
|
+
|
|
48
|
+
The licensor grants you a patent license for the software that
|
|
49
|
+
covers patent claims the licensor can license, or becomes able
|
|
50
|
+
to license, that you would infringe by using the software.
|
|
51
|
+
|
|
52
|
+
## Noncommercial Purposes
|
|
53
|
+
|
|
54
|
+
Any noncommercial purpose is a permitted purpose.
|
|
55
|
+
|
|
56
|
+
## Personal Uses
|
|
57
|
+
|
|
58
|
+
Personal use for research, experiment, and testing for
|
|
59
|
+
the benefit of public knowledge, personal study, private
|
|
60
|
+
entertainment, hobby projects, amateur pursuits, or religious
|
|
61
|
+
observance, without any anticipated commercial application,
|
|
62
|
+
is use for a permitted purpose.
|
|
63
|
+
|
|
64
|
+
## Noncommercial Organizations
|
|
65
|
+
|
|
66
|
+
Use by any charitable organization, educational institution,
|
|
67
|
+
public research organization, public safety or health
|
|
68
|
+
organization, environmental protection organization,
|
|
69
|
+
or government institution is use for a permitted purpose
|
|
70
|
+
regardless of the source of funding or obligations resulting
|
|
71
|
+
from the funding.
|
|
72
|
+
|
|
73
|
+
## Fair Use
|
|
74
|
+
|
|
75
|
+
You may have "fair use" rights for the software under the
|
|
76
|
+
law. These terms do not limit them.
|
|
77
|
+
|
|
78
|
+
## No Other Rights
|
|
79
|
+
|
|
80
|
+
These terms do not allow you to sublicense or transfer any of
|
|
81
|
+
your licenses to anyone else, or prevent the licensor from
|
|
82
|
+
granting licenses to anyone else. These terms do not imply
|
|
83
|
+
any other licenses.
|
|
84
|
+
|
|
85
|
+
## Patent Defense
|
|
86
|
+
|
|
87
|
+
If you make any written claim that the software infringes or
|
|
88
|
+
contributes to infringement of any patent, your patent license
|
|
89
|
+
for the software granted under these terms ends immediately. If
|
|
90
|
+
your company makes such a claim, your patent license ends
|
|
91
|
+
immediately for work on behalf of your company.
|
|
92
|
+
|
|
93
|
+
## Violations
|
|
94
|
+
|
|
95
|
+
The first time you are notified in writing that you have
|
|
96
|
+
violated any of these terms, or done anything with the software
|
|
97
|
+
not covered by your licenses, your licenses can nonetheless
|
|
98
|
+
continue if you come into full compliance with these terms,
|
|
99
|
+
and take practical steps to correct past violations, within
|
|
100
|
+
32 days of receiving notice. Otherwise, all your licenses
|
|
101
|
+
end immediately.
|
|
102
|
+
|
|
103
|
+
## No Liability
|
|
104
|
+
|
|
105
|
+
***As far as the law allows, the software comes as is, without
|
|
106
|
+
any warranty or condition, and the licensor will not be liable
|
|
107
|
+
to you for any damages arising out of these terms or the use
|
|
108
|
+
or nature of the software, under any kind of legal claim.***
|
|
109
|
+
|
|
110
|
+
## Definitions
|
|
111
|
+
|
|
112
|
+
The **licensor** is the individual or entity offering these
|
|
113
|
+
terms, and the **software** is the software the licensor makes
|
|
114
|
+
available under these terms.
|
|
115
|
+
|
|
116
|
+
**You** refers to the individual or entity agreeing to these
|
|
117
|
+
terms.
|
|
118
|
+
|
|
119
|
+
**Your company** is any legal entity, sole proprietorship,
|
|
120
|
+
or other kind of organization that you work for, plus all
|
|
121
|
+
organizations that have control over, are under the control of,
|
|
122
|
+
or are under common control with that organization. **Control**
|
|
123
|
+
means ownership of substantially all the assets of an entity,
|
|
124
|
+
or the power to direct its management and policies by vote,
|
|
125
|
+
contract, or otherwise. Control can be direct or indirect.
|
|
126
|
+
|
|
127
|
+
**Your licenses** are all the licenses granted to you for the
|
|
128
|
+
software under these terms.
|
|
129
|
+
|
|
130
|
+
**Use** means anything you do with the software requiring one
|
|
131
|
+
of your licenses.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# @mappies/miniwat-provider-mock
|
|
2
|
+
|
|
3
|
+
A **deterministic** mock provider for the miniwat platform. It implements the same `Model` interface as the real providers (`@mappies/miniwat-provider-bedrock`, `@mappies/miniwat-provider-openai`) but produces its response purely from the input — no network calls, no model SDK, no tool dispatch.
|
|
4
|
+
|
|
5
|
+
Given identical inputs, both `invoke()` and `stream()` produce identical response content, which makes the provider ideal for writing predictable, repeatable tests against agent code without calling a real LLM.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Drop-in `Model` implementation — substitute it anywhere a `Model` is expected.
|
|
10
|
+
- Deterministic `message.content`: a stable JSON serialization of `{ modelId, tools }`.
|
|
11
|
+
- Consistent streaming: `stream()` emits the same content as `invoke()`, split into sequential chunks.
|
|
12
|
+
- No network, no model SDK, no `ToolDispatcher`.
|
|
13
|
+
- Input-driven simulated errors for exercising error-handling paths.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
npm install @mappies/miniwat-provider-mock
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Peer packages `@mappies/miniwat-agent-core` and `@mappies/miniwat-mcp-core` are declared as dependencies.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### `invoke`
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { MockModel } from "@mappies/miniwat-provider-mock";
|
|
29
|
+
import { Message, MessageType } from "@mappies/miniwat-agent-core";
|
|
30
|
+
|
|
31
|
+
const model = new MockModel({ modelId: "mock-model" });
|
|
32
|
+
|
|
33
|
+
const response = await model.invoke([
|
|
34
|
+
new Message({ type: MessageType.User, content: "hello", createdTimestamp: new Date().toISOString() }),
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
console.log(response.message.content);
|
|
38
|
+
// {
|
|
39
|
+
// "modelId": "mock-model",
|
|
40
|
+
// "tools": {}
|
|
41
|
+
// }
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The resolved `ResponseMessage` has `message` (an agent `Message`), `stopReason` (`StopReason.END_TURN`), and a deterministic `tokenUsage`.
|
|
45
|
+
|
|
46
|
+
### `stream`
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const stream = await model.stream(messages);
|
|
50
|
+
stream.on("data", (chunk) => process.stdout.write(chunk));
|
|
51
|
+
stream.on("end", () => console.log("\ndone"));
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Concatenating the emitted chunks yields exactly the same string as `invoke()`'s `message.content` for the same input.
|
|
55
|
+
|
|
56
|
+
### Deriving content from an `mcpConfig`
|
|
57
|
+
|
|
58
|
+
When constructed with an `mcpConfig`, the content includes a deterministic tools summary — one entry per MCP server (keys sorted), with the server package name and its tool count:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const model = new MockModel({
|
|
62
|
+
modelId: "mock-model",
|
|
63
|
+
mcpConfig: {
|
|
64
|
+
mcpServers: {
|
|
65
|
+
weather: { package: "@mappies/mcp-weather", tools: [{ import: "getWeather" }, { import: "getForecast" }] },
|
|
66
|
+
web: { package: "@mappies/mcp-web", tools: [{ import: "search" }] },
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// message.content:
|
|
72
|
+
// {
|
|
73
|
+
// "modelId": "mock-model",
|
|
74
|
+
// "tools": {
|
|
75
|
+
// "weather": { "package": "@mappies/mcp-weather", "tools": 2 },
|
|
76
|
+
// "web": { "package": "@mappies/mcp-web", "tools": 1 }
|
|
77
|
+
// }
|
|
78
|
+
// }
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The mock reads `mcpConfig.mcpServers` only to build this summary — it never executes tools.
|
|
82
|
+
|
|
83
|
+
## Simulated errors
|
|
84
|
+
|
|
85
|
+
The standalone word **"error"** is a reserved trigger in the **last** input message. When the last message's `content` contains the whole word "error" (case-insensitive — e.g. `"error"`, `"Error"`, `"ERROR"`, `"an error occurred"`), the mock deliberately simulates a failure:
|
|
86
|
+
|
|
87
|
+
- `invoke()` rejects the returned Promise with an `Error`.
|
|
88
|
+
- `stream()` emits an `"error"` event and then ends the stream.
|
|
89
|
+
|
|
90
|
+
Substrings inside larger words (e.g. `"errors"`, `"terror"`) do **not** trigger it, and an empty `messages` array produces a normal, valid response.
|
|
91
|
+
|
|
92
|
+
## API
|
|
93
|
+
|
|
94
|
+
### `new MockModel(options)`
|
|
95
|
+
|
|
96
|
+
| Option | Type | Required | Description |
|
|
97
|
+
| ----------- | ----------- | -------- | ------------------------------------------------------- |
|
|
98
|
+
| `modelId` | `string` | yes | Included in the deterministic content. |
|
|
99
|
+
| `mcpConfig` | `McpConfig` | no | Used to derive the tools summary. Omit for empty tools. |
|
|
100
|
+
|
|
101
|
+
### Methods
|
|
102
|
+
|
|
103
|
+
- `invoke(messages, option?): Promise<ResponseMessage>`
|
|
104
|
+
- `stream(messages, option?): Promise<Readable>`
|
|
105
|
+
|
|
106
|
+
> Note: `message.createdTimestamp` uses the current wall-clock time and is intentionally **not** part of the determinism guarantee. Only `message.content` and `tokenUsage` are deterministic.
|
|
107
|
+
|
|
108
|
+
## Development
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
npm install # install dependencies
|
|
112
|
+
npm test # run the vitest suite once
|
|
113
|
+
npm run test-watch # run vitest in watch mode
|
|
114
|
+
npm run build # build ESM + CJS bundles with tsdown
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The build emits both ESM (`dist/index.mjs`) and CommonJS (`dist/index.cjs`) outputs plus type declarations. Tests are plain vitest unit tests (no property-based testing).
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
PolyForm-Noncommercial-1.0.0
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let stream = require("stream");
|
|
3
|
+
let _mappies_miniwat_agent_core = require("@mappies/miniwat-agent-core");
|
|
4
|
+
//#region src/mockContent.ts
|
|
5
|
+
/**
|
|
6
|
+
* Deterministic, fixed error message used when the Simulated_Error_Condition
|
|
7
|
+
* is met (the last input message contains the whole word "error").
|
|
8
|
+
*/
|
|
9
|
+
const SIMULATED_ERROR_MESSAGE = "Simulated error: last message contained the word 'error'";
|
|
10
|
+
/**
|
|
11
|
+
* Determines whether the mock model should simulate an error for the given
|
|
12
|
+
* conversation. The Simulated_Error_Condition is met when the LAST message's
|
|
13
|
+
* content contains the whole word "error" (case-insensitive).
|
|
14
|
+
*
|
|
15
|
+
* Returns `false` when the message array is empty (no last element).
|
|
16
|
+
*/
|
|
17
|
+
function shouldSimulateError(messages) {
|
|
18
|
+
const last = messages[messages.length - 1];
|
|
19
|
+
if (!last) return false;
|
|
20
|
+
return /\berror\b/i.test(last.content ?? "");
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Builds the deterministic {@link ToolsSummary} from the constructor
|
|
24
|
+
* `mcpConfig`. Server keys are iterated in sorted order so the resulting
|
|
25
|
+
* object (and any downstream serialization) is independent of insertion
|
|
26
|
+
* order. Each entry captures the server package name and the count of tool
|
|
27
|
+
* imports it declares.
|
|
28
|
+
*
|
|
29
|
+
* Returns an empty object when `mcpConfig` is undefined or has no servers.
|
|
30
|
+
*/
|
|
31
|
+
function buildToolsSummary(mcpConfig) {
|
|
32
|
+
const servers = mcpConfig?.mcpServers ?? {};
|
|
33
|
+
const summary = {};
|
|
34
|
+
for (const key of Object.keys(servers).sort()) {
|
|
35
|
+
const server = servers[key];
|
|
36
|
+
summary[key] = {
|
|
37
|
+
package: server.package,
|
|
38
|
+
tools: server.tools?.length ?? 0
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return summary;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Builds a deterministic {@link TokenUsage} for the given conversation and
|
|
45
|
+
* generated content. `inputTokens` is the sum of the character lengths of all
|
|
46
|
+
* input message contents (0 for an empty array or missing content),
|
|
47
|
+
* `outputTokens` is the character length of the generated `content`, and
|
|
48
|
+
* `totalTokens` is their sum.
|
|
49
|
+
*/
|
|
50
|
+
function buildTokenUsage(messages, content) {
|
|
51
|
+
const inputTokens = messages.reduce((sum, m) => sum + (m.content?.length ?? 0), 0);
|
|
52
|
+
const outputTokens = content.length;
|
|
53
|
+
return {
|
|
54
|
+
inputTokens,
|
|
55
|
+
outputTokens,
|
|
56
|
+
totalTokens: inputTokens + outputTokens
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Builds the Deterministic_Content for the mock model as a pure function of
|
|
61
|
+
* the constructor `modelId` and `mcpConfig`. The resulting string is a stable
|
|
62
|
+
* JSON serialization of a {@link MockContentShape} object `{ modelId, tools }`,
|
|
63
|
+
* where `tools` is the {@link ToolsSummary} derived from `mcpConfig` (with
|
|
64
|
+
* server keys emitted in sorted order via {@link buildToolsSummary}).
|
|
65
|
+
*
|
|
66
|
+
* The `messages` and `option` parameters are accepted for signature
|
|
67
|
+
* completeness so callers can pass the full conversation context, even though
|
|
68
|
+
* the default content derives only from `modelId` and `mcpConfig`.
|
|
69
|
+
*/
|
|
70
|
+
function buildMockContent({ modelId, mcpConfig }) {
|
|
71
|
+
const shape = {
|
|
72
|
+
modelId,
|
|
73
|
+
tools: buildToolsSummary(mcpConfig)
|
|
74
|
+
};
|
|
75
|
+
return JSON.stringify(shape, null, 2);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Splits `content` into deterministic, fixed-size chunks whose in-order
|
|
79
|
+
* concatenation equals the original `content`. Returns `[""]` for empty
|
|
80
|
+
* content so a stream still emits a single chunk before ending.
|
|
81
|
+
*/
|
|
82
|
+
function chunkContent(content, size = 32) {
|
|
83
|
+
if (content.length === 0) return [""];
|
|
84
|
+
const chunks = [];
|
|
85
|
+
for (let i = 0; i < content.length; i += size) chunks.push(content.slice(i, i + size));
|
|
86
|
+
return chunks;
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/mockModel.ts
|
|
90
|
+
/**
|
|
91
|
+
* A deterministic {@link Model} implementation intended for use in automated
|
|
92
|
+
* tests. Given identical inputs, both {@link MockModel.invoke} and
|
|
93
|
+
* {@link MockModel.stream} produce identical response content.
|
|
94
|
+
*
|
|
95
|
+
* Unlike the real sibling providers, `MockModel` performs no network calls and
|
|
96
|
+
* instantiates no model SDK client and no `ToolDispatcher`. It depends on
|
|
97
|
+
* `@mappies/miniwat-mcp-core` only for the {@link McpConfig} type and to read
|
|
98
|
+
* `mcpConfig.mcpServers` when deriving the deterministic tools summary.
|
|
99
|
+
*/
|
|
100
|
+
var MockModel = class {
|
|
101
|
+
constructor(options) {
|
|
102
|
+
this.modelId = options.modelId;
|
|
103
|
+
this.mcpConfig = options.mcpConfig;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Computes the Deterministic_Content for the given conversation as a pure
|
|
107
|
+
* function of the constructor `modelId`, the constructor `mcpConfig`, the
|
|
108
|
+
* input `messages`, and the relevant `option` fields.
|
|
109
|
+
*/
|
|
110
|
+
deriveContent(messages, option) {
|
|
111
|
+
return buildMockContent({
|
|
112
|
+
modelId: this.modelId,
|
|
113
|
+
mcpConfig: this.mcpConfig,
|
|
114
|
+
messages,
|
|
115
|
+
option
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async invoke(messages, option) {
|
|
119
|
+
if (shouldSimulateError(messages)) throw new Error(SIMULATED_ERROR_MESSAGE);
|
|
120
|
+
const content = this.deriveContent(messages, option);
|
|
121
|
+
const tokenUsage = buildTokenUsage(messages, content);
|
|
122
|
+
const message = new _mappies_miniwat_agent_core.Message({
|
|
123
|
+
type: _mappies_miniwat_agent_core.MessageType.Agent,
|
|
124
|
+
content,
|
|
125
|
+
createdTimestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
126
|
+
});
|
|
127
|
+
return new _mappies_miniwat_agent_core.ResponseMessage({
|
|
128
|
+
message,
|
|
129
|
+
stopReason: _mappies_miniwat_agent_core.StopReason.END_TURN,
|
|
130
|
+
tokenUsage
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
async stream(messages, option) {
|
|
134
|
+
const out = new stream.PassThrough({ encoding: "utf8" });
|
|
135
|
+
(async () => {
|
|
136
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
137
|
+
try {
|
|
138
|
+
if (shouldSimulateError(messages)) {
|
|
139
|
+
out.emit("error", /* @__PURE__ */ new Error(SIMULATED_ERROR_MESSAGE));
|
|
140
|
+
out.write("");
|
|
141
|
+
out.end();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const content = this.deriveContent(messages, option);
|
|
145
|
+
for (const chunk of chunkContent(content)) out.write(chunk);
|
|
146
|
+
out.write("");
|
|
147
|
+
out.end();
|
|
148
|
+
} catch (e) {
|
|
149
|
+
out.emit("error", e);
|
|
150
|
+
out.write("");
|
|
151
|
+
out.end();
|
|
152
|
+
}
|
|
153
|
+
})();
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
//#endregion
|
|
158
|
+
exports.MockModel = MockModel;
|
|
159
|
+
|
|
160
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Message","MessageType","ResponseMessage","StopReason","PassThrough"],"sources":["../src/mockContent.ts","../src/mockModel.ts"],"sourcesContent":["import type { McpConfig } from \"@mappies/miniwat-mcp-core\";\nimport type { Message, ModelOption, TokenUsage } from \"@mappies/miniwat-agent-core\";\n\n/**\n * A single entry in the {@link ToolsSummary}, summarizing one MCP server:\n * the server package name and the number of tool imports it declares.\n */\nexport interface ToolsSummaryEntry\n{\n package: string;\n tools: number;\n}\n\n/**\n * Deterministic summary of the configured MCP servers, keyed by server key.\n * Derived purely from the constructor `mcpConfig`.\n */\nexport type ToolsSummary = Record<string, ToolsSummaryEntry>;\n\n/**\n * The object shape serialized into the Deterministic_Content: the constructor\n * `modelId` plus the {@link ToolsSummary} derived from `mcpConfig`.\n */\nexport interface MockContentShape\n{\n modelId: string;\n tools: ToolsSummary;\n}\n\n/**\n * Constructor options for the mock model: a required `modelId` and an optional\n * `mcpConfig` used to derive the {@link ToolsSummary}.\n */\nexport interface MockModelOptions\n{\n modelId: string;\n mcpConfig?: McpConfig;\n}\n\n/**\n * Deterministic, fixed error message used when the Simulated_Error_Condition\n * is met (the last input message contains the whole word \"error\").\n */\nexport const SIMULATED_ERROR_MESSAGE =\n \"Simulated error: last message contained the word 'error'\";\n\n/**\n * Determines whether the mock model should simulate an error for the given\n * conversation. The Simulated_Error_Condition is met when the LAST message's\n * content contains the whole word \"error\" (case-insensitive).\n *\n * Returns `false` when the message array is empty (no last element).\n */\nexport function shouldSimulateError(messages: Message[]): boolean\n{\n const last = messages[messages.length - 1];\n if (!last) return false;\n return /\\berror\\b/i.test(last.content ?? \"\");\n}\n\n/**\n * Builds the deterministic {@link ToolsSummary} from the constructor\n * `mcpConfig`. Server keys are iterated in sorted order so the resulting\n * object (and any downstream serialization) is independent of insertion\n * order. Each entry captures the server package name and the count of tool\n * imports it declares.\n *\n * Returns an empty object when `mcpConfig` is undefined or has no servers.\n */\nexport function buildToolsSummary(mcpConfig?: McpConfig): ToolsSummary\n{\n const servers = mcpConfig?.mcpServers ?? {};\n const summary: ToolsSummary = {};\n for (const key of Object.keys(servers).sort())\n {\n const server = servers[key];\n summary[key] = { package: server.package, tools: server.tools?.length ?? 0 };\n }\n return summary;\n}\n\n/**\n * Builds a deterministic {@link TokenUsage} for the given conversation and\n * generated content. `inputTokens` is the sum of the character lengths of all\n * input message contents (0 for an empty array or missing content),\n * `outputTokens` is the character length of the generated `content`, and\n * `totalTokens` is their sum.\n */\nexport function buildTokenUsage(messages: Message[], content: string): TokenUsage\n{\n const inputTokens = messages.reduce((sum, m) => sum + (m.content?.length ?? 0), 0);\n const outputTokens = content.length;\n return { inputTokens, outputTokens, totalTokens: inputTokens + outputTokens };\n}\n\n/**\n * Builds the Deterministic_Content for the mock model as a pure function of\n * the constructor `modelId` and `mcpConfig`. The resulting string is a stable\n * JSON serialization of a {@link MockContentShape} object `{ modelId, tools }`,\n * where `tools` is the {@link ToolsSummary} derived from `mcpConfig` (with\n * server keys emitted in sorted order via {@link buildToolsSummary}).\n *\n * The `messages` and `option` parameters are accepted for signature\n * completeness so callers can pass the full conversation context, even though\n * the default content derives only from `modelId` and `mcpConfig`.\n */\nexport function buildMockContent({\n modelId,\n mcpConfig,\n}: {\n modelId: string;\n mcpConfig?: McpConfig;\n messages: Message[];\n option?: ModelOption;\n}): string\n{\n const shape: MockContentShape = {\n modelId,\n tools: buildToolsSummary(mcpConfig),\n };\n return JSON.stringify(shape, null, 2);\n}\n\n/**\n * Splits `content` into deterministic, fixed-size chunks whose in-order\n * concatenation equals the original `content`. Returns `[\"\"]` for empty\n * content so a stream still emits a single chunk before ending.\n */\nexport function chunkContent(content: string, size = 32): string[]\n{\n if (content.length === 0) return [\"\"];\n const chunks: string[] = [];\n for (let i = 0; i < content.length; i += size) chunks.push(content.slice(i, i + size));\n return chunks;\n}\n","import { PassThrough, Readable } from \"stream\";\nimport\n{\n Model,\n Message,\n MessageType,\n ModelOption,\n ResponseMessage,\n StopReason,\n TokenUsage,\n} from \"@mappies/miniwat-agent-core\";\nimport type { McpConfig } from \"@mappies/miniwat-mcp-core\";\nimport\n{\n buildMockContent,\n buildTokenUsage,\n chunkContent,\n shouldSimulateError,\n SIMULATED_ERROR_MESSAGE,\n MockModelOptions,\n} from \"./mockContent\";\n\n/**\n * A deterministic {@link Model} implementation intended for use in automated\n * tests. Given identical inputs, both {@link MockModel.invoke} and\n * {@link MockModel.stream} produce identical response content.\n *\n * Unlike the real sibling providers, `MockModel` performs no network calls and\n * instantiates no model SDK client and no `ToolDispatcher`. It depends on\n * `@mappies/miniwat-mcp-core` only for the {@link McpConfig} type and to read\n * `mcpConfig.mcpServers` when deriving the deterministic tools summary.\n */\nexport class MockModel implements Model\n{\n private modelId: string;\n private mcpConfig?: McpConfig;\n\n constructor(options: MockModelOptions)\n {\n this.modelId = options.modelId;\n this.mcpConfig = options.mcpConfig;\n // NOTE: no ToolDispatcher, no SDK client instantiated.\n }\n\n /**\n * Computes the Deterministic_Content for the given conversation as a pure\n * function of the constructor `modelId`, the constructor `mcpConfig`, the\n * input `messages`, and the relevant `option` fields.\n */\n private deriveContent(messages: Message[], option?: ModelOption): string\n {\n return buildMockContent({\n modelId: this.modelId,\n mcpConfig: this.mcpConfig,\n messages,\n option,\n });\n }\n\n async invoke(messages: Message[], option?: ModelOption): Promise<ResponseMessage>\n {\n if (shouldSimulateError(messages))\n {\n throw new Error(SIMULATED_ERROR_MESSAGE);\n }\n\n const content = this.deriveContent(messages, option);\n const tokenUsage: TokenUsage = buildTokenUsage(messages, content);\n const message = new Message({\n type: MessageType.Agent,\n content,\n createdTimestamp: new Date().toISOString(),\n });\n\n return new ResponseMessage({\n message,\n stopReason: StopReason.END_TURN,\n tokenUsage,\n });\n }\n\n async stream(messages: Message[], option?: ModelOption): Promise<Readable>\n {\n const out = new PassThrough({ encoding: \"utf8\" });\n\n (async () =>\n {\n // Defer to a later tick so the returned stream is handed back to the\n // caller and its listeners (notably \"error\") are attached before any\n // chunk or error is emitted. Writing synchronously here would emit an\n // \"error\" event with no listeners attached, surfacing as an unhandled\n // error, and would make chunk writes synchronous rather than async.\n await new Promise<void>((resolve) => setImmediate(resolve));\n\n try\n {\n if (shouldSimulateError(messages))\n {\n out.emit(\"error\", new Error(SIMULATED_ERROR_MESSAGE));\n out.write(\"\");\n out.end();\n return;\n }\n\n const content = this.deriveContent(messages, option);\n for (const chunk of chunkContent(content))\n {\n out.write(chunk);\n }\n out.write(\"\");\n out.end();\n } catch (e)\n {\n out.emit(\"error\", e);\n out.write(\"\");\n out.end();\n }\n })();\n\n return out;\n }\n}\n"],"mappings":";;;;;;;;AA2CA,MAAa,0BACT;;;;;;;;AASJ,SAAgB,oBAAoB,UACpC;CACI,MAAM,OAAO,SAAS,SAAS,SAAS;CACxC,IAAI,CAAC,MAAM,OAAO;CAClB,OAAO,aAAa,KAAK,KAAK,WAAW,EAAE;AAC/C;;;;;;;;;;AAWA,SAAgB,kBAAkB,WAClC;CACI,MAAM,UAAU,WAAW,cAAc,CAAC;CAC1C,MAAM,UAAwB,CAAC;CAC/B,KAAK,MAAM,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,KAAK,GAC5C;EACI,MAAM,SAAS,QAAQ;EACvB,QAAQ,OAAO;GAAE,SAAS,OAAO;GAAS,OAAO,OAAO,OAAO,UAAU;EAAE;CAC/E;CACA,OAAO;AACX;;;;;;;;AASA,SAAgB,gBAAgB,UAAqB,SACrD;CACI,MAAM,cAAc,SAAS,QAAQ,KAAK,MAAM,OAAO,EAAE,SAAS,UAAU,IAAI,CAAC;CACjF,MAAM,eAAe,QAAQ;CAC7B,OAAO;EAAE;EAAa;EAAc,aAAa,cAAc;CAAa;AAChF;;;;;;;;;;;;AAaA,SAAgB,iBAAiB,EAC7B,SACA,aAOJ;CACI,MAAM,QAA0B;EAC5B;EACA,OAAO,kBAAkB,SAAS;CACtC;CACA,OAAO,KAAK,UAAU,OAAO,MAAM,CAAC;AACxC;;;;;;AAOA,SAAgB,aAAa,SAAiB,OAAO,IACrD;CACI,IAAI,QAAQ,WAAW,GAAG,OAAO,CAAC,EAAE;CACpC,MAAM,SAAmB,CAAC;CAC1B,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK,MAAM,OAAO,KAAK,QAAQ,MAAM,GAAG,IAAI,IAAI,CAAC;CACrF,OAAO;AACX;;;;;;;;;;;;;ACtGA,IAAa,YAAb,MACA;CAII,YAAY,SACZ;EACI,KAAK,UAAU,QAAQ;EACvB,KAAK,YAAY,QAAQ;CAE7B;;;;;;CAOA,cAAsB,UAAqB,QAC3C;EACI,OAAO,iBAAiB;GACpB,SAAS,KAAK;GACd,WAAW,KAAK;GAChB;GACA;EACJ,CAAC;CACL;CAEA,MAAM,OAAO,UAAqB,QAClC;EACI,IAAI,oBAAoB,QAAQ,GAE5B,MAAM,IAAI,MAAM,uBAAuB;EAG3C,MAAM,UAAU,KAAK,cAAc,UAAU,MAAM;EACnD,MAAM,aAAyB,gBAAgB,UAAU,OAAO;EAChE,MAAM,UAAU,IAAIA,4BAAAA,QAAQ;GACxB,MAAMC,4BAAAA,YAAY;GAClB;GACA,mCAAkB,IAAI,KAAK,EAAA,CAAE,YAAY;EAC7C,CAAC;EAED,OAAO,IAAIC,4BAAAA,gBAAgB;GACvB;GACA,YAAYC,4BAAAA,WAAW;GACvB;EACJ,CAAC;CACL;CAEA,MAAM,OAAO,UAAqB,QAClC;EACI,MAAM,MAAM,IAAIC,OAAAA,YAAY,EAAE,UAAU,OAAO,CAAC;EAEhD,CAAC,YACD;GAMI,MAAM,IAAI,SAAe,YAAY,aAAa,OAAO,CAAC;GAE1D,IACA;IACI,IAAI,oBAAoB,QAAQ,GAChC;KACI,IAAI,KAAK,yBAAS,IAAI,MAAM,uBAAuB,CAAC;KACpD,IAAI,MAAM,EAAE;KACZ,IAAI,IAAI;KACR;IACJ;IAEA,MAAM,UAAU,KAAK,cAAc,UAAU,MAAM;IACnD,KAAK,MAAM,SAAS,aAAa,OAAO,GAEpC,IAAI,MAAM,KAAK;IAEnB,IAAI,MAAM,EAAE;IACZ,IAAI,IAAI;GACZ,SAAS,GACT;IACI,IAAI,KAAK,SAAS,CAAC;IACnB,IAAI,MAAM,EAAE;IACZ,IAAI,IAAI;GACZ;EACJ,EAAA,CAAG;EAEH,OAAO;CACX;AACJ"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Readable } from "stream";
|
|
2
|
+
import { Message, Model, ModelOption, ResponseMessage } from "@mappies/miniwat-agent-core";
|
|
3
|
+
import { McpConfig } from "@mappies/miniwat-mcp-core";
|
|
4
|
+
//#region src/mockContent.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Constructor options for the mock model: a required `modelId` and an optional
|
|
7
|
+
* `mcpConfig` used to derive the {@link ToolsSummary}.
|
|
8
|
+
*/
|
|
9
|
+
interface MockModelOptions {
|
|
10
|
+
modelId: string;
|
|
11
|
+
mcpConfig?: McpConfig;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/mockModel.d.ts
|
|
15
|
+
/**
|
|
16
|
+
* A deterministic {@link Model} implementation intended for use in automated
|
|
17
|
+
* tests. Given identical inputs, both {@link MockModel.invoke} and
|
|
18
|
+
* {@link MockModel.stream} produce identical response content.
|
|
19
|
+
*
|
|
20
|
+
* Unlike the real sibling providers, `MockModel` performs no network calls and
|
|
21
|
+
* instantiates no model SDK client and no `ToolDispatcher`. It depends on
|
|
22
|
+
* `@mappies/miniwat-mcp-core` only for the {@link McpConfig} type and to read
|
|
23
|
+
* `mcpConfig.mcpServers` when deriving the deterministic tools summary.
|
|
24
|
+
*/
|
|
25
|
+
export declare class MockModel implements Model {
|
|
26
|
+
private modelId;
|
|
27
|
+
private mcpConfig?;
|
|
28
|
+
constructor(options: MockModelOptions);
|
|
29
|
+
/**
|
|
30
|
+
* Computes the Deterministic_Content for the given conversation as a pure
|
|
31
|
+
* function of the constructor `modelId`, the constructor `mcpConfig`, the
|
|
32
|
+
* input `messages`, and the relevant `option` fields.
|
|
33
|
+
*/
|
|
34
|
+
private deriveContent;
|
|
35
|
+
invoke(messages: Message[], option?: ModelOption): Promise<ResponseMessage>;
|
|
36
|
+
stream(messages: Message[], option?: ModelOption): Promise<Readable>;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export type { MockModelOptions };
|
|
40
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Readable } from "stream";
|
|
2
|
+
import { Message, Model, ModelOption, ResponseMessage } from "@mappies/miniwat-agent-core";
|
|
3
|
+
import { McpConfig } from "@mappies/miniwat-mcp-core";
|
|
4
|
+
//#region src/mockContent.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Constructor options for the mock model: a required `modelId` and an optional
|
|
7
|
+
* `mcpConfig` used to derive the {@link ToolsSummary}.
|
|
8
|
+
*/
|
|
9
|
+
interface MockModelOptions {
|
|
10
|
+
modelId: string;
|
|
11
|
+
mcpConfig?: McpConfig;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/mockModel.d.ts
|
|
15
|
+
/**
|
|
16
|
+
* A deterministic {@link Model} implementation intended for use in automated
|
|
17
|
+
* tests. Given identical inputs, both {@link MockModel.invoke} and
|
|
18
|
+
* {@link MockModel.stream} produce identical response content.
|
|
19
|
+
*
|
|
20
|
+
* Unlike the real sibling providers, `MockModel` performs no network calls and
|
|
21
|
+
* instantiates no model SDK client and no `ToolDispatcher`. It depends on
|
|
22
|
+
* `@mappies/miniwat-mcp-core` only for the {@link McpConfig} type and to read
|
|
23
|
+
* `mcpConfig.mcpServers` when deriving the deterministic tools summary.
|
|
24
|
+
*/
|
|
25
|
+
export declare class MockModel implements Model {
|
|
26
|
+
private modelId;
|
|
27
|
+
private mcpConfig?;
|
|
28
|
+
constructor(options: MockModelOptions);
|
|
29
|
+
/**
|
|
30
|
+
* Computes the Deterministic_Content for the given conversation as a pure
|
|
31
|
+
* function of the constructor `modelId`, the constructor `mcpConfig`, the
|
|
32
|
+
* input `messages`, and the relevant `option` fields.
|
|
33
|
+
*/
|
|
34
|
+
private deriveContent;
|
|
35
|
+
invoke(messages: Message[], option?: ModelOption): Promise<ResponseMessage>;
|
|
36
|
+
stream(messages: Message[], option?: ModelOption): Promise<Readable>;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export type { MockModelOptions };
|
|
40
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { PassThrough } from "stream";
|
|
2
|
+
import { Message, MessageType, ResponseMessage, StopReason } from "@mappies/miniwat-agent-core";
|
|
3
|
+
//#region src/mockContent.ts
|
|
4
|
+
/**
|
|
5
|
+
* Deterministic, fixed error message used when the Simulated_Error_Condition
|
|
6
|
+
* is met (the last input message contains the whole word "error").
|
|
7
|
+
*/
|
|
8
|
+
const SIMULATED_ERROR_MESSAGE = "Simulated error: last message contained the word 'error'";
|
|
9
|
+
/**
|
|
10
|
+
* Determines whether the mock model should simulate an error for the given
|
|
11
|
+
* conversation. The Simulated_Error_Condition is met when the LAST message's
|
|
12
|
+
* content contains the whole word "error" (case-insensitive).
|
|
13
|
+
*
|
|
14
|
+
* Returns `false` when the message array is empty (no last element).
|
|
15
|
+
*/
|
|
16
|
+
function shouldSimulateError(messages) {
|
|
17
|
+
const last = messages[messages.length - 1];
|
|
18
|
+
if (!last) return false;
|
|
19
|
+
return /\berror\b/i.test(last.content ?? "");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Builds the deterministic {@link ToolsSummary} from the constructor
|
|
23
|
+
* `mcpConfig`. Server keys are iterated in sorted order so the resulting
|
|
24
|
+
* object (and any downstream serialization) is independent of insertion
|
|
25
|
+
* order. Each entry captures the server package name and the count of tool
|
|
26
|
+
* imports it declares.
|
|
27
|
+
*
|
|
28
|
+
* Returns an empty object when `mcpConfig` is undefined or has no servers.
|
|
29
|
+
*/
|
|
30
|
+
function buildToolsSummary(mcpConfig) {
|
|
31
|
+
const servers = mcpConfig?.mcpServers ?? {};
|
|
32
|
+
const summary = {};
|
|
33
|
+
for (const key of Object.keys(servers).sort()) {
|
|
34
|
+
const server = servers[key];
|
|
35
|
+
summary[key] = {
|
|
36
|
+
package: server.package,
|
|
37
|
+
tools: server.tools?.length ?? 0
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return summary;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Builds a deterministic {@link TokenUsage} for the given conversation and
|
|
44
|
+
* generated content. `inputTokens` is the sum of the character lengths of all
|
|
45
|
+
* input message contents (0 for an empty array or missing content),
|
|
46
|
+
* `outputTokens` is the character length of the generated `content`, and
|
|
47
|
+
* `totalTokens` is their sum.
|
|
48
|
+
*/
|
|
49
|
+
function buildTokenUsage(messages, content) {
|
|
50
|
+
const inputTokens = messages.reduce((sum, m) => sum + (m.content?.length ?? 0), 0);
|
|
51
|
+
const outputTokens = content.length;
|
|
52
|
+
return {
|
|
53
|
+
inputTokens,
|
|
54
|
+
outputTokens,
|
|
55
|
+
totalTokens: inputTokens + outputTokens
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Builds the Deterministic_Content for the mock model as a pure function of
|
|
60
|
+
* the constructor `modelId` and `mcpConfig`. The resulting string is a stable
|
|
61
|
+
* JSON serialization of a {@link MockContentShape} object `{ modelId, tools }`,
|
|
62
|
+
* where `tools` is the {@link ToolsSummary} derived from `mcpConfig` (with
|
|
63
|
+
* server keys emitted in sorted order via {@link buildToolsSummary}).
|
|
64
|
+
*
|
|
65
|
+
* The `messages` and `option` parameters are accepted for signature
|
|
66
|
+
* completeness so callers can pass the full conversation context, even though
|
|
67
|
+
* the default content derives only from `modelId` and `mcpConfig`.
|
|
68
|
+
*/
|
|
69
|
+
function buildMockContent({ modelId, mcpConfig }) {
|
|
70
|
+
const shape = {
|
|
71
|
+
modelId,
|
|
72
|
+
tools: buildToolsSummary(mcpConfig)
|
|
73
|
+
};
|
|
74
|
+
return JSON.stringify(shape, null, 2);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Splits `content` into deterministic, fixed-size chunks whose in-order
|
|
78
|
+
* concatenation equals the original `content`. Returns `[""]` for empty
|
|
79
|
+
* content so a stream still emits a single chunk before ending.
|
|
80
|
+
*/
|
|
81
|
+
function chunkContent(content, size = 32) {
|
|
82
|
+
if (content.length === 0) return [""];
|
|
83
|
+
const chunks = [];
|
|
84
|
+
for (let i = 0; i < content.length; i += size) chunks.push(content.slice(i, i + size));
|
|
85
|
+
return chunks;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/mockModel.ts
|
|
89
|
+
/**
|
|
90
|
+
* A deterministic {@link Model} implementation intended for use in automated
|
|
91
|
+
* tests. Given identical inputs, both {@link MockModel.invoke} and
|
|
92
|
+
* {@link MockModel.stream} produce identical response content.
|
|
93
|
+
*
|
|
94
|
+
* Unlike the real sibling providers, `MockModel` performs no network calls and
|
|
95
|
+
* instantiates no model SDK client and no `ToolDispatcher`. It depends on
|
|
96
|
+
* `@mappies/miniwat-mcp-core` only for the {@link McpConfig} type and to read
|
|
97
|
+
* `mcpConfig.mcpServers` when deriving the deterministic tools summary.
|
|
98
|
+
*/
|
|
99
|
+
var MockModel = class {
|
|
100
|
+
constructor(options) {
|
|
101
|
+
this.modelId = options.modelId;
|
|
102
|
+
this.mcpConfig = options.mcpConfig;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Computes the Deterministic_Content for the given conversation as a pure
|
|
106
|
+
* function of the constructor `modelId`, the constructor `mcpConfig`, the
|
|
107
|
+
* input `messages`, and the relevant `option` fields.
|
|
108
|
+
*/
|
|
109
|
+
deriveContent(messages, option) {
|
|
110
|
+
return buildMockContent({
|
|
111
|
+
modelId: this.modelId,
|
|
112
|
+
mcpConfig: this.mcpConfig,
|
|
113
|
+
messages,
|
|
114
|
+
option
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
async invoke(messages, option) {
|
|
118
|
+
if (shouldSimulateError(messages)) throw new Error(SIMULATED_ERROR_MESSAGE);
|
|
119
|
+
const content = this.deriveContent(messages, option);
|
|
120
|
+
const tokenUsage = buildTokenUsage(messages, content);
|
|
121
|
+
const message = new Message({
|
|
122
|
+
type: MessageType.Agent,
|
|
123
|
+
content,
|
|
124
|
+
createdTimestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
125
|
+
});
|
|
126
|
+
return new ResponseMessage({
|
|
127
|
+
message,
|
|
128
|
+
stopReason: StopReason.END_TURN,
|
|
129
|
+
tokenUsage
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async stream(messages, option) {
|
|
133
|
+
const out = new PassThrough({ encoding: "utf8" });
|
|
134
|
+
(async () => {
|
|
135
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
136
|
+
try {
|
|
137
|
+
if (shouldSimulateError(messages)) {
|
|
138
|
+
out.emit("error", /* @__PURE__ */ new Error(SIMULATED_ERROR_MESSAGE));
|
|
139
|
+
out.write("");
|
|
140
|
+
out.end();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const content = this.deriveContent(messages, option);
|
|
144
|
+
for (const chunk of chunkContent(content)) out.write(chunk);
|
|
145
|
+
out.write("");
|
|
146
|
+
out.end();
|
|
147
|
+
} catch (e) {
|
|
148
|
+
out.emit("error", e);
|
|
149
|
+
out.write("");
|
|
150
|
+
out.end();
|
|
151
|
+
}
|
|
152
|
+
})();
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
//#endregion
|
|
157
|
+
export { MockModel };
|
|
158
|
+
|
|
159
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/mockContent.ts","../src/mockModel.ts"],"sourcesContent":["import type { McpConfig } from \"@mappies/miniwat-mcp-core\";\nimport type { Message, ModelOption, TokenUsage } from \"@mappies/miniwat-agent-core\";\n\n/**\n * A single entry in the {@link ToolsSummary}, summarizing one MCP server:\n * the server package name and the number of tool imports it declares.\n */\nexport interface ToolsSummaryEntry\n{\n package: string;\n tools: number;\n}\n\n/**\n * Deterministic summary of the configured MCP servers, keyed by server key.\n * Derived purely from the constructor `mcpConfig`.\n */\nexport type ToolsSummary = Record<string, ToolsSummaryEntry>;\n\n/**\n * The object shape serialized into the Deterministic_Content: the constructor\n * `modelId` plus the {@link ToolsSummary} derived from `mcpConfig`.\n */\nexport interface MockContentShape\n{\n modelId: string;\n tools: ToolsSummary;\n}\n\n/**\n * Constructor options for the mock model: a required `modelId` and an optional\n * `mcpConfig` used to derive the {@link ToolsSummary}.\n */\nexport interface MockModelOptions\n{\n modelId: string;\n mcpConfig?: McpConfig;\n}\n\n/**\n * Deterministic, fixed error message used when the Simulated_Error_Condition\n * is met (the last input message contains the whole word \"error\").\n */\nexport const SIMULATED_ERROR_MESSAGE =\n \"Simulated error: last message contained the word 'error'\";\n\n/**\n * Determines whether the mock model should simulate an error for the given\n * conversation. The Simulated_Error_Condition is met when the LAST message's\n * content contains the whole word \"error\" (case-insensitive).\n *\n * Returns `false` when the message array is empty (no last element).\n */\nexport function shouldSimulateError(messages: Message[]): boolean\n{\n const last = messages[messages.length - 1];\n if (!last) return false;\n return /\\berror\\b/i.test(last.content ?? \"\");\n}\n\n/**\n * Builds the deterministic {@link ToolsSummary} from the constructor\n * `mcpConfig`. Server keys are iterated in sorted order so the resulting\n * object (and any downstream serialization) is independent of insertion\n * order. Each entry captures the server package name and the count of tool\n * imports it declares.\n *\n * Returns an empty object when `mcpConfig` is undefined or has no servers.\n */\nexport function buildToolsSummary(mcpConfig?: McpConfig): ToolsSummary\n{\n const servers = mcpConfig?.mcpServers ?? {};\n const summary: ToolsSummary = {};\n for (const key of Object.keys(servers).sort())\n {\n const server = servers[key];\n summary[key] = { package: server.package, tools: server.tools?.length ?? 0 };\n }\n return summary;\n}\n\n/**\n * Builds a deterministic {@link TokenUsage} for the given conversation and\n * generated content. `inputTokens` is the sum of the character lengths of all\n * input message contents (0 for an empty array or missing content),\n * `outputTokens` is the character length of the generated `content`, and\n * `totalTokens` is their sum.\n */\nexport function buildTokenUsage(messages: Message[], content: string): TokenUsage\n{\n const inputTokens = messages.reduce((sum, m) => sum + (m.content?.length ?? 0), 0);\n const outputTokens = content.length;\n return { inputTokens, outputTokens, totalTokens: inputTokens + outputTokens };\n}\n\n/**\n * Builds the Deterministic_Content for the mock model as a pure function of\n * the constructor `modelId` and `mcpConfig`. The resulting string is a stable\n * JSON serialization of a {@link MockContentShape} object `{ modelId, tools }`,\n * where `tools` is the {@link ToolsSummary} derived from `mcpConfig` (with\n * server keys emitted in sorted order via {@link buildToolsSummary}).\n *\n * The `messages` and `option` parameters are accepted for signature\n * completeness so callers can pass the full conversation context, even though\n * the default content derives only from `modelId` and `mcpConfig`.\n */\nexport function buildMockContent({\n modelId,\n mcpConfig,\n}: {\n modelId: string;\n mcpConfig?: McpConfig;\n messages: Message[];\n option?: ModelOption;\n}): string\n{\n const shape: MockContentShape = {\n modelId,\n tools: buildToolsSummary(mcpConfig),\n };\n return JSON.stringify(shape, null, 2);\n}\n\n/**\n * Splits `content` into deterministic, fixed-size chunks whose in-order\n * concatenation equals the original `content`. Returns `[\"\"]` for empty\n * content so a stream still emits a single chunk before ending.\n */\nexport function chunkContent(content: string, size = 32): string[]\n{\n if (content.length === 0) return [\"\"];\n const chunks: string[] = [];\n for (let i = 0; i < content.length; i += size) chunks.push(content.slice(i, i + size));\n return chunks;\n}\n","import { PassThrough, Readable } from \"stream\";\nimport\n{\n Model,\n Message,\n MessageType,\n ModelOption,\n ResponseMessage,\n StopReason,\n TokenUsage,\n} from \"@mappies/miniwat-agent-core\";\nimport type { McpConfig } from \"@mappies/miniwat-mcp-core\";\nimport\n{\n buildMockContent,\n buildTokenUsage,\n chunkContent,\n shouldSimulateError,\n SIMULATED_ERROR_MESSAGE,\n MockModelOptions,\n} from \"./mockContent\";\n\n/**\n * A deterministic {@link Model} implementation intended for use in automated\n * tests. Given identical inputs, both {@link MockModel.invoke} and\n * {@link MockModel.stream} produce identical response content.\n *\n * Unlike the real sibling providers, `MockModel` performs no network calls and\n * instantiates no model SDK client and no `ToolDispatcher`. It depends on\n * `@mappies/miniwat-mcp-core` only for the {@link McpConfig} type and to read\n * `mcpConfig.mcpServers` when deriving the deterministic tools summary.\n */\nexport class MockModel implements Model\n{\n private modelId: string;\n private mcpConfig?: McpConfig;\n\n constructor(options: MockModelOptions)\n {\n this.modelId = options.modelId;\n this.mcpConfig = options.mcpConfig;\n // NOTE: no ToolDispatcher, no SDK client instantiated.\n }\n\n /**\n * Computes the Deterministic_Content for the given conversation as a pure\n * function of the constructor `modelId`, the constructor `mcpConfig`, the\n * input `messages`, and the relevant `option` fields.\n */\n private deriveContent(messages: Message[], option?: ModelOption): string\n {\n return buildMockContent({\n modelId: this.modelId,\n mcpConfig: this.mcpConfig,\n messages,\n option,\n });\n }\n\n async invoke(messages: Message[], option?: ModelOption): Promise<ResponseMessage>\n {\n if (shouldSimulateError(messages))\n {\n throw new Error(SIMULATED_ERROR_MESSAGE);\n }\n\n const content = this.deriveContent(messages, option);\n const tokenUsage: TokenUsage = buildTokenUsage(messages, content);\n const message = new Message({\n type: MessageType.Agent,\n content,\n createdTimestamp: new Date().toISOString(),\n });\n\n return new ResponseMessage({\n message,\n stopReason: StopReason.END_TURN,\n tokenUsage,\n });\n }\n\n async stream(messages: Message[], option?: ModelOption): Promise<Readable>\n {\n const out = new PassThrough({ encoding: \"utf8\" });\n\n (async () =>\n {\n // Defer to a later tick so the returned stream is handed back to the\n // caller and its listeners (notably \"error\") are attached before any\n // chunk or error is emitted. Writing synchronously here would emit an\n // \"error\" event with no listeners attached, surfacing as an unhandled\n // error, and would make chunk writes synchronous rather than async.\n await new Promise<void>((resolve) => setImmediate(resolve));\n\n try\n {\n if (shouldSimulateError(messages))\n {\n out.emit(\"error\", new Error(SIMULATED_ERROR_MESSAGE));\n out.write(\"\");\n out.end();\n return;\n }\n\n const content = this.deriveContent(messages, option);\n for (const chunk of chunkContent(content))\n {\n out.write(chunk);\n }\n out.write(\"\");\n out.end();\n } catch (e)\n {\n out.emit(\"error\", e);\n out.write(\"\");\n out.end();\n }\n })();\n\n return out;\n }\n}\n"],"mappings":";;;;;;;AA2CA,MAAa,0BACT;;;;;;;;AASJ,SAAgB,oBAAoB,UACpC;CACI,MAAM,OAAO,SAAS,SAAS,SAAS;CACxC,IAAI,CAAC,MAAM,OAAO;CAClB,OAAO,aAAa,KAAK,KAAK,WAAW,EAAE;AAC/C;;;;;;;;;;AAWA,SAAgB,kBAAkB,WAClC;CACI,MAAM,UAAU,WAAW,cAAc,CAAC;CAC1C,MAAM,UAAwB,CAAC;CAC/B,KAAK,MAAM,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,KAAK,GAC5C;EACI,MAAM,SAAS,QAAQ;EACvB,QAAQ,OAAO;GAAE,SAAS,OAAO;GAAS,OAAO,OAAO,OAAO,UAAU;EAAE;CAC/E;CACA,OAAO;AACX;;;;;;;;AASA,SAAgB,gBAAgB,UAAqB,SACrD;CACI,MAAM,cAAc,SAAS,QAAQ,KAAK,MAAM,OAAO,EAAE,SAAS,UAAU,IAAI,CAAC;CACjF,MAAM,eAAe,QAAQ;CAC7B,OAAO;EAAE;EAAa;EAAc,aAAa,cAAc;CAAa;AAChF;;;;;;;;;;;;AAaA,SAAgB,iBAAiB,EAC7B,SACA,aAOJ;CACI,MAAM,QAA0B;EAC5B;EACA,OAAO,kBAAkB,SAAS;CACtC;CACA,OAAO,KAAK,UAAU,OAAO,MAAM,CAAC;AACxC;;;;;;AAOA,SAAgB,aAAa,SAAiB,OAAO,IACrD;CACI,IAAI,QAAQ,WAAW,GAAG,OAAO,CAAC,EAAE;CACpC,MAAM,SAAmB,CAAC;CAC1B,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK,MAAM,OAAO,KAAK,QAAQ,MAAM,GAAG,IAAI,IAAI,CAAC;CACrF,OAAO;AACX;;;;;;;;;;;;;ACtGA,IAAa,YAAb,MACA;CAII,YAAY,SACZ;EACI,KAAK,UAAU,QAAQ;EACvB,KAAK,YAAY,QAAQ;CAE7B;;;;;;CAOA,cAAsB,UAAqB,QAC3C;EACI,OAAO,iBAAiB;GACpB,SAAS,KAAK;GACd,WAAW,KAAK;GAChB;GACA;EACJ,CAAC;CACL;CAEA,MAAM,OAAO,UAAqB,QAClC;EACI,IAAI,oBAAoB,QAAQ,GAE5B,MAAM,IAAI,MAAM,uBAAuB;EAG3C,MAAM,UAAU,KAAK,cAAc,UAAU,MAAM;EACnD,MAAM,aAAyB,gBAAgB,UAAU,OAAO;EAChE,MAAM,UAAU,IAAI,QAAQ;GACxB,MAAM,YAAY;GAClB;GACA,mCAAkB,IAAI,KAAK,EAAA,CAAE,YAAY;EAC7C,CAAC;EAED,OAAO,IAAI,gBAAgB;GACvB;GACA,YAAY,WAAW;GACvB;EACJ,CAAC;CACL;CAEA,MAAM,OAAO,UAAqB,QAClC;EACI,MAAM,MAAM,IAAI,YAAY,EAAE,UAAU,OAAO,CAAC;EAEhD,CAAC,YACD;GAMI,MAAM,IAAI,SAAe,YAAY,aAAa,OAAO,CAAC;GAE1D,IACA;IACI,IAAI,oBAAoB,QAAQ,GAChC;KACI,IAAI,KAAK,yBAAS,IAAI,MAAM,uBAAuB,CAAC;KACpD,IAAI,MAAM,EAAE;KACZ,IAAI,IAAI;KACR;IACJ;IAEA,MAAM,UAAU,KAAK,cAAc,UAAU,MAAM;IACnD,KAAK,MAAM,SAAS,aAAa,OAAO,GAEpC,IAAI,MAAM,KAAK;IAEnB,IAAI,MAAM,EAAE;IACZ,IAAI,IAAI;GACZ,SAAS,GACT;IACI,IAAI,KAAK,SAAS,CAAC;IACnB,IAAI,MAAM,EAAE;IACZ,IAAI,IAAI;GACZ;EACJ,EAAA,CAAG;EAEH,OAAO;CACX;AACJ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mappies/miniwat-provider-mock",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Mock provider for Miniwat.",
|
|
5
|
+
"homepage": "https://gitlab.com/mappies/miniwat-provider-mock#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://gitlab.com/mappies/miniwat-provider-mock/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+ssh://git@gitlab.com/mappies/miniwat-provider-mock.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "PolyForm-Noncommercial-1.0.0",
|
|
14
|
+
"author": "Nithiwat Kampanya",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "vitest run",
|
|
17
|
+
"test-watch": "vitest",
|
|
18
|
+
"build": "tsdown"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.cjs",
|
|
22
|
+
"module": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
|
+
"default": "./dist/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./dist/index.d.cts",
|
|
32
|
+
"default": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^25",
|
|
41
|
+
"tsdown": "^0.23.0",
|
|
42
|
+
"typescript": "^5.9",
|
|
43
|
+
"vitest": "^5"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@mappies/miniwat-agent-core": "^0.17.0",
|
|
47
|
+
"@mappies/miniwat-mcp-core": "^0.2.0"
|
|
48
|
+
}
|
|
49
|
+
}
|