@opencode/ai 0.0.0-reserved → 2.0.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/README.md +879 -2
- package/dist/cache-policy.d.ts +2 -0
- package/dist/cache-policy.js +129 -0
- package/dist/image-client.d.ts +19 -0
- package/dist/image-client.js +28 -0
- package/dist/image.d.ts +110 -0
- package/dist/image.js +103 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +12 -0
- package/dist/llm.d.ts +252 -0
- package/dist/llm.js +74 -0
- package/dist/protocols/alibaba-chat.d.ts +225 -0
- package/dist/protocols/alibaba-chat.js +75 -0
- package/dist/protocols/alibaba-messages.d.ts +350 -0
- package/dist/protocols/alibaba-messages.js +40 -0
- package/dist/protocols/alibaba-responses.d.ts +255 -0
- package/dist/protocols/alibaba-responses.js +80 -0
- package/dist/protocols/anthropic-messages.d.ts +980 -0
- package/dist/protocols/anthropic-messages.js +1460 -0
- package/dist/protocols/bedrock-converse.d.ts +499 -0
- package/dist/protocols/bedrock-converse.js +643 -0
- package/dist/protocols/bedrock-event-stream.d.ts +9 -0
- package/dist/protocols/bedrock-event-stream.js +97 -0
- package/dist/protocols/gemini.d.ts +285 -0
- package/dist/protocols/gemini.js +671 -0
- package/dist/protocols/google-images.d.ts +30 -0
- package/dist/protocols/google-images.js +181 -0
- package/dist/protocols/index.d.ts +12 -0
- package/dist/protocols/index.js +12 -0
- package/dist/protocols/meta-images.d.ts +26 -0
- package/dist/protocols/meta-images.js +89 -0
- package/dist/protocols/meta-messages.d.ts +356 -0
- package/dist/protocols/meta-messages.js +41 -0
- package/dist/protocols/meta-responses.d.ts +473 -0
- package/dist/protocols/meta-responses.js +173 -0
- package/dist/protocols/mistral-chat.d.ts +540 -0
- package/dist/protocols/mistral-chat.js +624 -0
- package/dist/protocols/open-responses-channel.d.ts +25 -0
- package/dist/protocols/open-responses-channel.js +141 -0
- package/dist/protocols/open-responses-continuation.d.ts +16 -0
- package/dist/protocols/open-responses-continuation.js +170 -0
- package/dist/protocols/open-responses.d.ts +1715 -0
- package/dist/protocols/open-responses.js +1228 -0
- package/dist/protocols/openai-chat.d.ts +860 -0
- package/dist/protocols/openai-chat.js +966 -0
- package/dist/protocols/openai-compatible-chat.d.ts +109 -0
- package/dist/protocols/openai-compatible-chat.js +19 -0
- package/dist/protocols/openai-compatible-responses.d.ts +172 -0
- package/dist/protocols/openai-compatible-responses.js +18 -0
- package/dist/protocols/openai-images.d.ts +32 -0
- package/dist/protocols/openai-images.js +183 -0
- package/dist/protocols/openai-responses.d.ts +1389 -0
- package/dist/protocols/openai-responses.js +260 -0
- package/dist/protocols/shared.d.ts +193 -0
- package/dist/protocols/shared.js +290 -0
- package/dist/protocols/utils/bedrock-auth.d.ts +60 -0
- package/dist/protocols/utils/bedrock-auth.js +85 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +19 -0
- package/dist/protocols/utils/bedrock-cache.js +49 -0
- package/dist/protocols/utils/bedrock-media.d.ts +55 -0
- package/dist/protocols/utils/bedrock-media.js +73 -0
- package/dist/protocols/utils/cache.d.ts +6 -0
- package/dist/protocols/utils/cache.js +5 -0
- package/dist/protocols/utils/gemini-tool-schema.d.ts +2 -0
- package/dist/protocols/utils/gemini-tool-schema.js +103 -0
- package/dist/protocols/utils/image-input.d.ts +21 -0
- package/dist/protocols/utils/image-input.js +20 -0
- package/dist/protocols/utils/lifecycle.d.ts +24 -0
- package/dist/protocols/utils/lifecycle.js +72 -0
- package/dist/protocols/utils/meta-image.d.ts +2 -0
- package/dist/protocols/utils/meta-image.js +13 -0
- package/dist/protocols/utils/open-responses-options.d.ts +56 -0
- package/dist/protocols/utils/open-responses-options.js +61 -0
- package/dist/protocols/utils/openai-image.d.ts +5 -0
- package/dist/protocols/utils/openai-image.js +18 -0
- package/dist/protocols/utils/openai-options.d.ts +16 -0
- package/dist/protocols/utils/openai-options.js +14 -0
- package/dist/protocols/utils/partial-json-options.d.ts +62 -0
- package/dist/protocols/utils/partial-json-options.js +52 -0
- package/dist/protocols/utils/partial-json.d.ts +8 -0
- package/dist/protocols/utils/partial-json.js +279 -0
- package/dist/protocols/utils/responses-checkpoint.d.ts +4 -0
- package/dist/protocols/utils/responses-checkpoint.js +107 -0
- package/dist/protocols/utils/responses-compaction.d.ts +4 -0
- package/dist/protocols/utils/responses-compaction.js +129 -0
- package/dist/protocols/utils/responses-hosted-tools.d.ts +26 -0
- package/dist/protocols/utils/responses-hosted-tools.js +32 -0
- package/dist/protocols/utils/tool-schema.d.ts +8 -0
- package/dist/protocols/utils/tool-schema.js +54 -0
- package/dist/protocols/utils/tool-stream.d.ts +710 -0
- package/dist/protocols/utils/tool-stream.js +155 -0
- package/dist/protocols/xai-images.d.ts +26 -0
- package/dist/protocols/xai-images.js +106 -0
- package/dist/protocols/xai-responses.d.ts +250 -0
- package/dist/protocols/xai-responses.js +81 -0
- package/dist/protocols/zai-chat.d.ts +202 -0
- package/dist/protocols/zai-chat.js +49 -0
- package/dist/protocols/zai-images.d.ts +22 -0
- package/dist/protocols/zai-images.js +79 -0
- package/dist/protocols/zai-messages.d.ts +344 -0
- package/dist/protocols/zai-messages.js +27 -0
- package/dist/protocols.d.ts +1 -0
- package/dist/protocols.js +1 -0
- package/dist/provider-error.d.ts +15 -0
- package/dist/provider-error.js +125 -0
- package/dist/provider-package.d.ts +15 -0
- package/dist/provider-package.js +1 -0
- package/dist/provider.d.ts +23 -0
- package/dist/provider.js +2 -0
- package/dist/providers/alibaba/chat.d.ts +1 -0
- package/dist/providers/alibaba/chat.js +1 -0
- package/dist/providers/alibaba/messages.d.ts +3 -0
- package/dist/providers/alibaba/messages.js +1 -0
- package/dist/providers/alibaba/responses.d.ts +3 -0
- package/dist/providers/alibaba/responses.js +1 -0
- package/dist/providers/alibaba.d.ts +711 -0
- package/dist/providers/alibaba.js +94 -0
- package/dist/providers/amazon-bedrock/mantle/chat.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/chat.js +1 -0
- package/dist/providers/amazon-bedrock/mantle/responses.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/responses.js +1 -0
- package/dist/providers/amazon-bedrock/mantle.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle.js +1 -0
- package/dist/providers/amazon-bedrock-mantle.d.ts +313 -0
- package/dist/providers/amazon-bedrock-mantle.js +79 -0
- package/dist/providers/amazon-bedrock.d.ts +173 -0
- package/dist/providers/amazon-bedrock.js +41 -0
- package/dist/providers/anthropic-compatible.d.ts +309 -0
- package/dist/providers/anthropic-compatible.js +50 -0
- package/dist/providers/anthropic.d.ts +307 -0
- package/dist/providers/anthropic.js +42 -0
- package/dist/providers/azure/chat.d.ts +2 -0
- package/dist/providers/azure/chat.js +1 -0
- package/dist/providers/azure/responses.d.ts +2 -0
- package/dist/providers/azure/responses.js +1 -0
- package/dist/providers/azure.d.ts +598 -0
- package/dist/providers/azure.js +115 -0
- package/dist/providers/baseten.d.ts +228 -0
- package/dist/providers/baseten.js +38 -0
- package/dist/providers/cerebras.d.ts +227 -0
- package/dist/providers/cerebras.js +40 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts +237 -0
- package/dist/providers/cloudflare-ai-gateway.js +65 -0
- package/dist/providers/cloudflare-workers-ai.d.ts +231 -0
- package/dist/providers/cloudflare-workers-ai.js +51 -0
- package/dist/providers/deepinfra.d.ts +227 -0
- package/dist/providers/deepinfra.js +43 -0
- package/dist/providers/deepseek.d.ts +228 -0
- package/dist/providers/deepseek.js +41 -0
- package/dist/providers/fireworks.d.ts +228 -0
- package/dist/providers/fireworks.js +38 -0
- package/dist/providers/google-vertex/chat.d.ts +2 -0
- package/dist/providers/google-vertex/chat.js +1 -0
- package/dist/providers/google-vertex/gemini.d.ts +2 -0
- package/dist/providers/google-vertex/gemini.js +1 -0
- package/dist/providers/google-vertex/messages.d.ts +2 -0
- package/dist/providers/google-vertex/messages.js +1 -0
- package/dist/providers/google-vertex/responses.d.ts +2 -0
- package/dist/providers/google-vertex/responses.js +1 -0
- package/dist/providers/google-vertex-chat.d.ts +132 -0
- package/dist/providers/google-vertex-chat.js +55 -0
- package/dist/providers/google-vertex-messages.d.ts +306 -0
- package/dist/providers/google-vertex-messages.js +75 -0
- package/dist/providers/google-vertex-responses.d.ts +197 -0
- package/dist/providers/google-vertex-responses.js +56 -0
- package/dist/providers/google-vertex-shared.d.ts +22 -0
- package/dist/providers/google-vertex-shared.js +72 -0
- package/dist/providers/google-vertex.d.ts +119 -0
- package/dist/providers/google-vertex.js +98 -0
- package/dist/providers/google.d.ts +114 -0
- package/dist/providers/google.js +42 -0
- package/dist/providers/groq.d.ts +314 -0
- package/dist/providers/groq.js +81 -0
- package/dist/providers/index.d.ts +31 -0
- package/dist/providers/index.js +31 -0
- package/dist/providers/meta/chat.d.ts +2 -0
- package/dist/providers/meta/chat.js +1 -0
- package/dist/providers/meta/messages.d.ts +2 -0
- package/dist/providers/meta/messages.js +1 -0
- package/dist/providers/meta/responses.d.ts +2 -0
- package/dist/providers/meta/responses.js +1 -0
- package/dist/providers/meta.d.ts +636 -0
- package/dist/providers/meta.js +113 -0
- package/dist/providers/minimax/chat.d.ts +4 -0
- package/dist/providers/minimax/chat.js +8 -0
- package/dist/providers/minimax/messages.d.ts +1 -0
- package/dist/providers/minimax/messages.js +1 -0
- package/dist/providers/minimax/responses.d.ts +4 -0
- package/dist/providers/minimax/responses.js +8 -0
- package/dist/providers/minimax.d.ts +598 -0
- package/dist/providers/minimax.js +89 -0
- package/dist/providers/mistral.d.ts +207 -0
- package/dist/providers/mistral.js +28 -0
- package/dist/providers/moonshot/chat.d.ts +1 -0
- package/dist/providers/moonshot/chat.js +1 -0
- package/dist/providers/moonshot/messages.d.ts +4 -0
- package/dist/providers/moonshot/messages.js +8 -0
- package/dist/providers/moonshot/responses.d.ts +4 -0
- package/dist/providers/moonshot/responses.js +8 -0
- package/dist/providers/moonshot.d.ts +596 -0
- package/dist/providers/moonshot.js +90 -0
- package/dist/providers/open-responses-options.d.ts +6 -0
- package/dist/providers/open-responses-options.js +1 -0
- package/dist/providers/openai/chat.d.ts +2 -0
- package/dist/providers/openai/chat.js +1 -0
- package/dist/providers/openai/responses.d.ts +2 -0
- package/dist/providers/openai/responses.js +1 -0
- package/dist/providers/openai-compatible/responses.d.ts +1 -0
- package/dist/providers/openai-compatible/responses.js +1 -0
- package/dist/providers/openai-compatible-responses.d.ts +195 -0
- package/dist/providers/openai-compatible-responses.js +32 -0
- package/dist/providers/openai-compatible.d.ts +130 -0
- package/dist/providers/openai-compatible.js +33 -0
- package/dist/providers/openai-options.d.ts +26 -0
- package/dist/providers/openai-options.js +41 -0
- package/dist/providers/openai.d.ts +404 -0
- package/dist/providers/openai.js +82 -0
- package/dist/providers/openrouter.d.ts +563 -0
- package/dist/providers/openrouter.js +114 -0
- package/dist/providers/togetherai.d.ts +227 -0
- package/dist/providers/togetherai.js +40 -0
- package/dist/providers/xai.d.ts +163 -0
- package/dist/providers/xai.js +88 -0
- package/dist/providers/zai/chat.d.ts +1 -0
- package/dist/providers/zai/chat.js +1 -0
- package/dist/providers/zai-coding-plan/chat.d.ts +1 -0
- package/dist/providers/zai-coding-plan/chat.js +1 -0
- package/dist/providers/zai-coding-plan/messages.d.ts +4 -0
- package/dist/providers/zai-coding-plan/messages.js +8 -0
- package/dist/providers/zai-coding-plan/responses.d.ts +4 -0
- package/dist/providers/zai-coding-plan/responses.js +8 -0
- package/dist/providers/zai-coding-plan.d.ts +577 -0
- package/dist/providers/zai-coding-plan.js +63 -0
- package/dist/providers/zai.d.ts +149 -0
- package/dist/providers/zai.js +53 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1 -0
- package/dist/route/auth-options.d.ts +34 -0
- package/dist/route/auth-options.js +14 -0
- package/dist/route/auth.d.ts +51 -0
- package/dist/route/auth.js +87 -0
- package/dist/route/client.d.ts +430 -0
- package/dist/route/client.js +307 -0
- package/dist/route/endpoint.d.ts +28 -0
- package/dist/route/endpoint.js +21 -0
- package/dist/route/executor.d.ts +27 -0
- package/dist/route/executor.js +183 -0
- package/dist/route/framing.d.ts +29 -0
- package/dist/route/framing.js +14 -0
- package/dist/route/index.d.ts +16 -0
- package/dist/route/index.js +9 -0
- package/dist/route/protocol.d.ts +76 -0
- package/dist/route/protocol.js +16 -0
- package/dist/route/transport/http.d.ts +34 -0
- package/dist/route/transport/http.js +68 -0
- package/dist/route/transport/index.d.ts +41 -0
- package/dist/route/transport/index.js +4 -0
- package/dist/route/transport/websocket-channel.d.ts +57 -0
- package/dist/route/transport/websocket-channel.js +1 -0
- package/dist/route/transport/websocket.d.ts +56 -0
- package/dist/route/transport/websocket.js +370 -0
- package/dist/route.d.ts +1 -0
- package/dist/route.js +1 -0
- package/dist/schema/errors.d.ts +170 -0
- package/dist/schema/errors.js +131 -0
- package/dist/schema/events.d.ts +8123 -0
- package/dist/schema/events.js +550 -0
- package/dist/schema/ids.d.ts +17 -0
- package/dist/schema/ids.js +10 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +5 -0
- package/dist/schema/messages.d.ts +646 -0
- package/dist/schema/messages.js +301 -0
- package/dist/schema/options.d.ts +159 -0
- package/dist/schema/options.js +189 -0
- package/dist/testing.d.ts +1693 -0
- package/dist/testing.js +129 -0
- package/dist/tool-history.d.ts +2 -0
- package/dist/tool-history.js +84 -0
- package/dist/tool-runtime.d.ts +15 -0
- package/dist/tool-runtime.js +62 -0
- package/dist/tool.d.ts +136 -0
- package/dist/tool.js +66 -0
- package/dist/utils/record.d.ts +2 -0
- package/dist/utils/record.js +2 -0
- package/dist/utils/sanitize.d.ts +1 -0
- package/dist/utils/sanitize.js +12 -0
- package/package.json +43 -10
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Apply an `LLMRequest.cache` policy by injecting `CacheHint`s onto the parts
|
|
2
|
+
// the policy designates. Runs once at compile time, before the per-protocol
|
|
3
|
+
// body builder, so the existing inline-hint lowering path handles the rest.
|
|
4
|
+
//
|
|
5
|
+
// The default `"auto"` shape places breakpoints at the last tool definition,
|
|
6
|
+
// the first and last distinct system parts, and the conversation tail. This
|
|
7
|
+
// exposes reusable tool, base-agent, project, and session prefixes while
|
|
8
|
+
// advancing the tail after each tool result keeps recent conversation prefixes
|
|
9
|
+
// reusable during long agent runs.
|
|
10
|
+
//
|
|
11
|
+
// Manual `cache: CacheHint` placements on individual parts are preserved and
|
|
12
|
+
// count against the four-breakpoint budget; auto only fills remaining slots.
|
|
13
|
+
import { CacheHint } from "./schema/options.js";
|
|
14
|
+
import { LLMRequest, Message, ToolDefinition } from "./schema/messages.js";
|
|
15
|
+
const AUTO = {
|
|
16
|
+
tools: true,
|
|
17
|
+
system: true,
|
|
18
|
+
messages: { tail: 1 },
|
|
19
|
+
};
|
|
20
|
+
const NONE = {};
|
|
21
|
+
const BREAKPOINT_CAP = 4;
|
|
22
|
+
// Resolution rules:
|
|
23
|
+
// - undefined → "auto" — caching is on by default.
|
|
24
|
+
// - "auto" → tools + first/last system + final message boundary.
|
|
25
|
+
// - "none" → no auto placement; manual `CacheHint`s still flow.
|
|
26
|
+
// - object form → exactly what the caller asked for.
|
|
27
|
+
const resolve = (policy) => {
|
|
28
|
+
if (policy === undefined || policy === "auto")
|
|
29
|
+
return AUTO;
|
|
30
|
+
if (policy === "none")
|
|
31
|
+
return NONE;
|
|
32
|
+
return policy;
|
|
33
|
+
};
|
|
34
|
+
// Protocols whose wire format ignores inline cache markers (OpenAI's implicit
|
|
35
|
+
// prefix caching, Gemini's implicit + out-of-band CachedContent). Skip the
|
|
36
|
+
// whole policy pass for these — emitting hints would be harmless but pointless.
|
|
37
|
+
const RESPECTS_INLINE_HINTS = new Set([
|
|
38
|
+
"anthropic-messages",
|
|
39
|
+
"google-vertex-messages",
|
|
40
|
+
"bedrock-converse",
|
|
41
|
+
"openrouter",
|
|
42
|
+
]);
|
|
43
|
+
const makeHint = (ttlSeconds) => ttlSeconds !== undefined ? new CacheHint({ type: "ephemeral", ttlSeconds }) : new CacheHint({ type: "ephemeral" });
|
|
44
|
+
const markLastTool = (tools, hint, budget) => {
|
|
45
|
+
const target = tools.at(-1);
|
|
46
|
+
if (target === undefined)
|
|
47
|
+
return tools;
|
|
48
|
+
if (target.type === "namespace") {
|
|
49
|
+
const nested = markLastTool(target.tools, hint, budget);
|
|
50
|
+
return nested === target.tools ? tools : [...tools.slice(0, -1), { ...target, tools: nested }];
|
|
51
|
+
}
|
|
52
|
+
if (target.cache || budget.remaining === 0)
|
|
53
|
+
return tools;
|
|
54
|
+
budget.remaining -= 1;
|
|
55
|
+
return [...tools.slice(0, -1), new ToolDefinition({ ...target, cache: hint })];
|
|
56
|
+
};
|
|
57
|
+
const countToolHints = (tools) => tools.reduce((count, tool) => count + (tool.type === "tool" ? (tool.cache === undefined ? 0 : 1) : countToolHints(tool.tools)), 0);
|
|
58
|
+
const markSystemBoundaries = (system, hint, budget) => {
|
|
59
|
+
if (system.length === 0)
|
|
60
|
+
return system;
|
|
61
|
+
let changed = false;
|
|
62
|
+
const next = system.map((part, index) => {
|
|
63
|
+
if ((index !== 0 && index !== system.length - 1) || part.cache || budget.remaining === 0)
|
|
64
|
+
return part;
|
|
65
|
+
budget.remaining -= 1;
|
|
66
|
+
changed = true;
|
|
67
|
+
return { ...part, cache: hint };
|
|
68
|
+
});
|
|
69
|
+
return changed ? next : system;
|
|
70
|
+
};
|
|
71
|
+
const lastIndexOfRole = (messages, role) => messages.findLastIndex((m) => m.role === role);
|
|
72
|
+
// Mark the last text part of `messages[index]`. If no text part exists, mark
|
|
73
|
+
// the last content part regardless of type — that's the breakpoint position
|
|
74
|
+
// in tool-result-only messages too.
|
|
75
|
+
const markMessageAt = (messages, index, hint, budget) => {
|
|
76
|
+
if (index < 0 || index >= messages.length)
|
|
77
|
+
return messages;
|
|
78
|
+
const target = messages[index];
|
|
79
|
+
if (target.content.length === 0)
|
|
80
|
+
return messages;
|
|
81
|
+
const lastTextIndex = target.content.findLastIndex((part) => part.type === "text");
|
|
82
|
+
const markAt = lastTextIndex >= 0 ? lastTextIndex : target.content.length - 1;
|
|
83
|
+
const existing = target.content[markAt];
|
|
84
|
+
if (("cache" in existing && existing.cache) || budget.remaining === 0)
|
|
85
|
+
return messages;
|
|
86
|
+
budget.remaining -= 1;
|
|
87
|
+
const nextContent = target.content.map((part, i) => (i === markAt ? { ...part, cache: hint } : part));
|
|
88
|
+
const next = new Message({ ...target, content: nextContent });
|
|
89
|
+
// Single pass over `messages`, substituting the one updated entry. Long
|
|
90
|
+
// conversations call this on every request, so avoid `.map()` here — its
|
|
91
|
+
// closure dispatch and identity copies show up in profiling.
|
|
92
|
+
const result = messages.slice();
|
|
93
|
+
result[index] = next;
|
|
94
|
+
return result;
|
|
95
|
+
};
|
|
96
|
+
const markMessages = (messages, strategy, hint, budget) => {
|
|
97
|
+
if (messages.length === 0)
|
|
98
|
+
return messages;
|
|
99
|
+
if (strategy === "latest-user-message")
|
|
100
|
+
return markMessageAt(messages, lastIndexOfRole(messages, "user"), hint, budget);
|
|
101
|
+
if (strategy === "latest-assistant")
|
|
102
|
+
return markMessageAt(messages, lastIndexOfRole(messages, "assistant"), hint, budget);
|
|
103
|
+
const start = Math.max(0, messages.length - strategy.tail);
|
|
104
|
+
let next = messages;
|
|
105
|
+
for (let i = start; i < messages.length; i++)
|
|
106
|
+
next = markMessageAt(next, i, hint, budget);
|
|
107
|
+
return next;
|
|
108
|
+
};
|
|
109
|
+
const countHints = (request) => countToolHints(request.tools) +
|
|
110
|
+
request.system.reduce((count, part) => count + (part.cache === undefined ? 0 : 1), 0) +
|
|
111
|
+
request.messages.reduce((count, message) => count +
|
|
112
|
+
message.content.reduce((contentCount, part) => contentCount + ("cache" in part && part.cache !== undefined ? 1 : 0), 0), 0);
|
|
113
|
+
export const applyCachePolicy = (request) => {
|
|
114
|
+
if (!RESPECTS_INLINE_HINTS.has(request.model.route.id))
|
|
115
|
+
return request;
|
|
116
|
+
if (request.model.route.id === "openrouter" && (request.cache === undefined || request.cache === "auto"))
|
|
117
|
+
return request;
|
|
118
|
+
const policy = resolve(request.cache);
|
|
119
|
+
if (!policy.tools && !policy.system && !policy.messages)
|
|
120
|
+
return request;
|
|
121
|
+
const hint = makeHint(policy.ttlSeconds);
|
|
122
|
+
const budget = { remaining: Math.max(0, BREAKPOINT_CAP - countHints(request)) };
|
|
123
|
+
const tools = policy.tools ? markLastTool(request.tools, hint, budget) : request.tools;
|
|
124
|
+
const system = policy.system ? markSystemBoundaries(request.system, hint, budget) : request.system;
|
|
125
|
+
const messages = policy.messages ? markMessages(request.messages, policy.messages, hint, budget) : request.messages;
|
|
126
|
+
if (tools === request.tools && system === request.system && messages === request.messages)
|
|
127
|
+
return request;
|
|
128
|
+
return LLMRequest.update(request, { tools, system, messages });
|
|
129
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Context, Effect, Layer } from "effect";
|
|
2
|
+
import { RequestExecutor } from "./route/executor.js";
|
|
3
|
+
import { type AIError } from "./schema/index.js";
|
|
4
|
+
import type { ImageOptions, ImageRequestFor, ImageResponse } from "./image.js";
|
|
5
|
+
export type Execute = RequestExecutor.Interface["execute"];
|
|
6
|
+
export interface Interface {
|
|
7
|
+
readonly generate: <Options extends ImageOptions>(request: ImageRequestFor<Options>) => Effect.Effect<ImageResponse, AIError>;
|
|
8
|
+
}
|
|
9
|
+
declare const Service_base: Context.ServiceClass<Service, "@opencode/ImageClient", Interface>;
|
|
10
|
+
export declare class Service extends Service_base {
|
|
11
|
+
}
|
|
12
|
+
export declare const generate: <Options extends ImageOptions>(request: ImageRequestFor<Options>) => Effect.Effect<ImageResponse, AIError, Service>;
|
|
13
|
+
export declare const layer: Layer.Layer<Service, never, RequestExecutor.Service>;
|
|
14
|
+
export declare const ImageClient: {
|
|
15
|
+
readonly Service: typeof Service;
|
|
16
|
+
readonly layer: Layer.Layer<Service, never, RequestExecutor.Service>;
|
|
17
|
+
readonly generate: <Options extends ImageOptions>(request: ImageRequestFor<Options>) => Effect.Effect<ImageResponse, AIError, Service>;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Context, Effect, Layer } from "effect";
|
|
2
|
+
import { RequestExecutor } from "./route/executor.js";
|
|
3
|
+
import { mergeHttpOptions } from "./schema/index.js";
|
|
4
|
+
import { sanitizeSurrogates } from "./utils/sanitize.js";
|
|
5
|
+
export class Service extends Context.Service()("@opencode/ImageClient") {
|
|
6
|
+
}
|
|
7
|
+
export const generate = (request) => Effect.gen(function* () {
|
|
8
|
+
const client = yield* Service;
|
|
9
|
+
return yield* client.generate(request);
|
|
10
|
+
});
|
|
11
|
+
export const layer = Layer.effect(Service, Effect.gen(function* () {
|
|
12
|
+
const executor = yield* RequestExecutor.Service;
|
|
13
|
+
return Service.of({
|
|
14
|
+
generate: (request) => request.model.route.generate({
|
|
15
|
+
...sanitizeSurrogates({
|
|
16
|
+
...request,
|
|
17
|
+
model: undefined,
|
|
18
|
+
http: mergeHttpOptions(request.model.http, request.http),
|
|
19
|
+
}),
|
|
20
|
+
model: request.model,
|
|
21
|
+
}, executor.execute),
|
|
22
|
+
});
|
|
23
|
+
}));
|
|
24
|
+
export const ImageClient = {
|
|
25
|
+
Service,
|
|
26
|
+
layer,
|
|
27
|
+
generate,
|
|
28
|
+
};
|
package/dist/image.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { HttpOptions, AIError, ModelID, ProviderID, Usage } from "./schema/index.js";
|
|
3
|
+
import { Service, type Execute as ImageExecute } from "./image-client.js";
|
|
4
|
+
export interface ImageRoute<Options extends ImageOptions = ImageOptions> {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly generate: (request: ImageRequestFor<Options>, execute: ImageExecute) => Effect.Effect<ImageResponse, AIError>;
|
|
7
|
+
}
|
|
8
|
+
export type ImageOptions = Record<string, unknown>;
|
|
9
|
+
export declare class ImageModel<Options extends ImageOptions = ImageOptions> {
|
|
10
|
+
protected readonly _Options: (options: Options) => Options;
|
|
11
|
+
readonly id: ModelID;
|
|
12
|
+
readonly provider: ProviderID;
|
|
13
|
+
readonly route: ImageRoute<Options>;
|
|
14
|
+
readonly http?: HttpOptions;
|
|
15
|
+
constructor(input: ImageModel.Input<Options>);
|
|
16
|
+
static make<Options extends ImageOptions = ImageOptions>(input: ImageModel.MakeInput<Options>): ImageModel<Options>;
|
|
17
|
+
}
|
|
18
|
+
export declare namespace ImageModel {
|
|
19
|
+
interface Input<Options extends ImageOptions = ImageOptions> {
|
|
20
|
+
readonly id: ModelID;
|
|
21
|
+
readonly provider: ProviderID;
|
|
22
|
+
readonly route: ImageRoute<Options>;
|
|
23
|
+
readonly http?: HttpOptions;
|
|
24
|
+
}
|
|
25
|
+
interface MakeInput<Options extends ImageOptions = ImageOptions> extends Omit<Input<Options>, "id" | "provider"> {
|
|
26
|
+
readonly id: string | ModelID;
|
|
27
|
+
readonly provider: string | ProviderID;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export declare const ImageModelSchema: Schema.declare<ImageModel<ImageOptions>, ImageModel<ImageOptions>>;
|
|
31
|
+
export declare const ImageInputSchema: Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
|
|
32
|
+
readonly type: Schema.Literal<"bytes">;
|
|
33
|
+
readonly data: Schema.Uint8Array;
|
|
34
|
+
readonly mediaType: Schema.String;
|
|
35
|
+
}>, Schema.Struct<{
|
|
36
|
+
readonly type: Schema.Literal<"url">;
|
|
37
|
+
readonly url: Schema.String;
|
|
38
|
+
}>, Schema.Struct<{
|
|
39
|
+
readonly type: Schema.Literal<"file-id">;
|
|
40
|
+
readonly id: Schema.String;
|
|
41
|
+
}>, Schema.Struct<{
|
|
42
|
+
readonly type: Schema.Literal<"file-uri">;
|
|
43
|
+
readonly uri: Schema.String;
|
|
44
|
+
readonly mediaType: Schema.String;
|
|
45
|
+
}>]>;
|
|
46
|
+
export type ImageInput = Schema.Schema.Type<typeof ImageInputSchema>;
|
|
47
|
+
export declare const ImageInput: {
|
|
48
|
+
readonly bytes: (data: Uint8Array, mediaType: string) => ImageInput;
|
|
49
|
+
readonly url: (url: string) => ImageInput;
|
|
50
|
+
readonly file: (id: string) => ImageInput;
|
|
51
|
+
readonly fileUri: (uri: string, mediaType: string) => ImageInput;
|
|
52
|
+
};
|
|
53
|
+
declare const ImageRequest_base: Schema.Class<ImageRequest, Schema.Struct<{
|
|
54
|
+
readonly model: Schema.declare<ImageModel<ImageOptions>, ImageModel<ImageOptions>>;
|
|
55
|
+
readonly prompt: Schema.String;
|
|
56
|
+
readonly images: Schema.optional<Schema.$Array<Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
|
|
57
|
+
readonly type: Schema.Literal<"bytes">;
|
|
58
|
+
readonly data: Schema.Uint8Array;
|
|
59
|
+
readonly mediaType: Schema.String;
|
|
60
|
+
}>, Schema.Struct<{
|
|
61
|
+
readonly type: Schema.Literal<"url">;
|
|
62
|
+
readonly url: Schema.String;
|
|
63
|
+
}>, Schema.Struct<{
|
|
64
|
+
readonly type: Schema.Literal<"file-id">;
|
|
65
|
+
readonly id: Schema.String;
|
|
66
|
+
}>, Schema.Struct<{
|
|
67
|
+
readonly type: Schema.Literal<"file-uri">;
|
|
68
|
+
readonly uri: Schema.String;
|
|
69
|
+
readonly mediaType: Schema.String;
|
|
70
|
+
}>]>>>;
|
|
71
|
+
readonly options: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
72
|
+
readonly http: Schema.optional<typeof HttpOptions>;
|
|
73
|
+
}>, {}>;
|
|
74
|
+
export declare class ImageRequest extends ImageRequest_base {
|
|
75
|
+
protected readonly _ImageRequest: void;
|
|
76
|
+
}
|
|
77
|
+
export type ImageRequestFor<Options extends ImageOptions = ImageOptions> = Omit<ImageRequest, "model" | "options"> & {
|
|
78
|
+
readonly model: ImageModel<Options>;
|
|
79
|
+
readonly options?: Options;
|
|
80
|
+
};
|
|
81
|
+
export type ImageModelOptions<Model> = Model extends ImageModel<infer Options> ? Options : never;
|
|
82
|
+
export type ImageRequestInput<Model extends object = ImageModel> = Omit<ConstructorParameters<typeof ImageRequest>[0], "model" | "options" | "http"> & {
|
|
83
|
+
readonly model: Model;
|
|
84
|
+
readonly options?: NoInfer<ImageModelOptions<Model>>;
|
|
85
|
+
readonly http?: HttpOptions.Input;
|
|
86
|
+
} & (Model extends ImageModel<ImageModelOptions<Model>> ? unknown : never);
|
|
87
|
+
declare const GeneratedImage_base: Schema.Class<GeneratedImage, Schema.Struct<{
|
|
88
|
+
readonly mediaType: Schema.String;
|
|
89
|
+
readonly data: Schema.Union<readonly [Schema.String, Schema.Uint8Array]>;
|
|
90
|
+
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
91
|
+
}>, {}>;
|
|
92
|
+
export declare class GeneratedImage extends GeneratedImage_base {
|
|
93
|
+
}
|
|
94
|
+
declare const ImageResponse_base: Schema.Class<ImageResponse, Schema.Struct<{
|
|
95
|
+
readonly images: Schema.$Array<typeof GeneratedImage>;
|
|
96
|
+
readonly usage: Schema.optional<typeof Usage>;
|
|
97
|
+
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
98
|
+
}>, {}>;
|
|
99
|
+
export declare class ImageResponse extends ImageResponse_base {
|
|
100
|
+
get image(): GeneratedImage;
|
|
101
|
+
}
|
|
102
|
+
export declare function request<const Model extends object>(input: ImageRequestInput<Model>): ImageRequestFor<ImageModelOptions<Model>>;
|
|
103
|
+
export declare function request(input: ImageRequest): ImageRequest;
|
|
104
|
+
export declare function generate<const Model extends object>(input: ImageRequestInput<Model>): Effect.Effect<ImageResponse, AIError, Service>;
|
|
105
|
+
export declare function generate(input: ImageRequest): Effect.Effect<ImageResponse, AIError, Service>;
|
|
106
|
+
export declare const Image: {
|
|
107
|
+
readonly request: typeof request;
|
|
108
|
+
readonly generate: typeof generate;
|
|
109
|
+
};
|
|
110
|
+
export {};
|
package/dist/image.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { HttpOptions, InvalidRequestError, AIError, ModelID, ProviderID, ProviderMetadata, Usage, } from "./schema/index.js";
|
|
3
|
+
import { ImageClient, Service } from "./image-client.js";
|
|
4
|
+
export class ImageModel {
|
|
5
|
+
id;
|
|
6
|
+
provider;
|
|
7
|
+
route;
|
|
8
|
+
http;
|
|
9
|
+
constructor(input) {
|
|
10
|
+
this.id = input.id;
|
|
11
|
+
this.provider = input.provider;
|
|
12
|
+
this.route = input.route;
|
|
13
|
+
this.http = input.http;
|
|
14
|
+
}
|
|
15
|
+
static make(input) {
|
|
16
|
+
return new ImageModel({
|
|
17
|
+
id: ModelID.make(input.id),
|
|
18
|
+
provider: ProviderID.make(input.provider),
|
|
19
|
+
route: input.route,
|
|
20
|
+
http: input.http,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export const ImageModelSchema = Schema.declare((value) => value instanceof ImageModel, {
|
|
25
|
+
expected: "Image.Model",
|
|
26
|
+
});
|
|
27
|
+
const ImageBytesInput = Schema.Struct({
|
|
28
|
+
type: Schema.Literal("bytes"),
|
|
29
|
+
data: Schema.Uint8Array,
|
|
30
|
+
mediaType: Schema.String,
|
|
31
|
+
});
|
|
32
|
+
const ImageUrlInput = Schema.Struct({
|
|
33
|
+
type: Schema.Literal("url"),
|
|
34
|
+
url: Schema.String,
|
|
35
|
+
});
|
|
36
|
+
const ImageFileIDInput = Schema.Struct({
|
|
37
|
+
type: Schema.Literal("file-id"),
|
|
38
|
+
id: Schema.String,
|
|
39
|
+
});
|
|
40
|
+
const ImageFileURIInput = Schema.Struct({
|
|
41
|
+
type: Schema.Literal("file-uri"),
|
|
42
|
+
uri: Schema.String,
|
|
43
|
+
mediaType: Schema.String,
|
|
44
|
+
});
|
|
45
|
+
export const ImageInputSchema = Schema.Union([
|
|
46
|
+
ImageBytesInput,
|
|
47
|
+
ImageUrlInput,
|
|
48
|
+
ImageFileIDInput,
|
|
49
|
+
ImageFileURIInput,
|
|
50
|
+
]).pipe(Schema.toTaggedUnion("type"));
|
|
51
|
+
export const ImageInput = {
|
|
52
|
+
bytes: (data, mediaType) => ({ type: "bytes", data, mediaType }),
|
|
53
|
+
url: (url) => ({ type: "url", url }),
|
|
54
|
+
file: (id) => ({ type: "file-id", id }),
|
|
55
|
+
fileUri: (uri, mediaType) => ({ type: "file-uri", uri, mediaType }),
|
|
56
|
+
};
|
|
57
|
+
export class ImageRequest extends Schema.Class("Image.Request")({
|
|
58
|
+
model: ImageModelSchema,
|
|
59
|
+
prompt: Schema.String,
|
|
60
|
+
images: Schema.optional(Schema.Array(ImageInputSchema)),
|
|
61
|
+
options: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
62
|
+
http: Schema.optional(HttpOptions),
|
|
63
|
+
}) {
|
|
64
|
+
}
|
|
65
|
+
export class GeneratedImage extends Schema.Class("Image.Generated")({
|
|
66
|
+
mediaType: Schema.String,
|
|
67
|
+
data: Schema.Union([Schema.String, Schema.Uint8Array]),
|
|
68
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
69
|
+
}) {
|
|
70
|
+
}
|
|
71
|
+
export class ImageResponse extends Schema.Class("Image.Response")({
|
|
72
|
+
images: Schema.Array(GeneratedImage),
|
|
73
|
+
usage: Schema.optional(Usage),
|
|
74
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
75
|
+
}) {
|
|
76
|
+
get image() {
|
|
77
|
+
return this.images[0];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export function request(input) {
|
|
81
|
+
if (input instanceof ImageRequest)
|
|
82
|
+
return input;
|
|
83
|
+
return new ImageRequest({
|
|
84
|
+
...input,
|
|
85
|
+
model: input.model,
|
|
86
|
+
http: input.http === undefined ? undefined : HttpOptions.make(input.http),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
export function generate(input) {
|
|
90
|
+
return Effect.try({
|
|
91
|
+
try: () => (input instanceof ImageRequest ? input : request(input)),
|
|
92
|
+
catch: (error) => new AIError({
|
|
93
|
+
reason: new InvalidRequestError({
|
|
94
|
+
message: error instanceof Error ? error.message : String(error),
|
|
95
|
+
cause: error,
|
|
96
|
+
}),
|
|
97
|
+
}),
|
|
98
|
+
}).pipe(Effect.flatMap((request) => ImageClient.generate(request)));
|
|
99
|
+
}
|
|
100
|
+
export const Image = {
|
|
101
|
+
request,
|
|
102
|
+
generate,
|
|
103
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { LLMClient } from "./route/client.js";
|
|
2
|
+
export { ImageClient } from "./image-client.js";
|
|
3
|
+
export { Auth } from "./route/auth.js";
|
|
4
|
+
export { Provider } from "./provider.js";
|
|
5
|
+
export { ProviderPackage } from "./provider-package.js";
|
|
6
|
+
export { isContextOverflow, isContextOverflowFailure } from "./provider-error.js";
|
|
7
|
+
export type { RouteLanguageModelInput, RouteRoutedLanguageModelInput, Interface as LLMClientShape, Service as LLMClientService, } from "./route/client.js";
|
|
8
|
+
export * from "./schema/index.js";
|
|
9
|
+
export { GeneratedImage, ImageInput, ImageInputSchema, ImageModel, ImageRequest, ImageResponse } from "./image.js";
|
|
10
|
+
export type { ImageModelOptions, ImageOptions, ImageRequestFor, ImageRequestInput, ImageRoute } from "./image.js";
|
|
11
|
+
export { Image } from "./image.js";
|
|
12
|
+
export { Tool, ToolFailure, toDefinitions } from "./tool.js";
|
|
13
|
+
export { ToolRuntime } from "./tool-runtime.js";
|
|
14
|
+
export type { DispatchResult as ToolDispatchResult, ToolSettlement } from "./tool-runtime.js";
|
|
15
|
+
export type { AnyExecutableTool, AnyTool, ExecutableTool, ExecutableTools, Definition as ToolShape, ToolExecute, ToolExecuteContext, ToolModelOutputInput, Tools, ToolSchema, ToolToModelOutput, } from "./tool.js";
|
|
16
|
+
export * as LLM from "./llm.js";
|
|
17
|
+
export type { Definition as ProviderDefinition, LanguageModelFactory as ProviderLanguageModelFactory, LanguageModelOptions as ProviderLanguageModelOptions, } from "./provider.js";
|
|
18
|
+
export type { Definition as ProviderPackageDefinition, Settings as ProviderPackageSettings, } from "./provider-package.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { LLMClient } from "./route/client.js";
|
|
2
|
+
export { ImageClient } from "./image-client.js";
|
|
3
|
+
export { Auth } from "./route/auth.js";
|
|
4
|
+
export { Provider } from "./provider.js";
|
|
5
|
+
export { ProviderPackage } from "./provider-package.js";
|
|
6
|
+
export { isContextOverflow, isContextOverflowFailure } from "./provider-error.js";
|
|
7
|
+
export * from "./schema/index.js";
|
|
8
|
+
export { GeneratedImage, ImageInput, ImageInputSchema, ImageModel, ImageRequest, ImageResponse } from "./image.js";
|
|
9
|
+
export { Image } from "./image.js";
|
|
10
|
+
export { Tool, ToolFailure, toDefinitions } from "./tool.js";
|
|
11
|
+
export { ToolRuntime } from "./tool-runtime.js";
|
|
12
|
+
export * as LLM from "./llm.js";
|