@autumnsgrove/lyra 0.1.0 → 0.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/adapters/openai/errors.d.ts +36 -0
- package/dist/adapters/openai/errors.d.ts.map +1 -0
- package/dist/adapters/openai/errors.js +117 -0
- package/dist/adapters/openai/errors.js.map +1 -0
- package/dist/adapters/openai/index.d.ts +18 -0
- package/dist/adapters/openai/index.d.ts.map +1 -0
- package/dist/adapters/openai/index.js +18 -0
- package/dist/adapters/openai/index.js.map +1 -0
- package/dist/adapters/openai/model.d.ts +50 -0
- package/dist/adapters/openai/model.d.ts.map +1 -0
- package/dist/adapters/openai/model.js +169 -0
- package/dist/adapters/openai/model.js.map +1 -0
- package/dist/adapters/openai/presets.d.ts +36 -0
- package/dist/adapters/openai/presets.d.ts.map +1 -0
- package/dist/adapters/openai/presets.js +43 -0
- package/dist/adapters/openai/presets.js.map +1 -0
- package/dist/adapters/openai/request.d.ts +47 -0
- package/dist/adapters/openai/request.d.ts.map +1 -0
- package/dist/adapters/openai/request.js +93 -0
- package/dist/adapters/openai/request.js.map +1 -0
- package/dist/adapters/openai/sse.d.ts +44 -0
- package/dist/adapters/openai/sse.d.ts.map +1 -0
- package/dist/adapters/openai/sse.js +410 -0
- package/dist/adapters/openai/sse.js.map +1 -0
- package/dist/core/system-clock.d.ts.map +1 -1
- package/dist/core/system-clock.js +5 -1
- package/dist/core/system-clock.js.map +1 -1
- package/dist/testkit/cassette.d.ts +91 -0
- package/dist/testkit/cassette.d.ts.map +1 -0
- package/dist/testkit/cassette.js +250 -0
- package/dist/testkit/cassette.js.map +1 -0
- package/dist/testkit/fake-model-server.d.ts +87 -0
- package/dist/testkit/fake-model-server.d.ts.map +1 -0
- package/dist/testkit/fake-model-server.js +225 -0
- package/dist/testkit/fake-model-server.js.map +1 -0
- package/dist/testkit/index.d.ts +4 -1
- package/dist/testkit/index.d.ts.map +1 -1
- package/dist/testkit/index.js +4 -1
- package/dist/testkit/index.js.map +1 -1
- package/dist/testkit/misbehaving-model.d.ts +40 -0
- package/dist/testkit/misbehaving-model.d.ts.map +1 -0
- package/dist/testkit/misbehaving-model.js +134 -0
- package/dist/testkit/misbehaving-model.js.map +1 -0
- package/dist/testkit/model.d.ts +2 -0
- package/dist/testkit/model.d.ts.map +1 -1
- package/dist/testkit/model.js +3 -2
- package/dist/testkit/model.js.map +1 -1
- package/dist/testkit/scenario.d.ts +11 -1
- package/dist/testkit/scenario.d.ts.map +1 -1
- package/dist/testkit/scenario.js +4 -2
- package/dist/testkit/scenario.js.map +1 -1
- package/package.json +5 -1
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a provider's failure becomes a `ModelError` (docs/adapters.md, OAI20 to
|
|
3
|
+
* OAI23).
|
|
4
|
+
*
|
|
5
|
+
* Step 2's retries trust `retryable`, so it has to be right: a transient outage
|
|
6
|
+
* is tried again and then parks the run for a person, and a request the provider
|
|
7
|
+
* will never accept fails it at once. Messages are cut to a length a record can
|
|
8
|
+
* hold, and they never carry a request header, so a key cannot leave this way.
|
|
9
|
+
*/
|
|
10
|
+
import type { ModelError } from "../../core/model.js";
|
|
11
|
+
import type { ErrorCode } from "../../core/result.js";
|
|
12
|
+
/** However long a provider asks for, five minutes is the most it may ask. */
|
|
13
|
+
export declare const MAX_RETRY_AFTER_MS = 300000;
|
|
14
|
+
/** How much of a provider's own text a message may carry. */
|
|
15
|
+
export declare const MAX_MESSAGE_CHARS = 300;
|
|
16
|
+
export declare function isRetryableStatus(status: number): boolean;
|
|
17
|
+
/** The error code a status deserves. Anything unknown is a provider failure. */
|
|
18
|
+
export declare function codeForStatus(status: number): ErrorCode;
|
|
19
|
+
export declare function truncateMessage(text: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* `Retry-After` as a wait in milliseconds: seconds, or an HTTP date, capped and
|
|
22
|
+
* never negative. A value this cannot read is no guidance at all.
|
|
23
|
+
*/
|
|
24
|
+
export declare function retryAfterMs(header: string | null, now: number): number | undefined;
|
|
25
|
+
/** The provider's own words about a failure, when its body is an error object. */
|
|
26
|
+
export declare function errorFromJsonBody(text: string): ModelError | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* A failure that came back with a status. The body is the provider's own
|
|
29
|
+
* explanation when it has one, in JSON or in plain text, and it is the only
|
|
30
|
+
* thing about the response that reaches the message.
|
|
31
|
+
*/
|
|
32
|
+
export declare function errorForStatus(status: number, body: string, options: {
|
|
33
|
+
retryAfter?: string | null;
|
|
34
|
+
now: number;
|
|
35
|
+
}): ModelError;
|
|
36
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAQtD,6EAA6E;AAC7E,eAAO,MAAM,kBAAkB,SAAU,CAAC;AAE1C,6DAA6D;AAC7D,eAAO,MAAM,iBAAiB,MAAM,CAAC;AAErC,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEzD;AAED,gFAAgF;AAChF,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAmBvD;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGpD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAgBnF;AAED,kFAAkF;AAClF,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CA0BtE;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE;IAAE,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAClD,UAAU,CAkBZ"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a provider's failure becomes a `ModelError` (docs/adapters.md, OAI20 to
|
|
3
|
+
* OAI23).
|
|
4
|
+
*
|
|
5
|
+
* Step 2's retries trust `retryable`, so it has to be right: a transient outage
|
|
6
|
+
* is tried again and then parks the run for a person, and a request the provider
|
|
7
|
+
* will never accept fails it at once. Messages are cut to a length a record can
|
|
8
|
+
* hold, and they never carry a request header, so a key cannot leave this way.
|
|
9
|
+
*/
|
|
10
|
+
import { isPlainObject } from "../../core/json.js";
|
|
11
|
+
/** The statuses worth trying again: the provider is busy, not the request wrong. */
|
|
12
|
+
const RETRYABLE_STATUSES = new Set([408, 429, 500, 502, 503, 504]);
|
|
13
|
+
/** A provider's own code, when it says something permanent rather than loading. */
|
|
14
|
+
const PERMANENT_STATUSES = new Set([400, 401, 403, 404, 422]);
|
|
15
|
+
/** However long a provider asks for, five minutes is the most it may ask. */
|
|
16
|
+
export const MAX_RETRY_AFTER_MS = 300_000;
|
|
17
|
+
/** How much of a provider's own text a message may carry. */
|
|
18
|
+
export const MAX_MESSAGE_CHARS = 300;
|
|
19
|
+
export function isRetryableStatus(status) {
|
|
20
|
+
return RETRYABLE_STATUSES.has(status);
|
|
21
|
+
}
|
|
22
|
+
/** The error code a status deserves. Anything unknown is a provider failure. */
|
|
23
|
+
export function codeForStatus(status) {
|
|
24
|
+
switch (status) {
|
|
25
|
+
case 400:
|
|
26
|
+
case 422:
|
|
27
|
+
return "bad_request";
|
|
28
|
+
case 401:
|
|
29
|
+
case 403:
|
|
30
|
+
return "permission_denied";
|
|
31
|
+
case 404:
|
|
32
|
+
return "not_found";
|
|
33
|
+
case 408:
|
|
34
|
+
return "timeout";
|
|
35
|
+
case 409:
|
|
36
|
+
return "conflict";
|
|
37
|
+
case 429:
|
|
38
|
+
return "busy";
|
|
39
|
+
default:
|
|
40
|
+
return "provider_error";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function truncateMessage(text) {
|
|
44
|
+
const trimmed = text.trim();
|
|
45
|
+
return trimmed.length > MAX_MESSAGE_CHARS ? `${trimmed.slice(0, MAX_MESSAGE_CHARS)}…` : trimmed;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* `Retry-After` as a wait in milliseconds: seconds, or an HTTP date, capped and
|
|
49
|
+
* never negative. A value this cannot read is no guidance at all.
|
|
50
|
+
*/
|
|
51
|
+
export function retryAfterMs(header, now) {
|
|
52
|
+
if (header === null) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const value = header.trim();
|
|
56
|
+
if (value === "") {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
if (/^\d+$/.test(value)) {
|
|
60
|
+
return Math.min(Number(value) * 1_000, MAX_RETRY_AFTER_MS);
|
|
61
|
+
}
|
|
62
|
+
const at = Date.parse(value);
|
|
63
|
+
if (Number.isNaN(at)) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
return Math.min(Math.max(at - now, 0), MAX_RETRY_AFTER_MS);
|
|
67
|
+
}
|
|
68
|
+
/** The provider's own words about a failure, when its body is an error object. */
|
|
69
|
+
export function errorFromJsonBody(text) {
|
|
70
|
+
let parsed;
|
|
71
|
+
try {
|
|
72
|
+
parsed = JSON.parse(text);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
if (!isPlainObject(parsed)) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const raw = parsed["error"];
|
|
81
|
+
if (raw === undefined) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
const message = isPlainObject(raw) && typeof raw["message"] === "string"
|
|
85
|
+
? raw["message"]
|
|
86
|
+
: typeof raw === "string"
|
|
87
|
+
? raw
|
|
88
|
+
: "the provider reported an error";
|
|
89
|
+
const code = isPlainObject(raw) && typeof raw["code"] === "number" ? raw["code"] : 0;
|
|
90
|
+
return {
|
|
91
|
+
code: "provider_error",
|
|
92
|
+
message: truncateMessage(message),
|
|
93
|
+
retryable: !PERMANENT_STATUSES.has(code),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A failure that came back with a status. The body is the provider's own
|
|
98
|
+
* explanation when it has one, in JSON or in plain text, and it is the only
|
|
99
|
+
* thing about the response that reaches the message.
|
|
100
|
+
*/
|
|
101
|
+
export function errorForStatus(status, body, options) {
|
|
102
|
+
const explained = errorFromJsonBody(body);
|
|
103
|
+
const reason = explained !== undefined ? explained.message : truncateMessage(body === "" ? "" : body);
|
|
104
|
+
const error = {
|
|
105
|
+
code: codeForStatus(status),
|
|
106
|
+
message: truncateMessage(reason === ""
|
|
107
|
+
? `the model provider answered ${status}`
|
|
108
|
+
: `the model provider answered ${status}: ${reason}`),
|
|
109
|
+
retryable: isRetryableStatus(status),
|
|
110
|
+
};
|
|
111
|
+
const wait = retryAfterMs(options.retryAfter ?? null, options.now);
|
|
112
|
+
if (wait !== undefined) {
|
|
113
|
+
error.retryAfterMs = wait;
|
|
114
|
+
}
|
|
115
|
+
return error;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/adapters/openai/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAInD,oFAAoF;AACpF,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CA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**
|
|
2
|
+
* The OpenAI-style model adapter (decision 14, step 5).
|
|
3
|
+
*
|
|
4
|
+
* One adapter for chat completions, pointed at OpenRouter or OpenCode Go by
|
|
5
|
+
* configuration. The core never imports this, and this imports the core and
|
|
6
|
+
* nothing else: `fetch`, `ReadableStream`, `TextDecoder` and `AbortSignal` are
|
|
7
|
+
* all web-standard, so it runs on Workers and on Node from the same code.
|
|
8
|
+
*
|
|
9
|
+
* The slot is `createOpenAIChatModel`. It reads its key from a Secrets slot on
|
|
10
|
+
* every call, streams one reply, and maps every failure onto the core's
|
|
11
|
+
* `ModelError` so step 2's retries can trust it.
|
|
12
|
+
*/
|
|
13
|
+
export { DEFAULT_DEADLINE_MS, createOpenAIChatModel, type OpenAIChatOptions } from "./model.js";
|
|
14
|
+
export { OPENCODE_GO_BASE_URL, OPENROUTER_BASE_URL, openCodeGo, openRouter, type ProviderOptions, } from "./presets.js";
|
|
15
|
+
export { MAX_MESSAGE_CHARS, MAX_RETRY_AFTER_MS, codeForStatus, errorForStatus, errorFromJsonBody, isRetryableStatus, retryAfterMs, truncateMessage, } from "./errors.js";
|
|
16
|
+
export { buildHeaders, buildWireRequest, type HeaderOptions, type WireBody } from "./request.js";
|
|
17
|
+
export { DEFAULT_STREAM_CAP_BYTES, readReply, sseReader, type ReadReplyOptions, type SseOptions, type SseReader, } from "./sse.js";
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,KAAK,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAChG,OAAO,EACN,oBAAoB,EACpB,mBAAmB,EACnB,UAAU,EACV,UAAU,EACV,KAAK,eAAe,GACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EACN,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,YAAY,EACZ,eAAe,GACf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,KAAK,aAAa,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AACjG,OAAO,EACN,wBAAwB,EACxB,SAAS,EACT,SAAS,EACT,KAAK,gBAAgB,EACrB,KAAK,UAAU,EACf,KAAK,SAAS,GACd,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OpenAI-style model adapter (decision 14, step 5).
|
|
3
|
+
*
|
|
4
|
+
* One adapter for chat completions, pointed at OpenRouter or OpenCode Go by
|
|
5
|
+
* configuration. The core never imports this, and this imports the core and
|
|
6
|
+
* nothing else: `fetch`, `ReadableStream`, `TextDecoder` and `AbortSignal` are
|
|
7
|
+
* all web-standard, so it runs on Workers and on Node from the same code.
|
|
8
|
+
*
|
|
9
|
+
* The slot is `createOpenAIChatModel`. It reads its key from a Secrets slot on
|
|
10
|
+
* every call, streams one reply, and maps every failure onto the core's
|
|
11
|
+
* `ModelError` so step 2's retries can trust it.
|
|
12
|
+
*/
|
|
13
|
+
export { DEFAULT_DEADLINE_MS, createOpenAIChatModel } from "./model.js";
|
|
14
|
+
export { OPENCODE_GO_BASE_URL, OPENROUTER_BASE_URL, openCodeGo, openRouter, } from "./presets.js";
|
|
15
|
+
export { MAX_MESSAGE_CHARS, MAX_RETRY_AFTER_MS, codeForStatus, errorForStatus, errorFromJsonBody, isRetryableStatus, retryAfterMs, truncateMessage, } from "./errors.js";
|
|
16
|
+
export { buildHeaders, buildWireRequest } from "./request.js";
|
|
17
|
+
export { DEFAULT_STREAM_CAP_BYTES, readReply, sseReader, } from "./sse.js";
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/openai/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAA0B,MAAM,YAAY,CAAC;AAChG,OAAO,EACN,oBAAoB,EACpB,mBAAmB,EACnB,UAAU,EACV,UAAU,GAEV,MAAM,cAAc,CAAC;AACtB,OAAO,EACN,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,YAAY,EACZ,eAAe,GACf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAqC,MAAM,cAAc,CAAC;AACjG,OAAO,EACN,wBAAwB,EACxB,SAAS,EACT,SAAS,GAIT,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Model slot, over chat completions (docs/adapters.md, step 5).
|
|
3
|
+
*
|
|
4
|
+
* One adapter for both of Autumn's providers (decision 14): the base URL, the
|
|
5
|
+
* model and the key's name are configuration, so OpenRouter and OpenCode Go are
|
|
6
|
+
* the same code pointed at different addresses. The key is read from a host's
|
|
7
|
+
* Secrets slot by name on every call and never held in this object, so it cannot
|
|
8
|
+
* be logged or recorded by accident. `fetch` and the clock are injected, which is
|
|
9
|
+
* how the fake server and the fake clock reach it.
|
|
10
|
+
*
|
|
11
|
+
* Every failure comes back as a `ModelError`; nothing here throws. A call that a
|
|
12
|
+
* person stopped is an error the loop reads as a stop (it checks its own signal
|
|
13
|
+
* before believing any failure), and a call that ran past its deadline is a
|
|
14
|
+
* retryable timeout.
|
|
15
|
+
*/
|
|
16
|
+
import type { Clock } from "../../core/clock.js";
|
|
17
|
+
import type { ModelSlot } from "../../core/model.js";
|
|
18
|
+
import type { SecretsSlot } from "../../core/slots.js";
|
|
19
|
+
/**
|
|
20
|
+
* The slice of `fetch` this adapter calls. Spelled structurally rather than as
|
|
21
|
+
* `typeof fetch`, so the fake model server, a cassette replay and the platform's
|
|
22
|
+
* own `fetch` all fit without a cast.
|
|
23
|
+
*/
|
|
24
|
+
export type FetchLike = (url: string, init?: {
|
|
25
|
+
method?: string;
|
|
26
|
+
headers?: Record<string, string>;
|
|
27
|
+
body?: string;
|
|
28
|
+
signal?: AbortSignal;
|
|
29
|
+
}) => Promise<Response>;
|
|
30
|
+
/** Two minutes, which is longer than any streamed reply should need. */
|
|
31
|
+
export declare const DEFAULT_DEADLINE_MS = 120000;
|
|
32
|
+
export interface OpenAIChatOptions {
|
|
33
|
+
/** The API root, without `/chat/completions`; a trailing slash is ignored. */
|
|
34
|
+
baseUrl: string;
|
|
35
|
+
/** The model id, which a recipe's fallback model overrides (decision 58). */
|
|
36
|
+
model: string;
|
|
37
|
+
secrets: SecretsSlot;
|
|
38
|
+
/** The secret's name. The key itself is never a parameter. */
|
|
39
|
+
keyName: string;
|
|
40
|
+
/** Extra request headers, such as the session OpenCode Go asks for. */
|
|
41
|
+
headers?: Record<string, string>;
|
|
42
|
+
fetch?: FetchLike;
|
|
43
|
+
clock: Clock;
|
|
44
|
+
/** How long one call may take before it is abandoned. */
|
|
45
|
+
deadlineMs?: number;
|
|
46
|
+
/** How many bytes of stream one reply may use. */
|
|
47
|
+
maxBytes?: number;
|
|
48
|
+
}
|
|
49
|
+
export declare function createOpenAIChatModel(options: OpenAIChatOptions): ModelSlot;
|
|
50
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAwC,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAE3F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAKvD;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,CACvB,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE;IACN,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB,KACG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEvB,wEAAwE;AACxE,eAAO,MAAM,mBAAmB,SAAU,CAAC;AAE3C,MAAM,WAAW,iBAAiB;IACjC,8EAA8E;IAC9E,OAAO,EAAE,MAAM,CAAC;IAChB,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,WAAW,CAAC;IACrB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;IAChB,uEAAuE;IACvE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,KAAK,EAAE,KAAK,CAAC;IACb,yDAAyD;IACzD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kDAAkD;IAClD,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAWD,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,iBAAiB,GAAG,SAAS,CAyJ3E"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Model slot, over chat completions (docs/adapters.md, step 5).
|
|
3
|
+
*
|
|
4
|
+
* One adapter for both of Autumn's providers (decision 14): the base URL, the
|
|
5
|
+
* model and the key's name are configuration, so OpenRouter and OpenCode Go are
|
|
6
|
+
* the same code pointed at different addresses. The key is read from a host's
|
|
7
|
+
* Secrets slot by name on every call and never held in this object, so it cannot
|
|
8
|
+
* be logged or recorded by accident. `fetch` and the clock are injected, which is
|
|
9
|
+
* how the fake server and the fake clock reach it.
|
|
10
|
+
*
|
|
11
|
+
* Every failure comes back as a `ModelError`; nothing here throws. A call that a
|
|
12
|
+
* person stopped is an error the loop reads as a stop (it checks its own signal
|
|
13
|
+
* before believing any failure), and a call that ran past its deadline is a
|
|
14
|
+
* retryable timeout.
|
|
15
|
+
*/
|
|
16
|
+
import { ok } from "../../core/result.js";
|
|
17
|
+
import { errorForStatus, errorFromJsonBody, truncateMessage } from "./errors.js";
|
|
18
|
+
import { buildHeaders, buildWireRequest } from "./request.js";
|
|
19
|
+
import { readReply } from "./sse.js";
|
|
20
|
+
/** Two minutes, which is longer than any streamed reply should need. */
|
|
21
|
+
export const DEFAULT_DEADLINE_MS = 120_000;
|
|
22
|
+
function reasonOf(error) {
|
|
23
|
+
return error instanceof Error ? error.message : String(error);
|
|
24
|
+
}
|
|
25
|
+
/** A message cannot carry the key, even if something underneath echoed it. */
|
|
26
|
+
function redact(text, secret) {
|
|
27
|
+
return secret === "" ? text : text.split(secret).join("[redacted]");
|
|
28
|
+
}
|
|
29
|
+
export function createOpenAIChatModel(options) {
|
|
30
|
+
const doFetch = options.fetch ?? fetch;
|
|
31
|
+
const clock = options.clock;
|
|
32
|
+
const deadlineMs = options.deadlineMs ?? DEFAULT_DEADLINE_MS;
|
|
33
|
+
const endpoint = `${options.baseUrl.replace(/\/+$/, "")}/chat/completions`;
|
|
34
|
+
const stopped = (secret) => ({
|
|
35
|
+
code: "timeout",
|
|
36
|
+
message: redact("the model call was stopped", secret),
|
|
37
|
+
retryable: false,
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
async complete(request) {
|
|
41
|
+
if (request.signal.aborted) {
|
|
42
|
+
// Stopped before anything was sent: nothing to clean up and no fetch.
|
|
43
|
+
return { ok: false, error: stopped("") };
|
|
44
|
+
}
|
|
45
|
+
const key = await options.secrets.get(options.keyName);
|
|
46
|
+
if (key === undefined) {
|
|
47
|
+
return {
|
|
48
|
+
ok: false,
|
|
49
|
+
error: {
|
|
50
|
+
code: "permission_denied",
|
|
51
|
+
message: `the secret "${options.keyName}" is not available`,
|
|
52
|
+
retryable: false,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// A stop can arrive while the secret is being read, and the listener below is
|
|
57
|
+
// added after it, so the signal is read again here: a stop before the request
|
|
58
|
+
// must cost nothing, not one whole stream.
|
|
59
|
+
if (request.signal.aborted) {
|
|
60
|
+
return { ok: false, error: stopped(key) };
|
|
61
|
+
}
|
|
62
|
+
// One controller carries both stops: the caller's, and the deadline's.
|
|
63
|
+
const controller = new AbortController();
|
|
64
|
+
let deadlineReached = false;
|
|
65
|
+
const forward = () => controller.abort();
|
|
66
|
+
request.signal.addEventListener("abort", forward, { once: true });
|
|
67
|
+
const deadline = clock.sleep(deadlineMs, controller.signal).then(() => {
|
|
68
|
+
deadlineReached = true;
|
|
69
|
+
controller.abort();
|
|
70
|
+
}, () => undefined);
|
|
71
|
+
const failure = (error) => {
|
|
72
|
+
if (request.signal.aborted) {
|
|
73
|
+
return stopped(key);
|
|
74
|
+
}
|
|
75
|
+
if (deadlineReached) {
|
|
76
|
+
return {
|
|
77
|
+
code: "timeout",
|
|
78
|
+
message: `the model did not answer within ${deadlineMs} ms`,
|
|
79
|
+
retryable: true,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
code: "provider_error",
|
|
84
|
+
message: truncateMessage(redact(`the model could not be reached: ${reasonOf(error)}`, key)),
|
|
85
|
+
retryable: true,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
try {
|
|
89
|
+
let response;
|
|
90
|
+
try {
|
|
91
|
+
response = await doFetch(endpoint, {
|
|
92
|
+
method: "POST",
|
|
93
|
+
headers: buildHeaders(options.headers === undefined
|
|
94
|
+
? { apiKey: key }
|
|
95
|
+
: { apiKey: key, extra: options.headers }),
|
|
96
|
+
body: JSON.stringify(buildWireRequest({ request, model: options.model })),
|
|
97
|
+
signal: controller.signal,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
return { ok: false, error: failure(error) };
|
|
102
|
+
}
|
|
103
|
+
if (!response.ok) {
|
|
104
|
+
const body = await textOf(response);
|
|
105
|
+
const error = errorForStatus(response.status, body, {
|
|
106
|
+
retryAfter: response.headers.get("retry-after"),
|
|
107
|
+
now: clock.now(),
|
|
108
|
+
});
|
|
109
|
+
return { ok: false, error: { ...error, message: redact(error.message, key) } };
|
|
110
|
+
}
|
|
111
|
+
// A 200 that is JSON is either the provider's own error object or an answer
|
|
112
|
+
// this adapter cannot read, because it asked for a stream.
|
|
113
|
+
const type = response.headers.get("content-type") ?? "";
|
|
114
|
+
if (type.includes("json") || type.includes("html")) {
|
|
115
|
+
const body = await textOf(response);
|
|
116
|
+
const explained = errorFromJsonBody(body);
|
|
117
|
+
if (explained !== undefined) {
|
|
118
|
+
return {
|
|
119
|
+
ok: false,
|
|
120
|
+
error: { ...explained, message: redact(explained.message, key) },
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
ok: false,
|
|
125
|
+
error: {
|
|
126
|
+
code: "provider_error",
|
|
127
|
+
message: redact(truncateMessage(`the model provider answered without a stream: ${truncateMessage(body)}`), key),
|
|
128
|
+
retryable: false,
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const read = await readReply(response.body, options.maxBytes === undefined
|
|
133
|
+
? { signal: controller.signal }
|
|
134
|
+
: { signal: controller.signal, maxBytes: options.maxBytes });
|
|
135
|
+
// Stopped as the last chunk arrived is still a stop: the reply is not
|
|
136
|
+
// handed over, because the run is not going to use it (decision 19).
|
|
137
|
+
if (request.signal.aborted) {
|
|
138
|
+
return { ok: false, error: stopped(key) };
|
|
139
|
+
}
|
|
140
|
+
if (deadlineReached) {
|
|
141
|
+
return { ok: false, error: failure(new Error("the deadline passed")) };
|
|
142
|
+
}
|
|
143
|
+
if (!read.ok) {
|
|
144
|
+
return {
|
|
145
|
+
ok: false,
|
|
146
|
+
error: { ...read.error, message: redact(read.error.message, key) },
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return ok(read.value);
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
request.signal.removeEventListener("abort", forward);
|
|
153
|
+
// Clears the deadline's sleep, so a fake clock has nothing left waiting.
|
|
154
|
+
controller.abort();
|
|
155
|
+
await deadline;
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/** A response body as text, without letting a read failure become a throw. */
|
|
161
|
+
async function textOf(response) {
|
|
162
|
+
try {
|
|
163
|
+
return await response.text();
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return "";
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../../src/adapters/openai/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,EAAE,EAAe,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACjF,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAiBrC,wEAAwE;AACxE,MAAM,CAAC,MAAM,mBAAmB,GAAG,OAAO,CAAC;AAoB3C,SAAS,QAAQ,CAAC,KAAc;IAC/B,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC/D,CAAC;AAED,8EAA8E;AAC9E,SAAS,MAAM,CAAC,IAAY,EAAE,MAAc;IAC3C,OAAO,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,M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AAE,CAAC;gBACxE,CAAC;gBACD,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;oBACd,OAAO;wBACN,EAAE,EAAE,KAAK;wBACT,KAAK,EAAE,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE;qBAClE,CAAC;gBACH,CAAC;gBACD,OAAO,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;oBAAS,CAAC;gBACV,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;gBACrD,yEAAyE;gBACzE,UAAU,CAAC,KAAK,EAAE,CAAC;gBACnB,MAAM,QAAQ,CAAC;YAChB,CAAC;QACF,CAAC;KACD,CAAC;AACH,CAAC;AAED,8EAA8E;AAC9E,KAAK,UAAU,MAAM,CAAC,QAAkB;IACvC,IAAI,CAAC;QACJ,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,EAAE,CAAC;IACX,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two providers this adapter speaks (decision 14, docs/adapters.md OAI30).
|
|
3
|
+
*
|
|
4
|
+
* A preset is only configuration: a base URL, the name of the secret that holds
|
|
5
|
+
* the key, and any header the provider asks for. Everything else — the request,
|
|
6
|
+
* the stream, the errors — is the same code, which is the point of speaking one
|
|
7
|
+
* dialect. The key itself is never here: it is read from the Secrets slot by the
|
|
8
|
+
* name a preset gives.
|
|
9
|
+
*/
|
|
10
|
+
import type { Clock } from "../../core/clock.js";
|
|
11
|
+
import type { ModelSlot } from "../../core/model.js";
|
|
12
|
+
import type { SecretsSlot } from "../../core/slots.js";
|
|
13
|
+
import { type FetchLike } from "./model.js";
|
|
14
|
+
export declare const OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1";
|
|
15
|
+
export declare const OPENCODE_GO_BASE_URL = "https://opencode.ai/zen/go/v1";
|
|
16
|
+
export interface ProviderOptions {
|
|
17
|
+
/** The model id, which a recipe's fallback model overrides (decision 58). */
|
|
18
|
+
model: string;
|
|
19
|
+
secrets: SecretsSlot;
|
|
20
|
+
clock: Clock;
|
|
21
|
+
fetch?: FetchLike;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
deadlineMs?: number;
|
|
24
|
+
maxBytes?: number;
|
|
25
|
+
}
|
|
26
|
+
/** OpenRouter, the primary provider (decision 14). */
|
|
27
|
+
export declare function openRouter(options: ProviderOptions): ModelSlot;
|
|
28
|
+
/**
|
|
29
|
+
* OpenCode Go. It asks every client for a stable session header so a conversation
|
|
30
|
+
* keeps its routing and its prompt cache, so one is required rather than
|
|
31
|
+
* optional: a session that changes every call would quietly lose both.
|
|
32
|
+
*/
|
|
33
|
+
export declare function openCodeGo(options: ProviderOptions & {
|
|
34
|
+
session: string;
|
|
35
|
+
}): ModelSlot;
|
|
36
|
+
//# sourceMappingURL=presets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presets.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/presets.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAyB,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAEnE,eAAO,MAAM,mBAAmB,iCAAiC,CAAC;AAClE,eAAO,MAAM,oBAAoB,kCAAkC,CAAC;AAEpE,MAAM,WAAW,eAAe;IAC/B,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,WAAW,CAAC;IACrB,KAAK,EAAE,KAAK,CAAC;IACb,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAsBD,sDAAsD;AACtD,wBAAgB,UAAU,CAAC,OAAO,EAAE,eAAe,GAAG,SAAS,CAE9D;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,eAAe,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAKpF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two providers this adapter speaks (decision 14, docs/adapters.md OAI30).
|
|
3
|
+
*
|
|
4
|
+
* A preset is only configuration: a base URL, the name of the secret that holds
|
|
5
|
+
* the key, and any header the provider asks for. Everything else — the request,
|
|
6
|
+
* the stream, the errors — is the same code, which is the point of speaking one
|
|
7
|
+
* dialect. The key itself is never here: it is read from the Secrets slot by the
|
|
8
|
+
* name a preset gives.
|
|
9
|
+
*/
|
|
10
|
+
import { createOpenAIChatModel } from "./model.js";
|
|
11
|
+
export const OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1";
|
|
12
|
+
export const OPENCODE_GO_BASE_URL = "https://opencode.ai/zen/go/v1";
|
|
13
|
+
function build(baseUrl, keyName, options) {
|
|
14
|
+
const headers = { ...options.headers, ...options.extra };
|
|
15
|
+
const settings = {
|
|
16
|
+
baseUrl,
|
|
17
|
+
model: options.model,
|
|
18
|
+
secrets: options.secrets,
|
|
19
|
+
keyName,
|
|
20
|
+
clock: options.clock,
|
|
21
|
+
...(Object.keys(headers).length === 0 ? {} : { headers }),
|
|
22
|
+
...(options.fetch === undefined ? {} : { fetch: options.fetch }),
|
|
23
|
+
...(options.deadlineMs === undefined ? {} : { deadlineMs: options.deadlineMs }),
|
|
24
|
+
...(options.maxBytes === undefined ? {} : { maxBytes: options.maxBytes }),
|
|
25
|
+
};
|
|
26
|
+
return createOpenAIChatModel(settings);
|
|
27
|
+
}
|
|
28
|
+
/** OpenRouter, the primary provider (decision 14). */
|
|
29
|
+
export function openRouter(options) {
|
|
30
|
+
return build(OPENROUTER_BASE_URL, "OPENROUTER_API_KEY", options);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* OpenCode Go. It asks every client for a stable session header so a conversation
|
|
34
|
+
* keeps its routing and its prompt cache, so one is required rather than
|
|
35
|
+
* optional: a session that changes every call would quietly lose both.
|
|
36
|
+
*/
|
|
37
|
+
export function openCodeGo(options) {
|
|
38
|
+
return build(OPENCODE_GO_BASE_URL, "OPENCODE_GO_API_KEY", {
|
|
39
|
+
...options,
|
|
40
|
+
extra: { "x-opencode-session": options.session },
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=presets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presets.js","sourceRoot":"","sources":["../../../src/adapters/openai/presets.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,EAAE,qBAAqB,EAAkB,MAAM,YAAY,CAAC;AAEnE,MAAM,CAAC,MAAM,mBAAmB,GAAG,8BAA8B,CAAC;AAClE,MAAM,CAAC,MAAM,oBAAoB,GAAG,+BAA+B,CAAC;AAapE,SAAS,KAAK,CACb,OAAe,EACf,OAAe,EACf,OAA6D;IAE7D,MAAM,OAAO,GAA2B,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC;IACjF,MAAM,QAAQ,GAAG;QAChB,OAAO;QACP,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,OAAO;QACP,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC;QACzD,GAAG,CAAC,OAAO,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;QAChE,GAAG,CAAC,OAAO,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC;QAC/E,GAAG,CAAC,OAAO,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC;KACzE,CAAC;IACF,OAAO,qBAAqB,CAAC,QAAQ,CAAC,CAAC;AACxC,CAAC;AAED,sDAAsD;AACtD,MAAM,UAAU,UAAU,CAAC,OAAwB;IAClD,OAAO,KAAK,CAAC,mBAAmB,EAAE,oBAAoB,EAAE,OAAO,CAAC,CAAC;AAClE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,OAA8C;IACxE,OAAO,KAAK,CAAC,oBAAoB,EAAE,qBAAqB,EAAE;QACzD,GAAG,OAAO;QACV,KAAK,EAAE,EAAE,oBAAoB,EAAE,OAAO,CAAC,OAAO,EAAE;KAChD,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The request on the wire (docs/adapters.md, OAI14 to OAI19).
|
|
3
|
+
*
|
|
4
|
+
* The Model slot's request is generic — a system prompt, messages, tools, a
|
|
5
|
+
* signal — and chat completions wants the same things spelled its own way. This
|
|
6
|
+
* file is that translation and nothing else: it is pure, so a test can read the
|
|
7
|
+
* whole of what would be sent without a socket.
|
|
8
|
+
*
|
|
9
|
+
* Two properties matter more than the mapping. Nothing is sent that a model might
|
|
10
|
+
* reject: no `tool_choice`, no `stream_options`, no usage switch, no provider
|
|
11
|
+
* field, because the core has no way to ask for one and both endpoints answer
|
|
12
|
+
* without them. And the key exists in exactly one place, the `Authorization`
|
|
13
|
+
* header, built from what a host's Secrets slot gave rather than from anything
|
|
14
|
+
* that could be logged or recorded.
|
|
15
|
+
*/
|
|
16
|
+
import type { JsonValue } from "../../core/json.js";
|
|
17
|
+
import type { ModelRequest } from "../../core/model.js";
|
|
18
|
+
/** One chat-completions request body, as the endpoint takes it. */
|
|
19
|
+
export interface WireBody {
|
|
20
|
+
model: string;
|
|
21
|
+
stream: true;
|
|
22
|
+
messages: JsonValue[];
|
|
23
|
+
tools?: JsonValue[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The body for one request. `configured` is the adapter's model, which a recipe's
|
|
27
|
+
* fallback model overrides (decision 58).
|
|
28
|
+
*/
|
|
29
|
+
export declare function buildWireRequest(options: {
|
|
30
|
+
request: ModelRequest;
|
|
31
|
+
model: string;
|
|
32
|
+
}): WireBody;
|
|
33
|
+
export interface HeaderOptions {
|
|
34
|
+
/** The key, read from a host's Secrets slot by the adapter and never held here. */
|
|
35
|
+
apiKey: string;
|
|
36
|
+
/** A stable session id, which OpenCode Go asks for so it can route and cache. */
|
|
37
|
+
session?: string;
|
|
38
|
+
/** A preset's own headers. None of them can replace the computed ones below. */
|
|
39
|
+
extra?: Record<string, string>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The headers for one request. A preset's own headers go on first and the
|
|
43
|
+
* computed ones last, so nothing can replace the key or the content type by
|
|
44
|
+
* accident.
|
|
45
|
+
*/
|
|
46
|
+
export declare function buildHeaders(options: HeaderOptions): Record<string, string>;
|
|
47
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/adapters/openai/request.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,mEAAmE;AACnE,MAAM,WAAW,QAAQ;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,IAAI,CAAC;IACb,QAAQ,EAAE,SAAS,EAAE,CAAC;IACtB,KAAK,CAAC,EAAE,SAAS,EAAE,CAAC;CACpB;AA6BD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IAAE,OAAO,EAAE,YAAY,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,QAAQ,CA0B5F;AAED,MAAM,WAAW,aAAa;IAC7B,mFAAmF;IACnF,MAAM,EAAE,MAAM,CAAC;IACf,iFAAiF;IACjF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gFAAgF;IAChF,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC/B;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAe3E"}
|