@aexol/spectral 0.7.1 → 0.7.5
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/CHANGELOG.md +5 -0
- package/dist/agent/agents.js +1 -1
- package/dist/agent/index.js +199 -184
- package/dist/commands/serve.js +0 -3
- package/dist/designer/data/systems/renault/DESIGN.md +1 -1
- package/dist/designer/philosophies.js +668 -0
- package/dist/mcp/sampling-handler.js +1 -1
- package/dist/memory/commands/status.js +1 -1
- package/dist/memory/compaction.js +2 -2
- package/dist/memory/config.js +1 -1
- package/dist/memory/debug-log.js +1 -1
- package/dist/memory/hooks/compaction-hook.js +29 -0
- package/dist/memory/index.js +2 -0
- package/dist/memory/observer.js +2 -2
- package/dist/memory/project-observations-store.js +14 -0
- package/dist/memory/tokens.js +1 -1
- package/dist/memory/tools/read-project-observations.js +82 -0
- package/dist/memory/tools/recall-observation.js +2 -2
- package/dist/pi/agent-core/agent-loop.js +501 -0
- package/dist/pi/agent-core/agent.js +401 -0
- package/dist/pi/agent-core/harness/agent-harness.js +899 -0
- package/dist/pi/agent-core/harness/compaction/branch-summarization.js +173 -0
- package/dist/pi/agent-core/harness/compaction/compaction.js +532 -0
- package/dist/pi/agent-core/harness/compaction/utils.js +130 -0
- package/dist/pi/agent-core/harness/env/nodejs.js +485 -0
- package/dist/pi/agent-core/harness/messages.js +101 -0
- package/dist/pi/agent-core/harness/prompt-templates.js +229 -0
- package/dist/pi/agent-core/harness/session/jsonl-repo.js +100 -0
- package/dist/pi/agent-core/harness/session/jsonl-storage.js +230 -0
- package/dist/pi/agent-core/harness/session/memory-repo.js +41 -0
- package/dist/pi/agent-core/harness/session/memory-storage.js +113 -0
- package/dist/pi/agent-core/harness/session/repo-utils.js +38 -0
- package/dist/pi/agent-core/harness/session/session.js +196 -0
- package/dist/pi/agent-core/harness/session/uuid.js +49 -0
- package/dist/pi/agent-core/harness/skills.js +310 -0
- package/dist/pi/agent-core/harness/system-prompt.js +29 -0
- package/dist/pi/agent-core/harness/types.js +93 -0
- package/dist/pi/agent-core/harness/utils/shell-output.js +125 -0
- package/dist/pi/agent-core/harness/utils/truncate.js +289 -0
- package/dist/pi/agent-core/index.js +24 -0
- package/dist/pi/agent-core/node.js +2 -0
- package/dist/pi/agent-core/proxy.js +277 -0
- package/dist/pi/agent-core/types.js +1 -0
- package/dist/pi/ai/api-registry.js +43 -0
- package/dist/pi/ai/cli.js +120 -0
- package/dist/pi/ai/env-api-keys.js +169 -0
- package/dist/pi/ai/image-models.generated.js +441 -0
- package/dist/pi/ai/image-models.js +22 -0
- package/dist/pi/ai/images-api-registry.js +21 -0
- package/dist/pi/ai/images.js +13 -0
- package/dist/pi/ai/index.js +18 -0
- package/dist/pi/ai/models.generated.js +16220 -0
- package/dist/pi/ai/models.js +70 -0
- package/dist/pi/ai/oauth.js +1 -0
- package/dist/pi/ai/providers/anthropic.js +945 -0
- package/dist/pi/ai/providers/faux.js +367 -0
- package/dist/pi/ai/providers/github-copilot-headers.js +28 -0
- package/dist/pi/ai/providers/openai-completions.js +945 -0
- package/dist/pi/ai/providers/openai-prompt-cache.js +9 -0
- package/dist/pi/ai/providers/register-builtins.js +97 -0
- package/dist/pi/ai/providers/simple-options.js +40 -0
- package/dist/pi/ai/providers/transform-messages.js +183 -0
- package/dist/pi/ai/session-resources.js +21 -0
- package/dist/pi/ai/stream.js +26 -0
- package/dist/pi/ai/types.js +1 -0
- package/dist/pi/ai/utils/diagnostics.js +24 -0
- package/dist/pi/ai/utils/event-stream.js +80 -0
- package/dist/pi/ai/utils/hash.js +13 -0
- package/dist/pi/ai/utils/headers.js +7 -0
- package/dist/pi/ai/utils/json-parse.js +112 -0
- package/dist/pi/ai/utils/node-http-proxy.js +96 -0
- package/dist/pi/ai/utils/oauth/anthropic.js +334 -0
- package/dist/pi/ai/utils/oauth/device-code.js +54 -0
- package/dist/pi/ai/utils/oauth/github-copilot.js +270 -0
- package/dist/pi/ai/utils/oauth/index.js +121 -0
- package/dist/pi/ai/utils/oauth/oauth-page.js +104 -0
- package/dist/pi/ai/utils/oauth/openai-codex.js +384 -0
- package/dist/pi/ai/utils/oauth/pkce.js +30 -0
- package/dist/pi/ai/utils/oauth/types.js +1 -0
- package/dist/pi/ai/utils/overflow.js +150 -0
- package/dist/pi/ai/utils/sanitize-unicode.js +25 -0
- package/dist/pi/ai/utils/typebox-helpers.js +20 -0
- package/dist/pi/ai/utils/validation.js +280 -0
- package/dist/pi/coding-agent/bun/cli.js +7 -0
- package/dist/pi/coding-agent/bun/restore-sandbox-env.js +31 -0
- package/dist/pi/coding-agent/cli/args.js +340 -0
- package/dist/pi/coding-agent/cli/file-processor.js +82 -0
- package/dist/pi/coding-agent/cli/initial-message.js +21 -0
- package/dist/pi/coding-agent/cli.js +17 -0
- package/dist/pi/coding-agent/config.js +414 -0
- package/dist/pi/coding-agent/core/agent-session-runtime.js +299 -0
- package/dist/pi/coding-agent/core/agent-session-services.js +117 -0
- package/dist/pi/coding-agent/core/agent-session.js +2498 -0
- package/dist/pi/coding-agent/core/auth-guidance.js +20 -0
- package/dist/pi/coding-agent/core/auth-storage.js +441 -0
- package/dist/pi/coding-agent/core/bash-executor.js +110 -0
- package/dist/pi/coding-agent/core/compaction/branch-summarization.js +242 -0
- package/dist/pi/coding-agent/core/compaction/compaction.js +624 -0
- package/dist/pi/coding-agent/core/compaction/index.js +6 -0
- package/dist/pi/coding-agent/core/compaction/utils.js +152 -0
- package/dist/pi/coding-agent/core/defaults.js +1 -0
- package/dist/pi/coding-agent/core/diagnostics.js +1 -0
- package/dist/pi/coding-agent/core/event-bus.js +24 -0
- package/dist/pi/coding-agent/core/exec.js +74 -0
- package/dist/pi/coding-agent/core/export-html/ansi-to-html.js +248 -0
- package/dist/pi/coding-agent/core/export-html/index.js +225 -0
- package/dist/pi/coding-agent/core/export-html/tool-renderer.js +107 -0
- package/dist/pi/coding-agent/core/extensions/index.js +8 -0
- package/dist/pi/coding-agent/core/extensions/loader.js +485 -0
- package/dist/pi/coding-agent/core/extensions/runner.js +824 -0
- package/dist/pi/coding-agent/core/extensions/types.js +44 -0
- package/dist/pi/coding-agent/core/extensions/wrapper.js +21 -0
- package/dist/pi/coding-agent/core/footer-data-provider.js +309 -0
- package/dist/pi/coding-agent/core/http-dispatcher.js +47 -0
- package/dist/pi/coding-agent/core/index.js +11 -0
- package/dist/pi/coding-agent/core/keybindings.js +294 -0
- package/dist/pi/coding-agent/core/messages.js +122 -0
- package/dist/pi/coding-agent/core/model-registry.js +728 -0
- package/dist/pi/coding-agent/core/model-resolver.js +494 -0
- package/dist/pi/coding-agent/core/output-guard.js +58 -0
- package/dist/pi/coding-agent/core/package-manager.js +2020 -0
- package/dist/pi/coding-agent/core/prompt-templates.js +237 -0
- package/dist/pi/coding-agent/core/provider-display-names.js +32 -0
- package/dist/pi/coding-agent/core/resolve-config-value.js +125 -0
- package/dist/pi/coding-agent/core/resource-loader.js +733 -0
- package/dist/pi/coding-agent/core/sdk.js +282 -0
- package/dist/pi/coding-agent/core/session-cwd.js +37 -0
- package/dist/pi/coding-agent/core/session-manager.js +1146 -0
- package/dist/pi/coding-agent/core/settings-manager.js +794 -0
- package/dist/pi/coding-agent/core/skills.js +386 -0
- package/dist/pi/coding-agent/core/slash-commands.js +24 -0
- package/dist/pi/coding-agent/core/source-info.js +18 -0
- package/dist/pi/coding-agent/core/system-prompt.js +122 -0
- package/dist/pi/coding-agent/core/telemetry.js +8 -0
- package/dist/pi/coding-agent/core/timings.js +30 -0
- package/dist/pi/coding-agent/core/tools/bash.js +341 -0
- package/dist/pi/coding-agent/core/tools/edit-diff.js +344 -0
- package/dist/pi/coding-agent/core/tools/edit.js +324 -0
- package/dist/pi/coding-agent/core/tools/file-mutation-queue.js +36 -0
- package/dist/pi/coding-agent/core/tools/find.js +297 -0
- package/dist/pi/coding-agent/core/tools/grep.js +303 -0
- package/dist/pi/coding-agent/core/tools/index.js +111 -0
- package/dist/pi/coding-agent/core/tools/ls.js +168 -0
- package/dist/pi/coding-agent/core/tools/output-accumulator.js +183 -0
- package/dist/pi/coding-agent/core/tools/path-utils.js +61 -0
- package/dist/pi/coding-agent/core/tools/read.js +288 -0
- package/dist/pi/coding-agent/core/tools/render-utils.js +48 -0
- package/dist/pi/coding-agent/core/tools/tool-definition-wrapper.js +33 -0
- package/dist/pi/coding-agent/core/tools/truncate.js +214 -0
- package/dist/pi/coding-agent/core/tools/write.js +212 -0
- package/dist/pi/coding-agent/index.js +41 -0
- package/dist/pi/coding-agent/main.js +5 -0
- package/dist/pi/coding-agent/migrations.js +280 -0
- package/dist/pi/coding-agent/modes/index.js +7 -0
- package/dist/pi/coding-agent/modes/interactive/components/diff.js +132 -0
- package/dist/pi/coding-agent/modes/interactive/components/keybinding-hints.js +35 -0
- package/dist/pi/coding-agent/modes/interactive/components/visual-truncate.js +32 -0
- package/dist/pi/coding-agent/modes/interactive/interactive-mode.js +3 -0
- package/dist/pi/coding-agent/modes/interactive/theme/theme.js +1023 -0
- package/dist/pi/coding-agent/modes/print-mode.js +130 -0
- package/dist/pi/coding-agent/modes/rpc/jsonl.js +48 -0
- package/dist/pi/coding-agent/modes/rpc/rpc-client.js +409 -0
- package/dist/pi/coding-agent/modes/rpc/rpc-mode.js +600 -0
- package/dist/pi/coding-agent/modes/rpc/rpc-types.js +7 -0
- package/dist/pi/coding-agent/utils/ansi.js +51 -0
- package/dist/pi/coding-agent/utils/changelog.js +86 -0
- package/dist/pi/coding-agent/utils/child-process.js +87 -0
- package/dist/pi/coding-agent/utils/clipboard-image.js +244 -0
- package/dist/pi/coding-agent/utils/clipboard-native.js +13 -0
- package/dist/pi/coding-agent/utils/clipboard.js +116 -0
- package/dist/pi/coding-agent/utils/exif-orientation.js +157 -0
- package/dist/pi/coding-agent/utils/frontmatter.js +25 -0
- package/dist/pi/coding-agent/utils/fs-watch.js +24 -0
- package/dist/pi/coding-agent/utils/git.js +162 -0
- package/dist/pi/coding-agent/utils/html.js +39 -0
- package/dist/pi/coding-agent/utils/image-convert.js +38 -0
- package/dist/pi/coding-agent/utils/image-resize.js +136 -0
- package/dist/pi/coding-agent/utils/mime.js +68 -0
- package/dist/pi/coding-agent/utils/paths.js +91 -0
- package/dist/pi/coding-agent/utils/photon.js +120 -0
- package/dist/pi/coding-agent/utils/pi-user-agent.js +4 -0
- package/dist/pi/coding-agent/utils/shell.js +194 -0
- package/dist/pi/coding-agent/utils/sleep.js +16 -0
- package/dist/pi/coding-agent/utils/syntax-highlight.js +117 -0
- package/dist/pi/coding-agent/utils/tools-manager.js +327 -0
- package/dist/pi/coding-agent/utils/version-check.js +81 -0
- package/dist/pi/coding-agent/utils/windows-self-update.js +76 -0
- package/dist/pi/tui/autocomplete.js +631 -0
- package/dist/pi/tui/components/box.js +103 -0
- package/dist/pi/tui/components/cancellable-loader.js +34 -0
- package/dist/pi/tui/components/editor.js +1915 -0
- package/dist/pi/tui/components/image.js +88 -0
- package/dist/pi/tui/components/input.js +425 -0
- package/dist/pi/tui/components/loader.js +68 -0
- package/dist/pi/tui/components/markdown.js +633 -0
- package/dist/pi/tui/components/select-list.js +158 -0
- package/dist/pi/tui/components/settings-list.js +184 -0
- package/dist/pi/tui/components/spacer.js +22 -0
- package/dist/pi/tui/components/text.js +88 -0
- package/dist/pi/tui/components/truncated-text.js +50 -0
- package/dist/pi/tui/editor-component.js +1 -0
- package/dist/pi/tui/fuzzy.js +109 -0
- package/dist/pi/tui/index.js +31 -0
- package/dist/pi/tui/keybindings.js +173 -0
- package/dist/pi/tui/keys.js +1172 -0
- package/dist/pi/tui/kill-ring.js +43 -0
- package/dist/pi/tui/stdin-buffer.js +360 -0
- package/dist/pi/tui/terminal-image.js +335 -0
- package/dist/pi/tui/terminal.js +324 -0
- package/dist/pi/tui/tui.js +1076 -0
- package/dist/pi/tui/undo-stack.js +24 -0
- package/dist/pi/tui/utils.js +1016 -0
- package/dist/relay/dispatcher.js +30 -0
- package/dist/server/handlers/queue.js +52 -0
- package/dist/server/pi-bridge.js +9 -1
- package/dist/server/session-stream.js +76 -111
- package/dist/server/storage.js +154 -2
- package/dist/server/title-generator.js +14 -153
- package/package.json +24 -6
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import { registerApiProvider, unregisterApiProviders } from "../api-registry.js";
|
|
2
|
+
import { createAssistantMessageEventStream } from "../utils/event-stream.js";
|
|
3
|
+
const DEFAULT_API = "faux";
|
|
4
|
+
const DEFAULT_PROVIDER = "faux";
|
|
5
|
+
const DEFAULT_MODEL_ID = "faux-1";
|
|
6
|
+
const DEFAULT_MODEL_NAME = "Faux Model";
|
|
7
|
+
const DEFAULT_BASE_URL = "http://localhost:0";
|
|
8
|
+
const DEFAULT_MIN_TOKEN_SIZE = 3;
|
|
9
|
+
const DEFAULT_MAX_TOKEN_SIZE = 5;
|
|
10
|
+
const DEFAULT_USAGE = {
|
|
11
|
+
input: 0,
|
|
12
|
+
output: 0,
|
|
13
|
+
cacheRead: 0,
|
|
14
|
+
cacheWrite: 0,
|
|
15
|
+
totalTokens: 0,
|
|
16
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
17
|
+
};
|
|
18
|
+
export function fauxText(text) {
|
|
19
|
+
return { type: "text", text };
|
|
20
|
+
}
|
|
21
|
+
export function fauxThinking(thinking) {
|
|
22
|
+
return { type: "thinking", thinking };
|
|
23
|
+
}
|
|
24
|
+
export function fauxToolCall(name, arguments_, options = {}) {
|
|
25
|
+
return {
|
|
26
|
+
type: "toolCall",
|
|
27
|
+
id: options.id ?? randomId("tool"),
|
|
28
|
+
name,
|
|
29
|
+
arguments: arguments_,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function normalizeFauxAssistantContent(content) {
|
|
33
|
+
if (typeof content === "string") {
|
|
34
|
+
return [fauxText(content)];
|
|
35
|
+
}
|
|
36
|
+
return Array.isArray(content) ? content : [content];
|
|
37
|
+
}
|
|
38
|
+
export function fauxAssistantMessage(content, options = {}) {
|
|
39
|
+
return {
|
|
40
|
+
role: "assistant",
|
|
41
|
+
content: normalizeFauxAssistantContent(content),
|
|
42
|
+
api: DEFAULT_API,
|
|
43
|
+
provider: DEFAULT_PROVIDER,
|
|
44
|
+
model: DEFAULT_MODEL_ID,
|
|
45
|
+
usage: DEFAULT_USAGE,
|
|
46
|
+
stopReason: options.stopReason ?? "stop",
|
|
47
|
+
errorMessage: options.errorMessage,
|
|
48
|
+
responseId: options.responseId,
|
|
49
|
+
timestamp: options.timestamp ?? Date.now(),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function estimateTokens(text) {
|
|
53
|
+
return Math.ceil(text.length / 4);
|
|
54
|
+
}
|
|
55
|
+
function randomId(prefix) {
|
|
56
|
+
return `${prefix}:${Date.now()}:${Math.random().toString(36).slice(2)}`;
|
|
57
|
+
}
|
|
58
|
+
function contentToText(content) {
|
|
59
|
+
if (typeof content === "string") {
|
|
60
|
+
return content;
|
|
61
|
+
}
|
|
62
|
+
return content
|
|
63
|
+
.map((block) => {
|
|
64
|
+
if (block.type === "text") {
|
|
65
|
+
return block.text;
|
|
66
|
+
}
|
|
67
|
+
return `[image:${block.mimeType}:${block.data.length}]`;
|
|
68
|
+
})
|
|
69
|
+
.join("\n");
|
|
70
|
+
}
|
|
71
|
+
function assistantContentToText(content) {
|
|
72
|
+
return content
|
|
73
|
+
.map((block) => {
|
|
74
|
+
if (block.type === "text") {
|
|
75
|
+
return block.text;
|
|
76
|
+
}
|
|
77
|
+
if (block.type === "thinking") {
|
|
78
|
+
return block.thinking;
|
|
79
|
+
}
|
|
80
|
+
return `${block.name}:${JSON.stringify(block.arguments)}`;
|
|
81
|
+
})
|
|
82
|
+
.join("\n");
|
|
83
|
+
}
|
|
84
|
+
function toolResultToText(message) {
|
|
85
|
+
return [message.toolName, ...message.content.map((block) => contentToText([block]))].join("\n");
|
|
86
|
+
}
|
|
87
|
+
function messageToText(message) {
|
|
88
|
+
if (message.role === "user") {
|
|
89
|
+
return contentToText(message.content);
|
|
90
|
+
}
|
|
91
|
+
if (message.role === "assistant") {
|
|
92
|
+
return assistantContentToText(message.content);
|
|
93
|
+
}
|
|
94
|
+
return toolResultToText(message);
|
|
95
|
+
}
|
|
96
|
+
function serializeContext(context) {
|
|
97
|
+
const parts = [];
|
|
98
|
+
if (context.systemPrompt) {
|
|
99
|
+
parts.push(`system:${context.systemPrompt}`);
|
|
100
|
+
}
|
|
101
|
+
for (const message of context.messages) {
|
|
102
|
+
parts.push(`${message.role}:${messageToText(message)}`);
|
|
103
|
+
}
|
|
104
|
+
if (context.tools?.length) {
|
|
105
|
+
parts.push(`tools:${JSON.stringify(context.tools)}`);
|
|
106
|
+
}
|
|
107
|
+
return parts.join("\n\n");
|
|
108
|
+
}
|
|
109
|
+
function commonPrefixLength(a, b) {
|
|
110
|
+
const length = Math.min(a.length, b.length);
|
|
111
|
+
let index = 0;
|
|
112
|
+
while (index < length && a[index] === b[index]) {
|
|
113
|
+
index++;
|
|
114
|
+
}
|
|
115
|
+
return index;
|
|
116
|
+
}
|
|
117
|
+
function withUsageEstimate(message, context, options, promptCache) {
|
|
118
|
+
const promptText = serializeContext(context);
|
|
119
|
+
const promptTokens = estimateTokens(promptText);
|
|
120
|
+
const outputTokens = estimateTokens(assistantContentToText(message.content));
|
|
121
|
+
let input = promptTokens;
|
|
122
|
+
let cacheRead = 0;
|
|
123
|
+
let cacheWrite = 0;
|
|
124
|
+
const sessionId = options?.sessionId;
|
|
125
|
+
if (sessionId && options?.cacheRetention !== "none") {
|
|
126
|
+
const previousPrompt = promptCache.get(sessionId);
|
|
127
|
+
if (previousPrompt) {
|
|
128
|
+
const cachedChars = commonPrefixLength(previousPrompt, promptText);
|
|
129
|
+
cacheRead = estimateTokens(previousPrompt.slice(0, cachedChars));
|
|
130
|
+
cacheWrite = estimateTokens(promptText.slice(cachedChars));
|
|
131
|
+
input = Math.max(0, promptTokens - cacheRead);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
cacheWrite = promptTokens;
|
|
135
|
+
}
|
|
136
|
+
promptCache.set(sessionId, promptText);
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
...message,
|
|
140
|
+
usage: {
|
|
141
|
+
input,
|
|
142
|
+
output: outputTokens,
|
|
143
|
+
cacheRead,
|
|
144
|
+
cacheWrite,
|
|
145
|
+
totalTokens: input + outputTokens + cacheRead + cacheWrite,
|
|
146
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function splitStringByTokenSize(text, minTokenSize, maxTokenSize) {
|
|
151
|
+
const chunks = [];
|
|
152
|
+
let index = 0;
|
|
153
|
+
while (index < text.length) {
|
|
154
|
+
const tokenSize = minTokenSize + Math.floor(Math.random() * (maxTokenSize - minTokenSize + 1));
|
|
155
|
+
const charSize = Math.max(1, tokenSize * 4);
|
|
156
|
+
chunks.push(text.slice(index, index + charSize));
|
|
157
|
+
index += charSize;
|
|
158
|
+
}
|
|
159
|
+
return chunks.length > 0 ? chunks : [""];
|
|
160
|
+
}
|
|
161
|
+
function cloneMessage(message, api, provider, modelId) {
|
|
162
|
+
const cloned = structuredClone(message);
|
|
163
|
+
return {
|
|
164
|
+
...cloned,
|
|
165
|
+
api,
|
|
166
|
+
provider,
|
|
167
|
+
model: modelId,
|
|
168
|
+
timestamp: cloned.timestamp ?? Date.now(),
|
|
169
|
+
usage: cloned.usage ?? DEFAULT_USAGE,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function createErrorMessage(error, api, provider, modelId) {
|
|
173
|
+
return {
|
|
174
|
+
role: "assistant",
|
|
175
|
+
content: [],
|
|
176
|
+
api,
|
|
177
|
+
provider,
|
|
178
|
+
model: modelId,
|
|
179
|
+
usage: DEFAULT_USAGE,
|
|
180
|
+
stopReason: "error",
|
|
181
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
182
|
+
timestamp: Date.now(),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function createAbortedMessage(partial) {
|
|
186
|
+
return {
|
|
187
|
+
...partial,
|
|
188
|
+
stopReason: "aborted",
|
|
189
|
+
errorMessage: "Request was aborted",
|
|
190
|
+
timestamp: Date.now(),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function scheduleChunk(chunk, tokensPerSecond) {
|
|
194
|
+
if (!tokensPerSecond || tokensPerSecond <= 0) {
|
|
195
|
+
return new Promise((resolve) => queueMicrotask(resolve));
|
|
196
|
+
}
|
|
197
|
+
const delayMs = (estimateTokens(chunk) / tokensPerSecond) * 1000;
|
|
198
|
+
return new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
199
|
+
}
|
|
200
|
+
async function streamWithDeltas(stream, message, minTokenSize, maxTokenSize, tokensPerSecond, signal) {
|
|
201
|
+
const partial = { ...message, content: [] };
|
|
202
|
+
if (signal?.aborted) {
|
|
203
|
+
const aborted = createAbortedMessage(partial);
|
|
204
|
+
stream.push({ type: "error", reason: "aborted", error: aborted });
|
|
205
|
+
stream.end(aborted);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
stream.push({ type: "start", partial: { ...partial } });
|
|
209
|
+
for (let index = 0; index < message.content.length; index++) {
|
|
210
|
+
if (signal?.aborted) {
|
|
211
|
+
const aborted = createAbortedMessage(partial);
|
|
212
|
+
stream.push({ type: "error", reason: "aborted", error: aborted });
|
|
213
|
+
stream.end(aborted);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const block = message.content[index];
|
|
217
|
+
if (block.type === "thinking") {
|
|
218
|
+
partial.content = [...partial.content, { type: "thinking", thinking: "" }];
|
|
219
|
+
stream.push({ type: "thinking_start", contentIndex: index, partial: { ...partial } });
|
|
220
|
+
for (const chunk of splitStringByTokenSize(block.thinking, minTokenSize, maxTokenSize)) {
|
|
221
|
+
await scheduleChunk(chunk, tokensPerSecond);
|
|
222
|
+
if (signal?.aborted) {
|
|
223
|
+
const aborted = createAbortedMessage(partial);
|
|
224
|
+
stream.push({ type: "error", reason: "aborted", error: aborted });
|
|
225
|
+
stream.end(aborted);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
partial.content[index].thinking += chunk;
|
|
229
|
+
stream.push({ type: "thinking_delta", contentIndex: index, delta: chunk, partial: { ...partial } });
|
|
230
|
+
}
|
|
231
|
+
stream.push({
|
|
232
|
+
type: "thinking_end",
|
|
233
|
+
contentIndex: index,
|
|
234
|
+
content: block.thinking,
|
|
235
|
+
partial: { ...partial },
|
|
236
|
+
});
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (block.type === "text") {
|
|
240
|
+
partial.content = [...partial.content, { type: "text", text: "" }];
|
|
241
|
+
stream.push({ type: "text_start", contentIndex: index, partial: { ...partial } });
|
|
242
|
+
for (const chunk of splitStringByTokenSize(block.text, minTokenSize, maxTokenSize)) {
|
|
243
|
+
await scheduleChunk(chunk, tokensPerSecond);
|
|
244
|
+
if (signal?.aborted) {
|
|
245
|
+
const aborted = createAbortedMessage(partial);
|
|
246
|
+
stream.push({ type: "error", reason: "aborted", error: aborted });
|
|
247
|
+
stream.end(aborted);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
partial.content[index].text += chunk;
|
|
251
|
+
stream.push({ type: "text_delta", contentIndex: index, delta: chunk, partial: { ...partial } });
|
|
252
|
+
}
|
|
253
|
+
stream.push({ type: "text_end", contentIndex: index, content: block.text, partial: { ...partial } });
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
partial.content = [...partial.content, { type: "toolCall", id: block.id, name: block.name, arguments: {} }];
|
|
257
|
+
stream.push({ type: "toolcall_start", contentIndex: index, partial: { ...partial } });
|
|
258
|
+
for (const chunk of splitStringByTokenSize(JSON.stringify(block.arguments), minTokenSize, maxTokenSize)) {
|
|
259
|
+
await scheduleChunk(chunk, tokensPerSecond);
|
|
260
|
+
if (signal?.aborted) {
|
|
261
|
+
const aborted = createAbortedMessage(partial);
|
|
262
|
+
stream.push({ type: "error", reason: "aborted", error: aborted });
|
|
263
|
+
stream.end(aborted);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
stream.push({ type: "toolcall_delta", contentIndex: index, delta: chunk, partial: { ...partial } });
|
|
267
|
+
}
|
|
268
|
+
partial.content[index].arguments = block.arguments;
|
|
269
|
+
stream.push({ type: "toolcall_end", contentIndex: index, toolCall: block, partial: { ...partial } });
|
|
270
|
+
}
|
|
271
|
+
if (message.stopReason === "error" || message.stopReason === "aborted") {
|
|
272
|
+
stream.push({ type: "error", reason: message.stopReason, error: message });
|
|
273
|
+
stream.end(message);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
stream.push({ type: "done", reason: message.stopReason, message });
|
|
277
|
+
stream.end(message);
|
|
278
|
+
}
|
|
279
|
+
export function registerFauxProvider(options = {}) {
|
|
280
|
+
const api = options.api ?? randomId(DEFAULT_API);
|
|
281
|
+
const provider = options.provider ?? DEFAULT_PROVIDER;
|
|
282
|
+
const sourceId = randomId("faux-provider");
|
|
283
|
+
const minTokenSize = Math.max(1, Math.min(options.tokenSize?.min ?? DEFAULT_MIN_TOKEN_SIZE, options.tokenSize?.max ?? DEFAULT_MAX_TOKEN_SIZE));
|
|
284
|
+
const maxTokenSize = Math.max(minTokenSize, options.tokenSize?.max ?? DEFAULT_MAX_TOKEN_SIZE);
|
|
285
|
+
let pendingResponses = [];
|
|
286
|
+
const tokensPerSecond = options.tokensPerSecond;
|
|
287
|
+
const state = { callCount: 0 };
|
|
288
|
+
const promptCache = new Map();
|
|
289
|
+
const modelDefinitions = options.models?.length
|
|
290
|
+
? options.models
|
|
291
|
+
: [
|
|
292
|
+
{
|
|
293
|
+
id: DEFAULT_MODEL_ID,
|
|
294
|
+
name: DEFAULT_MODEL_NAME,
|
|
295
|
+
reasoning: false,
|
|
296
|
+
input: ["text", "image"],
|
|
297
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
298
|
+
contextWindow: 128000,
|
|
299
|
+
maxTokens: 16384,
|
|
300
|
+
},
|
|
301
|
+
];
|
|
302
|
+
const models = modelDefinitions.map((definition) => ({
|
|
303
|
+
id: definition.id,
|
|
304
|
+
name: definition.name ?? definition.id,
|
|
305
|
+
api,
|
|
306
|
+
provider,
|
|
307
|
+
baseUrl: DEFAULT_BASE_URL,
|
|
308
|
+
reasoning: definition.reasoning ?? false,
|
|
309
|
+
input: definition.input ?? ["text", "image"],
|
|
310
|
+
cost: definition.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
311
|
+
contextWindow: definition.contextWindow ?? 128000,
|
|
312
|
+
maxTokens: definition.maxTokens ?? 16384,
|
|
313
|
+
}));
|
|
314
|
+
const stream = (requestModel, context, streamOptions) => {
|
|
315
|
+
const outer = createAssistantMessageEventStream();
|
|
316
|
+
const step = pendingResponses.shift();
|
|
317
|
+
state.callCount++;
|
|
318
|
+
queueMicrotask(async () => {
|
|
319
|
+
try {
|
|
320
|
+
await streamOptions?.onResponse?.({ status: 200, headers: {} }, requestModel);
|
|
321
|
+
if (!step) {
|
|
322
|
+
let message = createErrorMessage(new Error("No more faux responses queued"), api, provider, requestModel.id);
|
|
323
|
+
message = withUsageEstimate(message, context, streamOptions, promptCache);
|
|
324
|
+
outer.push({ type: "error", reason: "error", error: message });
|
|
325
|
+
outer.end(message);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
const resolved = typeof step === "function" ? await step(context, streamOptions, state, requestModel) : step;
|
|
329
|
+
let message = cloneMessage(resolved, api, provider, requestModel.id);
|
|
330
|
+
message = withUsageEstimate(message, context, streamOptions, promptCache);
|
|
331
|
+
await streamWithDeltas(outer, message, minTokenSize, maxTokenSize, tokensPerSecond, streamOptions?.signal);
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
const message = createErrorMessage(error, api, provider, requestModel.id);
|
|
335
|
+
outer.push({ type: "error", reason: "error", error: message });
|
|
336
|
+
outer.end(message);
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
return outer;
|
|
340
|
+
};
|
|
341
|
+
const streamSimple = (streamModel, context, streamOptions) => stream(streamModel, context, streamOptions);
|
|
342
|
+
registerApiProvider({ api, stream, streamSimple }, sourceId);
|
|
343
|
+
function getModel(requestedModelId) {
|
|
344
|
+
if (!requestedModelId) {
|
|
345
|
+
return models[0];
|
|
346
|
+
}
|
|
347
|
+
return models.find((candidate) => candidate.id === requestedModelId);
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
api,
|
|
351
|
+
models,
|
|
352
|
+
getModel,
|
|
353
|
+
state,
|
|
354
|
+
setResponses(responses) {
|
|
355
|
+
pendingResponses = [...responses];
|
|
356
|
+
},
|
|
357
|
+
appendResponses(responses) {
|
|
358
|
+
pendingResponses.push(...responses);
|
|
359
|
+
},
|
|
360
|
+
getPendingResponseCount() {
|
|
361
|
+
return pendingResponses.length;
|
|
362
|
+
},
|
|
363
|
+
unregister() {
|
|
364
|
+
unregisterApiProviders(sourceId);
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copilot expects X-Initiator to indicate whether the request is user-initiated
|
|
2
|
+
// or agent-initiated (e.g. follow-up after assistant/tool messages).
|
|
3
|
+
export function inferCopilotInitiator(messages) {
|
|
4
|
+
const last = messages[messages.length - 1];
|
|
5
|
+
return last && last.role !== "user" ? "agent" : "user";
|
|
6
|
+
}
|
|
7
|
+
// Copilot requires Copilot-Vision-Request header when sending images
|
|
8
|
+
export function hasCopilotVisionInput(messages) {
|
|
9
|
+
return messages.some((msg) => {
|
|
10
|
+
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
11
|
+
return msg.content.some((c) => c.type === "image");
|
|
12
|
+
}
|
|
13
|
+
if (msg.role === "toolResult" && Array.isArray(msg.content)) {
|
|
14
|
+
return msg.content.some((c) => c.type === "image");
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export function buildCopilotDynamicHeaders(params) {
|
|
20
|
+
const headers = {
|
|
21
|
+
"X-Initiator": inferCopilotInitiator(params.messages),
|
|
22
|
+
"Openai-Intent": "conversation-edits",
|
|
23
|
+
};
|
|
24
|
+
if (params.hasImages) {
|
|
25
|
+
headers["Copilot-Vision-Request"] = "true";
|
|
26
|
+
}
|
|
27
|
+
return headers;
|
|
28
|
+
}
|