@openclaw/opencode-provider 0.0.0 → 2026.8.1-beta.2

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/stream.js ADDED
@@ -0,0 +1,166 @@
1
+ import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
2
+ import { streamSimple } from "openclaw/plugin-sdk/llm";
3
+ //#region extensions/opencode/stream.ts
4
+ const WEB_SEARCH = "web_search";
5
+ const WEB_SEARCH_ALIAS = "openclaw_web_search";
6
+ function payloadFunctions(payload) {
7
+ const choice = isRecord(payload.tool_choice) ? payload.tool_choice : void 0;
8
+ return [
9
+ ...Array.isArray(payload.tools) ? payload.tools : [],
10
+ ...Array.isArray(payload.input) ? payload.input : [],
11
+ ...Array.isArray(choice?.tools) ? choice.tools : [],
12
+ choice
13
+ ].filter((item) => isRecord(item) && (item.type === "function" || item.type === "function_call") && typeof item.name === "string");
14
+ }
15
+ function rewriteDynamicRecordSchemas(payload) {
16
+ const fieldsByTool = /* @__PURE__ */ new Map();
17
+ for (const tool of payloadFunctions(payload)) {
18
+ if (tool.type !== "function" || !isRecord(tool.parameters)) continue;
19
+ const properties = tool.parameters.properties;
20
+ if (!isRecord(properties)) continue;
21
+ const fields = [];
22
+ for (const [name, schema] of Object.entries(properties)) {
23
+ if (!isRecord(schema)) continue;
24
+ const patterns = schema.patternProperties;
25
+ if (isRecord(schema.properties) && Object.keys(schema.properties).length > 0 || !isRecord(patterns) || Object.keys(patterns).length !== 1 || !Object.hasOwn(patterns, "^.*$")) continue;
26
+ const valueSchema = patterns["^.*$"];
27
+ const jsonValues = !isRecord(valueSchema) || valueSchema.type !== "string";
28
+ const description = typeof schema.description === "string" ? `${schema.description} ` : "";
29
+ properties[name] = {
30
+ ...schema,
31
+ type: "array",
32
+ description: `${description}Provide as key/value entries.${jsonValues ? " JSON-encode every value, including strings." : ""}`,
33
+ items: {
34
+ type: "object",
35
+ properties: {
36
+ key: { type: "string" },
37
+ value: jsonValues ? {
38
+ type: "string",
39
+ description: "JSON-encoded value, including JSON encoding for string values."
40
+ } : valueSchema
41
+ },
42
+ required: ["key", "value"],
43
+ additionalProperties: false
44
+ },
45
+ properties: void 0,
46
+ patternProperties: void 0,
47
+ additionalProperties: void 0,
48
+ required: void 0
49
+ };
50
+ fields.push([name, jsonValues]);
51
+ }
52
+ fieldsByTool.set(tool.name, fields);
53
+ }
54
+ return fieldsByTool;
55
+ }
56
+ function transformArguments(toolName, args, fields, toWire) {
57
+ for (const [name, jsonValues] of fields.get(toolName) ?? []) {
58
+ const value = args[name];
59
+ if (toWire) {
60
+ if (isRecord(value)) args[name] = Object.entries(value).map(([key, item]) => ({
61
+ key,
62
+ value: jsonValues || typeof item !== "string" ? JSON.stringify(item) ?? "null" : item
63
+ }));
64
+ continue;
65
+ }
66
+ if (!Array.isArray(value)) continue;
67
+ const entries = [];
68
+ const keys = /* @__PURE__ */ new Set();
69
+ let valid = true;
70
+ for (const entry of value) {
71
+ if (!isRecord(entry) || typeof entry.key !== "string" || typeof entry.value !== "string" || keys.has(entry.key)) {
72
+ valid = false;
73
+ break;
74
+ }
75
+ keys.add(entry.key);
76
+ let item = entry.value;
77
+ if (jsonValues) try {
78
+ item = JSON.parse(entry.value);
79
+ } catch {
80
+ valid = false;
81
+ break;
82
+ }
83
+ entries.push([entry.key, item]);
84
+ }
85
+ if (valid) args[name] = Object.fromEntries(entries);
86
+ }
87
+ }
88
+ function transformCall(call, state, toWire) {
89
+ if (typeof call.name !== "string") return;
90
+ let toolName = call.name;
91
+ if (!toWire && state.alias && toolName === state.alias) call.name = toolName = WEB_SEARCH;
92
+ const serialized = typeof call.arguments === "string";
93
+ try {
94
+ const args = serialized ? JSON.parse(call.arguments) : !toWire && isRecord(call.arguments) ? { ...call.arguments } : call.arguments;
95
+ if (isRecord(args)) {
96
+ transformArguments(toolName, args, state.fields, toWire);
97
+ call.arguments = serialized ? JSON.stringify(args) : args;
98
+ }
99
+ } catch {}
100
+ }
101
+ function aliasWebSearch(payload) {
102
+ const functions = payloadFunctions(payload);
103
+ const names = new Set(functions.map((item) => item.name));
104
+ if (!names.has(WEB_SEARCH)) return;
105
+ let alias = WEB_SEARCH_ALIAS;
106
+ for (let suffix = 2; names.has(alias); suffix += 1) alias = `${WEB_SEARCH_ALIAS}_${suffix}`;
107
+ for (const item of functions) if (item.name === WEB_SEARCH) item.name = alias;
108
+ return alias;
109
+ }
110
+ function restoreMessage(message, state) {
111
+ const restored = {
112
+ ...message,
113
+ content: message.content.map((block) => ({ ...block }))
114
+ };
115
+ for (const block of restored.content) if (block.type === "toolCall") transformCall(block, state, false);
116
+ return restored;
117
+ }
118
+ function restoreEvent(event, state) {
119
+ const restored = { ...event };
120
+ if ("partial" in restored && restored.partial) restored.partial = restoreMessage(restored.partial, state);
121
+ if (restored.type === "toolcall_delta") {
122
+ const call = restored.partial.content[restored.contentIndex];
123
+ if (call?.type === "toolCall" && (state.fields.get(call.name)?.length ?? 0) > 0) restored.delta = "";
124
+ } else if (restored.type === "toolcall_end") {
125
+ restored.toolCall = { ...restored.toolCall };
126
+ transformCall(restored.toolCall, state, false);
127
+ } else if (restored.type === "done") restored.message = restoreMessage(restored.message, state);
128
+ else if (restored.type === "error") restored.error = restoreMessage(restored.error, state);
129
+ return restored;
130
+ }
131
+ function wrapResponseStream(stream, state) {
132
+ return {
133
+ async *[Symbol.asyncIterator]() {
134
+ for await (const event of stream) yield restoreEvent(event, state);
135
+ },
136
+ async result() {
137
+ return restoreMessage(await stream.result(), state);
138
+ }
139
+ };
140
+ }
141
+ function wrapOpencodeProviderStream(ctx) {
142
+ const underlying = ctx.streamFn ?? streamSimple;
143
+ return (model, context, options) => {
144
+ if (model.api !== "openai-responses") return underlying(model, context, options);
145
+ const originalOnPayload = options?.onPayload;
146
+ const state = { fields: /* @__PURE__ */ new Map() };
147
+ const maybeStream = underlying(model, context, {
148
+ ...options,
149
+ async onPayload(payload, payloadModel) {
150
+ const finalPayload = await originalOnPayload?.(payload, payloadModel) ?? payload;
151
+ state.fields = /* @__PURE__ */ new Map();
152
+ state.alias = void 0;
153
+ if (isRecord(finalPayload)) {
154
+ state.fields = rewriteDynamicRecordSchemas(finalPayload);
155
+ for (const call of payloadFunctions(finalPayload)) if (call.type === "function_call") transformCall(call, state, true);
156
+ state.alias = aliasWebSearch(finalPayload);
157
+ }
158
+ return finalPayload;
159
+ }
160
+ });
161
+ const wrap = (stream) => wrapResponseStream(stream, state);
162
+ return maybeStream && typeof maybeStream === "object" && "then" in maybeStream ? Promise.resolve(maybeStream).then(wrap) : wrap(maybeStream);
163
+ };
164
+ }
165
+ //#endregion
166
+ export { wrapOpencodeProviderStream };