@meridian-flow/runtime 0.0.0-stage → 0.1.1
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 +202 -0
- package/README.md +67 -2
- package/dist/gateway/adapters/anthropic/adapter.d.ts +9 -0
- package/dist/gateway/adapters/anthropic/adapter.js +52 -0
- package/dist/gateway/adapters/anthropic/request-map.d.ts +46 -0
- package/dist/gateway/adapters/anthropic/request-map.js +285 -0
- package/dist/gateway/adapters/anthropic/stream-collect.d.ts +67 -0
- package/dist/gateway/adapters/anthropic/stream-collect.js +295 -0
- package/dist/gateway/adapters/anthropic/tool-schema.d.ts +8 -0
- package/dist/gateway/adapters/anthropic/tool-schema.js +68 -0
- package/dist/gateway/adapters/credentials.d.ts +25 -0
- package/dist/gateway/adapters/credentials.js +118 -0
- package/dist/gateway/adapters/gemini/adapter.d.ts +10 -0
- package/dist/gateway/adapters/gemini/adapter.js +90 -0
- package/dist/gateway/adapters/gemini/request-map.d.ts +29 -0
- package/dist/gateway/adapters/gemini/request-map.js +178 -0
- package/dist/gateway/adapters/gemini/stream-collect.d.ts +29 -0
- package/dist/gateway/adapters/gemini/stream-collect.js +116 -0
- package/dist/gateway/adapters/mock/response.d.ts +11 -0
- package/dist/gateway/adapters/mock/response.js +160 -0
- package/dist/gateway/adapters/mock/samples.d.ts +8 -0
- package/dist/gateway/adapters/mock/samples.js +133 -0
- package/dist/gateway/adapters/mock/script-queue.d.ts +10 -0
- package/dist/gateway/adapters/mock/script-queue.js +60 -0
- package/dist/gateway/adapters/mock/server.d.ts +16 -0
- package/dist/gateway/adapters/mock/server.js +342 -0
- package/dist/gateway/adapters/mock/write-directive.d.ts +15 -0
- package/dist/gateway/adapters/mock/write-directive.js +21 -0
- package/dist/gateway/adapters/openai/adapter.d.ts +3 -0
- package/dist/gateway/adapters/openai/adapter.js +35 -0
- package/dist/gateway/adapters/openai/request-map.d.ts +31 -0
- package/dist/gateway/adapters/openai/request-map.js +228 -0
- package/dist/gateway/adapters/openai/stream-collect.d.ts +63 -0
- package/dist/gateway/adapters/openai/stream-collect.js +490 -0
- package/dist/gateway/adapters/openai-compatible/adapter.d.ts +3 -0
- package/dist/gateway/adapters/openai-compatible/adapter.js +35 -0
- package/dist/gateway/adapters/openai-compatible/request-map.d.ts +31 -0
- package/dist/gateway/adapters/openai-compatible/request-map.js +180 -0
- package/dist/gateway/adapters/openai-compatible/stream-collect.d.ts +84 -0
- package/dist/gateway/adapters/openai-compatible/stream-collect.js +215 -0
- package/dist/gateway/adapters/provider-http-error.d.ts +65 -0
- package/dist/gateway/adapters/provider-http-error.js +188 -0
- package/dist/gateway/adapters/provider-stream.d.ts +23 -0
- package/dist/gateway/adapters/provider-stream.js +58 -0
- package/dist/gateway/attempt-stream.d.ts +27 -0
- package/dist/gateway/attempt-stream.js +329 -0
- package/dist/gateway/create-gateway.d.ts +25 -0
- package/dist/gateway/create-gateway.js +88 -0
- package/dist/gateway/deadline.d.ts +56 -0
- package/dist/gateway/deadline.js +120 -0
- package/dist/gateway/domain/index.d.ts +10 -0
- package/dist/gateway/domain/index.js +10 -0
- package/dist/gateway/domain/media.d.ts +62 -0
- package/dist/gateway/domain/media.js +126 -0
- package/dist/gateway/domain/metering.d.ts +7 -0
- package/dist/gateway/domain/metering.js +6 -0
- package/dist/gateway/domain/resolve-call.d.ts +24 -0
- package/dist/gateway/domain/resolve-call.js +39 -0
- package/dist/gateway/domain/types.d.ts +457 -0
- package/dist/gateway/domain/types.js +10 -0
- package/dist/gateway/helpers/messages.d.ts +17 -0
- package/dist/gateway/helpers/messages.js +29 -0
- package/dist/gateway/helpers/parse-tool-arguments.d.ts +9 -0
- package/dist/gateway/helpers/parse-tool-arguments.js +38 -0
- package/dist/gateway/helpers/tool-call-pairing.d.ts +13 -0
- package/dist/gateway/helpers/tool-call-pairing.js +25 -0
- package/dist/gateway/index.d.ts +12 -0
- package/dist/gateway/index.js +11 -0
- package/dist/gateway/mock-script.d.ts +129 -0
- package/dist/gateway/mock-script.js +129 -0
- package/dist/gateway/mock.d.ts +11 -0
- package/dist/gateway/mock.js +22 -0
- package/dist/gateway/ports/gateway.d.ts +18 -0
- package/dist/gateway/ports/gateway.js +1 -0
- package/dist/gateway/ports/provider-adapter.d.ts +21 -0
- package/dist/gateway/ports/provider-adapter.js +1 -0
- package/dist/gateway/routing.d.ts +28 -0
- package/dist/gateway/routing.js +48 -0
- package/dist/gateway/shapes/index.d.ts +7 -0
- package/dist/gateway/shapes/index.js +1 -0
- package/dist/gateway/shapes/json.d.ts +4 -0
- package/dist/gateway/shapes/json.js +1 -0
- package/dist/gateway/shapes/provider-error.d.ts +10 -0
- package/dist/gateway/shapes/provider-error.js +1 -0
- package/dist/gateway/shapes/usage.d.ts +41 -0
- package/dist/gateway/shapes/usage.js +29 -0
- package/dist/gateway/stream-events.d.ts +21 -0
- package/dist/gateway/stream-events.js +26 -0
- package/package.json +76 -3
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
export const MOCK_SAMPLES = {
|
|
2
|
+
scene: {
|
|
3
|
+
reasoning: "The scene needs a concrete objective, an obstacle, and a choice that costs something. I'll keep the viewpoint close to Mei, reveal the cultivation rule through action, and end on a consequence rather than an explanation.",
|
|
4
|
+
text: `## The bell beneath the mountain
|
|
5
|
+
|
|
6
|
+
Mei reached the ninth stair before the bell rang beneath her feet.
|
|
7
|
+
|
|
8
|
+
Not above her, where the temple hung between two cliffs. Beneath her. The sound travelled up through the jade soles of her borrowed shoes and settled behind her teeth.
|
|
9
|
+
|
|
10
|
+
She stopped breathing. The old gatekeeper had been very specific about that part.
|
|
11
|
+
|
|
12
|
+
*When the mountain speaks, do not answer it.*
|
|
13
|
+
|
|
14
|
+
Three stairs ahead, Jun turned. His left sleeve was dark with rain, his right with something thicker. He pointed toward the gate without looking at the stone beneath her.
|
|
15
|
+
|
|
16
|
+
"Keep moving."
|
|
17
|
+
|
|
18
|
+
Mei shook her head.
|
|
19
|
+
|
|
20
|
+
A hairline crack opened across the stair. Through it shone the warm orange light of a kitchen fire. She smelled ginger, then scorched rice. Her mother's voice called her childhood name.
|
|
21
|
+
|
|
22
|
+
Jun came back down one step.
|
|
23
|
+
|
|
24
|
+
"That isn't her."
|
|
25
|
+
|
|
26
|
+
Of course it wasn't. Her mother had never burned rice.
|
|
27
|
+
|
|
28
|
+
Mei pressed two fingers against the copper coin at her throat. One breath of qi remained inside it. Enough to cross the gate. Enough to hold the crack shut while Jun crossed instead.
|
|
29
|
+
|
|
30
|
+
The mountain called again, this time in Jun's voice.
|
|
31
|
+
|
|
32
|
+
She broke the coin.
|
|
33
|
+
|
|
34
|
+
Light ran into the stair. The crack closed with a sound like a cup being set carefully on a table. Above her, the temple gate opened, and Jun stumbled through.
|
|
35
|
+
|
|
36
|
+
For the first time in seven hundred years, the mountain fell silent.
|
|
37
|
+
|
|
38
|
+
Then something on the other side knocked.`,
|
|
39
|
+
},
|
|
40
|
+
critique: {
|
|
41
|
+
reasoning: "I'll separate the scene's working tension from the revision opportunities. The useful feedback should point to a specific sentence or beat, explain its effect, and offer a small revision without flattening the voice.",
|
|
42
|
+
text: `## What is working
|
|
43
|
+
|
|
44
|
+
The borrowed shoes and the almost-empty copper coin establish Mei's position without a paragraph of backstory. Her final choice uses something the reader already understands.
|
|
45
|
+
|
|
46
|
+
The strongest detail is **the scorched rice**. It lets Mei recognize the deception herself, so Jun's warning does not solve the problem for her.
|
|
47
|
+
|
|
48
|
+
## Two revisions to try
|
|
49
|
+
|
|
50
|
+
1. **Make the deadline visible.** Let rainwater carry blood from Jun's sleeve toward the crack. Mei can watch the distance shrink while she decides.
|
|
51
|
+
2. **Give the sacrifice an immediate physical cost.** After she breaks the coin, her borrowed shoes might become too heavy to lift. That makes the lost qi tangible before the next chapter explains it.
|
|
52
|
+
|
|
53
|
+
### A tighter exchange
|
|
54
|
+
|
|
55
|
+
> "That isn't her."
|
|
56
|
+
>
|
|
57
|
+
> Mei knew. Her mother had never burned rice.
|
|
58
|
+
>
|
|
59
|
+
> Knowing did not make the voice easier to leave.
|
|
60
|
+
|
|
61
|
+
Keep the final knock. It turns a successful rescue into a new problem without undoing Mei's choice.`,
|
|
62
|
+
},
|
|
63
|
+
outline: {
|
|
64
|
+
reasoning: "Each chapter should change what Mei can do or what she believes, not merely postpone the same confrontation. I'll connect the progression reward to the cost of her earlier choice and give the supporting character an independent objective.",
|
|
65
|
+
text: `## Three-chapter progression arc
|
|
66
|
+
|
|
67
|
+
### Chapter 1: A debt in copper
|
|
68
|
+
|
|
69
|
+
- **Goal:** Enter the outer sect before the examination closes.
|
|
70
|
+
- **Obstacle:** The mountain offers a shortcut in a familiar voice.
|
|
71
|
+
- **Choice:** Mei spends her last stored breath rescuing Jun.
|
|
72
|
+
- **Turn:** She misses admission but becomes the only person who can hear the sealed door.
|
|
73
|
+
|
|
74
|
+
### Chapter 2: The silent technique
|
|
75
|
+
|
|
76
|
+
- **Goal:** Earn a place without passing the qi measurement.
|
|
77
|
+
- **Obstacle:** Every conventional technique makes the knocking louder.
|
|
78
|
+
- **Discovery:** Her empty coin can absorb a sound instead of storing a breath.
|
|
79
|
+
- **Cost:** Each use erases a small auditory memory. She records her mother's song before practising again.
|
|
80
|
+
|
|
81
|
+
### Chapter 3: Someone answers
|
|
82
|
+
|
|
83
|
+
- **Goal:** Stop a senior disciple from opening the door.
|
|
84
|
+
- **Complication:** Jun wants it opened because his brother is on the other side.
|
|
85
|
+
- **Payoff:** Mei uses the examination bell to interrupt the ritual, demonstrating her technique before the whole sect.
|
|
86
|
+
- **Hook:** The voice behind the door knows a verse of the song she never wrote down.
|
|
87
|
+
|
|
88
|
+
## Continuity checks
|
|
89
|
+
|
|
90
|
+
- The copper coin breaks before the technique is discovered.
|
|
91
|
+
- Jun owes Mei his life but does not owe her agreement.
|
|
92
|
+
- Power solves the immediate problem and creates a cost that survives the chapter.`,
|
|
93
|
+
},
|
|
94
|
+
markdown: {
|
|
95
|
+
reasoning: "This sample exercises common answer formatting, nested lists, quotations, code fences, links, and multilingual text. These are rendered text formats, not additional provider block types.",
|
|
96
|
+
text: `# Scene planning notes
|
|
97
|
+
|
|
98
|
+
A **clear objective** gives the action direction. An *unspoken fear* gives the same action a second meaning.
|
|
99
|
+
|
|
100
|
+
## Revision checklist
|
|
101
|
+
|
|
102
|
+
- Establish the viewpoint character's immediate goal.
|
|
103
|
+
- Name what they can touch or change.
|
|
104
|
+
- Put the obstacle in the same physical space.
|
|
105
|
+
- Track the resource they cannot replace.
|
|
106
|
+
- End after the choice has a consequence.
|
|
107
|
+
|
|
108
|
+
> The gate opened. Mei stayed on the wrong side.
|
|
109
|
+
|
|
110
|
+
### Continuity data
|
|
111
|
+
|
|
112
|
+
\`\`\`json
|
|
113
|
+
{
|
|
114
|
+
"character": "Mei",
|
|
115
|
+
"qiReserves": 0,
|
|
116
|
+
"inventory": ["broken copper coin", "borrowed jade shoes"],
|
|
117
|
+
"promise": "Bring Jun home"
|
|
118
|
+
}
|
|
119
|
+
\`\`\`
|
|
120
|
+
|
|
121
|
+
Use \`qiReserves\` consistently in the outline. A literal <gate> is not an HTML element in the manuscript.
|
|
122
|
+
|
|
123
|
+
### Language and punctuation
|
|
124
|
+
|
|
125
|
+
“别回头。” Mei did not turn around. The inscription read 山门. A margin note contained an emoji: 🐉.
|
|
126
|
+
|
|
127
|
+
For another formatting reference, see the [CommonMark specification](https://spec.commonmark.org/).
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
The next scene begins with a decision, not a recap.`,
|
|
132
|
+
},
|
|
133
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MockModelScript, MockModelScriptEnqueued, MockModelScriptState, MockModelStep } from "../../mock-script.js";
|
|
2
|
+
export interface MockScriptQueue {
|
|
3
|
+
enqueue(script: MockModelScript): MockModelScriptEnqueued;
|
|
4
|
+
/** Next step for a call whose latest user message is `userText`, or null to fall through. */
|
|
5
|
+
take(userText: string): MockModelStep | null;
|
|
6
|
+
/** Removes one script, or every script when `id` is omitted. */
|
|
7
|
+
clear(id?: string): MockModelScriptState;
|
|
8
|
+
state(): MockModelScriptState;
|
|
9
|
+
}
|
|
10
|
+
export declare function createMockScriptQueue(): MockScriptQueue;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scripted replies for the in-process mock model. Each queued step answers `times`
|
|
3
|
+
* model calls (default 1); an `error` step without `times` is sticky so retries and
|
|
4
|
+
* fallbacks cannot slip past it. Scripts may be scoped to calls whose latest user
|
|
5
|
+
* message contains `match`.
|
|
6
|
+
*/
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
function budget(step) {
|
|
9
|
+
if (step.times !== undefined)
|
|
10
|
+
return step.times;
|
|
11
|
+
return step.error ? Number.POSITIVE_INFINITY : 1;
|
|
12
|
+
}
|
|
13
|
+
export function createMockScriptQueue() {
|
|
14
|
+
const scripts = [];
|
|
15
|
+
const state = () => ({
|
|
16
|
+
scripts: scripts.map((script) => ({
|
|
17
|
+
id: script.id,
|
|
18
|
+
match: script.match,
|
|
19
|
+
remaining: script.steps.length,
|
|
20
|
+
sticky: script.steps[0] ? budget(script.steps[0]) === Number.POSITIVE_INFINITY : false,
|
|
21
|
+
})),
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
enqueue(script) {
|
|
25
|
+
const id = randomUUID();
|
|
26
|
+
scripts.push({ id, match: script.match ?? null, steps: [...script.steps], uses: 0 });
|
|
27
|
+
return { id, ...state() };
|
|
28
|
+
},
|
|
29
|
+
take(userText) {
|
|
30
|
+
// Scoped scripts win over unscoped ones so a targeted send is never pre-empted.
|
|
31
|
+
const scoped = scripts.findIndex((script) => script.match !== null && userText.includes(script.match));
|
|
32
|
+
const index = scoped >= 0 ? scoped : scripts.findIndex((script) => script.match === null);
|
|
33
|
+
if (index < 0)
|
|
34
|
+
return null;
|
|
35
|
+
const script = scripts[index];
|
|
36
|
+
const step = script.steps[0];
|
|
37
|
+
if (!step)
|
|
38
|
+
return null;
|
|
39
|
+
script.uses += 1;
|
|
40
|
+
if (script.uses >= budget(step)) {
|
|
41
|
+
script.steps.shift();
|
|
42
|
+
script.uses = 0;
|
|
43
|
+
if (script.steps.length === 0)
|
|
44
|
+
scripts.splice(index, 1);
|
|
45
|
+
}
|
|
46
|
+
return step;
|
|
47
|
+
},
|
|
48
|
+
clear(id) {
|
|
49
|
+
if (id === undefined)
|
|
50
|
+
scripts.splice(0);
|
|
51
|
+
else {
|
|
52
|
+
const index = scripts.findIndex((script) => script.id === id);
|
|
53
|
+
if (index >= 0)
|
|
54
|
+
scripts.splice(index, 1);
|
|
55
|
+
}
|
|
56
|
+
return state();
|
|
57
|
+
},
|
|
58
|
+
state,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { MockModelDefaultResponse } from "../../mock-script.js";
|
|
2
|
+
import type { MockScriptQueue } from "./script-queue.js";
|
|
3
|
+
export interface MockOpenAIServer {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
port: number;
|
|
6
|
+
requestCount: () => number;
|
|
7
|
+
close: () => Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* In-process OpenAI-compatible mock for `/v1/chat/completions` (SSE).
|
|
11
|
+
* Exercises the real openai-compatible adapter pipeline in dev and tests.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createMockOpenAICompatibleServer(options?: {
|
|
14
|
+
script?: MockScriptQueue;
|
|
15
|
+
defaultResponse?: MockModelDefaultResponse;
|
|
16
|
+
}): Promise<MockOpenAIServer>;
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock OpenAI-compatible HTTP server: a local Chat Completions endpoint (stream
|
|
3
|
+
* + non-stream) for tests and offline dev so the gateway can run without a real
|
|
4
|
+
* provider. Owns the canned-response server and request-derived mock usage.
|
|
5
|
+
*
|
|
6
|
+
* Write tool scripting (optional, for gate probes — does not change default
|
|
7
|
+
* behavior unless present in the user message that triggers a write):
|
|
8
|
+
* `[[write <uri>]]` — target `<uri>` instead of `manuscript://Chapter 1`
|
|
9
|
+
* `[[write <uri> overwrite]]` — same with `overwrite: true` on create
|
|
10
|
+
* A user message must include the mock's write trigger before these directives apply.
|
|
11
|
+
*
|
|
12
|
+
* Scripted replies (dev CLI `./mf mock script` / `send --mock`): when a
|
|
13
|
+
* `MockScriptQueue` is supplied, a queued step answers the call before any of
|
|
14
|
+
* the trigger-phrase behaviors below.
|
|
15
|
+
*/
|
|
16
|
+
import { createServer } from "node:http";
|
|
17
|
+
import { writeMockResponse } from "./response.js";
|
|
18
|
+
import { parseWriteDirective } from "./write-directive.js";
|
|
19
|
+
function readJsonBody(req) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const chunks = [];
|
|
22
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
23
|
+
req.on("end", () => {
|
|
24
|
+
try {
|
|
25
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
26
|
+
resolve(raw ? JSON.parse(raw) : {});
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
reject(err);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
req.on("error", reject);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
// Match the planner's three-byte heuristic on the wire input, not transport options.
|
|
36
|
+
// This is synthetic usage, but must grow with history so compaction probes stay reachable.
|
|
37
|
+
function inputTokens(body) {
|
|
38
|
+
return Math.ceil(Buffer.byteLength(JSON.stringify({
|
|
39
|
+
messages: body.messages ?? [],
|
|
40
|
+
tools: body.tools ?? [],
|
|
41
|
+
responseFormat: body.response_format ?? null,
|
|
42
|
+
}), "utf8") / 3);
|
|
43
|
+
}
|
|
44
|
+
function sseLine(payload) {
|
|
45
|
+
return `data: ${JSON.stringify(payload)}\n\n`;
|
|
46
|
+
}
|
|
47
|
+
const VERTICAL_SLICE_TRIGGER = "Phase 7 final gate";
|
|
48
|
+
const VERTICAL_SLICE_WRITE_PATH = "manuscript://Chapter 1";
|
|
49
|
+
function messageContentText(message) {
|
|
50
|
+
const content = message?.content;
|
|
51
|
+
if (typeof content === "string")
|
|
52
|
+
return content;
|
|
53
|
+
if (Array.isArray(content)) {
|
|
54
|
+
return content
|
|
55
|
+
.map((part) => {
|
|
56
|
+
if (typeof part === "object" && part !== null && "text" in part) {
|
|
57
|
+
return String(part.text ?? "");
|
|
58
|
+
}
|
|
59
|
+
return "";
|
|
60
|
+
})
|
|
61
|
+
.join("\n");
|
|
62
|
+
}
|
|
63
|
+
return "";
|
|
64
|
+
}
|
|
65
|
+
function messageText(messages) {
|
|
66
|
+
if (!messages?.length)
|
|
67
|
+
return "";
|
|
68
|
+
return messageContentText(messages[messages.length - 1]);
|
|
69
|
+
}
|
|
70
|
+
function lastUserMessageText(messages) {
|
|
71
|
+
if (!messages?.length)
|
|
72
|
+
return "";
|
|
73
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
74
|
+
if (messages[i]?.role === "user") {
|
|
75
|
+
return messageContentText(messages[i]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return "";
|
|
79
|
+
}
|
|
80
|
+
function lastMessageRole(messages) {
|
|
81
|
+
if (!messages?.length)
|
|
82
|
+
return undefined;
|
|
83
|
+
return messages[messages.length - 1]?.role;
|
|
84
|
+
}
|
|
85
|
+
function mockAssistantText(messages) {
|
|
86
|
+
const userText = lastUserMessageText(messages);
|
|
87
|
+
if (!userText)
|
|
88
|
+
return "Acknowledged: (empty)";
|
|
89
|
+
return `Acknowledged: ${userText}`;
|
|
90
|
+
}
|
|
91
|
+
function hasFunctionTool(body, name) {
|
|
92
|
+
return body.tools?.some((tool) => tool.function?.name === name) ?? false;
|
|
93
|
+
}
|
|
94
|
+
function wantsWriteToolCall(body) {
|
|
95
|
+
if (!hasFunctionTool(body, "write"))
|
|
96
|
+
return false;
|
|
97
|
+
const userText = lastUserMessageText(body.messages);
|
|
98
|
+
if (!userText.includes(VERTICAL_SLICE_TRIGGER))
|
|
99
|
+
return false;
|
|
100
|
+
return lastMessageRole(body.messages) !== "tool";
|
|
101
|
+
}
|
|
102
|
+
function wantsBackgroundWriterHelperSpawn(body) {
|
|
103
|
+
if (!hasFunctionTool(body, "spawn"))
|
|
104
|
+
return false;
|
|
105
|
+
if (lastMessageRole(body.messages) === "tool")
|
|
106
|
+
return false;
|
|
107
|
+
const userText = lastUserMessageText(body.messages).toLowerCase();
|
|
108
|
+
return userText.includes("background") && userText.includes("writer-helper");
|
|
109
|
+
}
|
|
110
|
+
function wantsReturnResult(body) {
|
|
111
|
+
if (!hasFunctionTool(body, "return_result"))
|
|
112
|
+
return false;
|
|
113
|
+
if (lastMessageRole(body.messages) === "tool")
|
|
114
|
+
return false;
|
|
115
|
+
return lastUserMessageText(body.messages).length > 0;
|
|
116
|
+
}
|
|
117
|
+
function wantsToolCall(body) {
|
|
118
|
+
if (!hasFunctionTool(body, "get_weather") || lastMessageRole(body.messages) === "tool")
|
|
119
|
+
return false;
|
|
120
|
+
const text = lastUserMessageText(body.messages).toLowerCase();
|
|
121
|
+
return text.includes("weather") || text.includes("tool");
|
|
122
|
+
}
|
|
123
|
+
function verticalSliceWriteContent(userText) {
|
|
124
|
+
return `# Chapter 1\n\nAcknowledged: ${userText}`;
|
|
125
|
+
}
|
|
126
|
+
function writeSlowAfterFirstChunk(res, id, model) {
|
|
127
|
+
res.writeHead(200, {
|
|
128
|
+
"Content-Type": "text/event-stream",
|
|
129
|
+
"Cache-Control": "no-cache",
|
|
130
|
+
Connection: "keep-alive",
|
|
131
|
+
});
|
|
132
|
+
res.write(sseLine({
|
|
133
|
+
id,
|
|
134
|
+
object: "chat.completion.chunk",
|
|
135
|
+
model,
|
|
136
|
+
choices: [{ index: 0, delta: { content: "partial" }, finish_reason: null }],
|
|
137
|
+
}));
|
|
138
|
+
const timer = setTimeout(() => {
|
|
139
|
+
if (res.destroyed)
|
|
140
|
+
return;
|
|
141
|
+
res.write("data: [DONE]\n\n");
|
|
142
|
+
res.end();
|
|
143
|
+
}, 200);
|
|
144
|
+
timer.unref();
|
|
145
|
+
}
|
|
146
|
+
/** Streams partial output then ignores abort — a stop must still end the stream at once. */
|
|
147
|
+
function writeHangCancelDrainStream(res, id, model) {
|
|
148
|
+
res.writeHead(200, {
|
|
149
|
+
"Content-Type": "text/event-stream",
|
|
150
|
+
"Cache-Control": "no-cache",
|
|
151
|
+
Connection: "keep-alive",
|
|
152
|
+
});
|
|
153
|
+
res.write(sseLine({
|
|
154
|
+
id,
|
|
155
|
+
object: "chat.completion.chunk",
|
|
156
|
+
model,
|
|
157
|
+
choices: [{ index: 0, delta: { content: "partial" }, finish_reason: null }],
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
/** Streams partial output + usage, then stalls until the client aborts the request. */
|
|
161
|
+
function writeCancelBillingStream(req, res, id, model) {
|
|
162
|
+
res.writeHead(200, {
|
|
163
|
+
"Content-Type": "text/event-stream",
|
|
164
|
+
"Cache-Control": "no-cache",
|
|
165
|
+
Connection: "keep-alive",
|
|
166
|
+
});
|
|
167
|
+
res.write(sseLine({
|
|
168
|
+
id,
|
|
169
|
+
object: "chat.completion.chunk",
|
|
170
|
+
model,
|
|
171
|
+
choices: [{ index: 0, delta: { content: "partial" }, finish_reason: null }],
|
|
172
|
+
}));
|
|
173
|
+
res.write(sseLine({
|
|
174
|
+
id,
|
|
175
|
+
object: "chat.completion.chunk",
|
|
176
|
+
model,
|
|
177
|
+
choices: [{ index: 0, delta: {}, finish_reason: null }],
|
|
178
|
+
usage: {
|
|
179
|
+
prompt_tokens: 500_000,
|
|
180
|
+
completion_tokens: 500_000,
|
|
181
|
+
total_tokens: 1_000_000,
|
|
182
|
+
},
|
|
183
|
+
}));
|
|
184
|
+
const finish = () => {
|
|
185
|
+
if (res.destroyed || res.writableEnded)
|
|
186
|
+
return;
|
|
187
|
+
res.end();
|
|
188
|
+
};
|
|
189
|
+
req.on("aborted", finish);
|
|
190
|
+
req.on("close", finish);
|
|
191
|
+
}
|
|
192
|
+
function selectResponse(body) {
|
|
193
|
+
const userText = lastUserMessageText(body.messages);
|
|
194
|
+
if (wantsBackgroundWriterHelperSpawn(body)) {
|
|
195
|
+
return {
|
|
196
|
+
toolCalls: [
|
|
197
|
+
{
|
|
198
|
+
name: "spawn",
|
|
199
|
+
args: {
|
|
200
|
+
agent: "writer-helper",
|
|
201
|
+
mode: "background",
|
|
202
|
+
name: "Writer helper background check",
|
|
203
|
+
prompt: `Check this request independently and return a concise writing-craft note: ${userText}`,
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
if (wantsReturnResult(body)) {
|
|
210
|
+
return {
|
|
211
|
+
toolCalls: [
|
|
212
|
+
{
|
|
213
|
+
name: "return_result",
|
|
214
|
+
args: {
|
|
215
|
+
summary: "Writer-helper checked the request and recommends keeping the next beat concrete.",
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (wantsWriteToolCall(body)) {
|
|
222
|
+
const directive = parseWriteDirective(userText);
|
|
223
|
+
return {
|
|
224
|
+
toolCalls: [
|
|
225
|
+
{
|
|
226
|
+
name: "write",
|
|
227
|
+
args: {
|
|
228
|
+
command: "create",
|
|
229
|
+
path: directive?.path ?? VERTICAL_SLICE_WRITE_PATH,
|
|
230
|
+
content: verticalSliceWriteContent(userText),
|
|
231
|
+
...(directive?.overwrite ? { overwrite: true } : {}),
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
if (wantsToolCall(body)) {
|
|
238
|
+
return { toolCalls: [{ name: "get_weather", args: { location: "San Francisco" } }] };
|
|
239
|
+
}
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* In-process OpenAI-compatible mock for `/v1/chat/completions` (SSE).
|
|
244
|
+
* Exercises the real openai-compatible adapter pipeline in dev and tests.
|
|
245
|
+
*/
|
|
246
|
+
export function createMockOpenAICompatibleServer(options) {
|
|
247
|
+
return new Promise((resolve, reject) => {
|
|
248
|
+
let requestCount = 0;
|
|
249
|
+
const server = createServer(async (req, res) => {
|
|
250
|
+
if (req.method === "GET" && req.url === "/health") {
|
|
251
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
252
|
+
res.end(JSON.stringify({ ok: true }));
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (req.method !== "POST" || req.url !== "/v1/chat/completions") {
|
|
256
|
+
res.writeHead(404);
|
|
257
|
+
res.end();
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const requestNumber = ++requestCount;
|
|
261
|
+
try {
|
|
262
|
+
const body = (await readJsonBody(req));
|
|
263
|
+
const promptTokens = inputTokens(body);
|
|
264
|
+
const id = `chatcmpl-mock-${requestNumber}`;
|
|
265
|
+
const model = body.model ?? "mock-llm-v1";
|
|
266
|
+
const scripted = options?.script?.take(lastUserMessageText(body.messages));
|
|
267
|
+
if (scripted) {
|
|
268
|
+
writeMockResponse(res, {
|
|
269
|
+
id,
|
|
270
|
+
model,
|
|
271
|
+
stream: body.stream === true,
|
|
272
|
+
step: scripted,
|
|
273
|
+
requestCount: requestNumber,
|
|
274
|
+
promptTokens,
|
|
275
|
+
});
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (body.stream) {
|
|
279
|
+
if (messageText(body.messages).includes("hang cancel drain")) {
|
|
280
|
+
writeHangCancelDrainStream(res, id, model);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (messageText(body.messages).includes("cancel billing")) {
|
|
284
|
+
writeCancelBillingStream(req, res, id, model);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (messageText(body.messages).includes("midstream disconnect")) {
|
|
288
|
+
res.writeHead(200, {
|
|
289
|
+
"Content-Type": "text/event-stream",
|
|
290
|
+
"Cache-Control": "no-cache",
|
|
291
|
+
Connection: "keep-alive",
|
|
292
|
+
});
|
|
293
|
+
res.write(sseLine({
|
|
294
|
+
id,
|
|
295
|
+
object: "chat.completion.chunk",
|
|
296
|
+
model,
|
|
297
|
+
choices: [{ index: 0, delta: { content: "partial" }, finish_reason: null }],
|
|
298
|
+
}));
|
|
299
|
+
res.write("data: {not-json}\n\n");
|
|
300
|
+
res.end();
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (messageText(body.messages).includes("slow after output")) {
|
|
304
|
+
writeSlowAfterFirstChunk(res, id, model);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const step = body.stream && messageText(body.messages).includes("slow before output")
|
|
309
|
+
? { text: mockAssistantText(body.messages), delayMs: 200 }
|
|
310
|
+
: (selectResponse(body) ?? options?.defaultResponse ?? { sample: "scene" });
|
|
311
|
+
writeMockResponse(res, {
|
|
312
|
+
id,
|
|
313
|
+
model,
|
|
314
|
+
stream: body.stream === true,
|
|
315
|
+
step,
|
|
316
|
+
requestCount: requestNumber,
|
|
317
|
+
promptTokens,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
catch (err) {
|
|
321
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
322
|
+
res.end(JSON.stringify({ error: { message: String(err) } }));
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
server.on("error", reject);
|
|
326
|
+
server.listen(0, "127.0.0.1", () => {
|
|
327
|
+
const addr = server.address();
|
|
328
|
+
if (!addr || typeof addr === "string") {
|
|
329
|
+
reject(new Error("mock server failed to bind"));
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
resolve({
|
|
333
|
+
port: addr.port,
|
|
334
|
+
baseUrl: `http://127.0.0.1:${addr.port}/v1`,
|
|
335
|
+
requestCount: () => requestCount,
|
|
336
|
+
close: () => new Promise((closeResolve, closeReject) => {
|
|
337
|
+
server.close((err) => (err ? closeReject(err) : closeResolve()));
|
|
338
|
+
}),
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional user-message directives for the mock gateway write tool.
|
|
3
|
+
*
|
|
4
|
+
* Syntax (case-insensitive `write`, URI is any non-whitespace token):
|
|
5
|
+
* `[[write <uri>]]` — create at `<uri>` (default mock path when absent)
|
|
6
|
+
* `[[write <uri> overwrite]]` — create with `overwrite: true`
|
|
7
|
+
*
|
|
8
|
+
* Directives apply only when the message triggers a mock write tool call.
|
|
9
|
+
* Without a directive, writes target `manuscript://Chapter 1` without overwrite.
|
|
10
|
+
*/
|
|
11
|
+
export type ParsedWriteDirective = {
|
|
12
|
+
path: string;
|
|
13
|
+
overwrite: boolean;
|
|
14
|
+
};
|
|
15
|
+
export declare function parseWriteDirective(text: string): ParsedWriteDirective | null;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional user-message directives for the mock gateway write tool.
|
|
3
|
+
*
|
|
4
|
+
* Syntax (case-insensitive `write`, URI is any non-whitespace token):
|
|
5
|
+
* `[[write <uri>]]` — create at `<uri>` (default mock path when absent)
|
|
6
|
+
* `[[write <uri> overwrite]]` — create with `overwrite: true`
|
|
7
|
+
*
|
|
8
|
+
* Directives apply only when the message triggers a mock write tool call.
|
|
9
|
+
* Without a directive, writes target `manuscript://Chapter 1` without overwrite.
|
|
10
|
+
*/
|
|
11
|
+
const WRITE_DIRECTIVE_RE = /\[\[write\s+(\S+?)(?:\s+overwrite)?\s*\]\]/i;
|
|
12
|
+
export function parseWriteDirective(text) {
|
|
13
|
+
const match = WRITE_DIRECTIVE_RE.exec(text);
|
|
14
|
+
if (!match)
|
|
15
|
+
return null;
|
|
16
|
+
const token = match[1]?.trim() ?? "";
|
|
17
|
+
if (!token)
|
|
18
|
+
return null;
|
|
19
|
+
const overwrite = /\s+overwrite\s*\]\]$/i.test(match[0]);
|
|
20
|
+
return { path: token, overwrite };
|
|
21
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Responses provider adapter: implements the provider-adapter port over
|
|
3
|
+
* the OpenAI Responses API, translating to/from canonical gateway types. Owns
|
|
4
|
+
* the OpenAI-specific streaming wiring. The shared stream envelope is
|
|
5
|
+
* `../provider-stream.ts`.
|
|
6
|
+
*/
|
|
7
|
+
import OpenAI from "openai";
|
|
8
|
+
import { bearerClientOptions, clientForRequest } from "../credentials.js";
|
|
9
|
+
import { OPENAI_ENV_OPTOUT, providerClientOptions, providerFetch } from "../provider-http-error.js";
|
|
10
|
+
import { streamProvider } from "../provider-stream.js";
|
|
11
|
+
import { toOpenAIResponsesParams } from "./request-map.js";
|
|
12
|
+
import { buildGenerateResult, createStreamAccumulator, eventsFromResponseStreamEvent, } from "./stream-collect.js";
|
|
13
|
+
export function createOpenAIResponsesAdapter(config) {
|
|
14
|
+
const own = new OpenAI({
|
|
15
|
+
...providerClientOptions(config, "https://api.openai.com/v1"),
|
|
16
|
+
...OPENAI_ENV_OPTOUT,
|
|
17
|
+
...bearerClientOptions(config.auth, providerFetch),
|
|
18
|
+
});
|
|
19
|
+
const clientFor = clientForRequest(config, own, (credential) => own.withOptions(bearerClientOptions(credential, providerFetch)));
|
|
20
|
+
const providerId = config.id;
|
|
21
|
+
return {
|
|
22
|
+
stream(request, model, call) {
|
|
23
|
+
const acc = createStreamAccumulator(model.id, providerId, call.longCacheWrites);
|
|
24
|
+
return streamProvider({
|
|
25
|
+
start: { type: "start", model: model.id, provider: providerId },
|
|
26
|
+
signal: request.signal,
|
|
27
|
+
open: () => clientFor(request).responses.create(toOpenAIResponsesParams(request, model, call), {
|
|
28
|
+
signal: request.signal,
|
|
29
|
+
}),
|
|
30
|
+
events: (event) => eventsFromResponseStreamEvent(event, acc),
|
|
31
|
+
result: () => buildGenerateResult(acc),
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Responses request mapper: converts canonical GenerateRequests into
|
|
3
|
+
* OpenAI Responses request bodies (input, tools, reasoning config, response
|
|
4
|
+
* format). Owns the canonical→OpenAI translation.
|
|
5
|
+
*
|
|
6
|
+
* Key decisions:
|
|
7
|
+
* - System messages are extracted into the top-level `instructions` param
|
|
8
|
+
* because the Responses API has no system-message role in its `input` array.
|
|
9
|
+
* - Assistant messages with tool calls are flattened: text parts become a
|
|
10
|
+
* message item, tool_use parts become separate `function_call` items, and
|
|
11
|
+
* reasoning parts with matching provider/model origin become reasoning items.
|
|
12
|
+
* This mirrors the Responses API's input-item model where each output type
|
|
13
|
+
* is a distinct item.
|
|
14
|
+
* - Reasoning replay: encrypted reasoning content is only sent back to the
|
|
15
|
+
* same provider/model pair to avoid cross-provider rejection.
|
|
16
|
+
* - `include: ["reasoning.encrypted_content"]` is always requested so reasoning
|
|
17
|
+
* items can be carried across turns in stateless mode (per OpenAI reasoning
|
|
18
|
+
* guide).
|
|
19
|
+
* - Prompt caching is automatic for prompts >=1024 tokens and needs no
|
|
20
|
+
* explicit breakpoints, so this mapper only forwards `GenerateRequest.
|
|
21
|
+
* promptCacheKey` as `prompt_cache_key` (a stable per-thread routing hint
|
|
22
|
+
* that improves cache-affinity — OpenAI's own docs recommend one per
|
|
23
|
+
* thread/session). A long retention request adds
|
|
24
|
+
* `prompt_cache_options: { ttl: "12h" }`; the short one relies on the 30m
|
|
25
|
+
* default.
|
|
26
|
+
*/
|
|
27
|
+
import type OpenAI from "openai";
|
|
28
|
+
import type { GenerateRequest, ModelInfo, ResolvedCall } from "../../domain/index.js";
|
|
29
|
+
/** Reject, at gateway creation, a model whose cache lifetimes this API cannot be sent. A null lifetime (unknown) sends nothing. */
|
|
30
|
+
export declare function assertCacheTtlsOnWire(model: Pick<ModelInfo, "id" | "promptCache">): void;
|
|
31
|
+
export declare function toOpenAIResponsesParams(request: GenerateRequest, model: ModelInfo, call: ResolvedCall): OpenAI.Responses.ResponseCreateParamsStreaming;
|