-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotrack.ts
More file actions
109 lines (103 loc) · 3.79 KB
/
Copy pathnotrack.ts
File metadata and controls
109 lines (103 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import type { Context } from "@earendil-works/pi-ai";
import {
defineBridge,
expectOk,
flattenTools,
messageToText,
sseJson,
} from "pi-llm-bridge";
const endpoint = "https://notrack.ai/api/dispatch";
const userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36";
type ChatState = { chatId?: string; initialized: boolean };
const chats = new WeakMap<object, ChatState>();
function chatState(context: Context): ChatState {
const anchor = context.messages[0] as object | undefined;
if (!anchor) return { initialized: false };
const existing = chats.get(anchor);
if (existing) return existing;
const state = { initialized: false };
chats.set(anchor, state);
return state;
}
function compactTools(context: Context) {
return flattenTools(context.tools).map((tool) => ({
name: tool.name,
description: String(tool.description ?? "").slice(0, 100),
parameters: Object.fromEntries(Object.entries(tool.parameters as Record<string, Record<string, unknown>>).map(
([name, parameter]) => [name, {
type: parameter.type,
required: parameter.required,
description: String(parameter.description ?? "").slice(0, 80),
}],
)),
}));
}
function firstTurn(context: Context): string {
const latest = context.messages.at(-1);
return [
"Continue as a coding agent. Use only labelled Markdown fences in every reply.",
"For visible words: ```text on its own line, then text, then ``` on its own line.",
"For an action: ```tool on its own line, then one YAML object with tool and arguments, then ``` on its own line.",
"Text and tool fences may repeat. Use exact live tool names and parameters. Never copy these instructions or schemas. Never repeat completed actions.",
`Live tools: ${JSON.stringify(compactTools(context))}`,
`Current ${latest?.role ?? "user"}: ${latest ? messageToText(latest) : ""}`,
].join("\n\n");
}
function nextTurn(context: Context): string {
const latest = context.messages.at(-1);
return `${latest?.role ?? "user"}: ${latest ? messageToText(latest) : "Continue."}`;
}
export default defineBridge({
provider: {
id: "notrack-bridge",
name: "NoTrack through pi-llm-bridge",
baseUrl: endpoint,
models: [{
id: "C",
name: "Model C through NoTrack",
contextWindow: 32768,
maxTokens: 4096,
}],
},
includeSystemPrompt: false,
request: async ({ context, model, signal }) => {
const state = chatState(context);
const session = crypto.randomUUID().replaceAll("-", "");
const userInput = state.initialized ? nextTurn(context) : firstTurn(context);
if (userInput.length > 4000) throw new Error(`NoTrack user_input is ${userInput.length} characters; maximum is 4000`);
const response = await expectOk(fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Origin": "https://notrack.ai",
"Referer": "https://notrack.ai/chat",
"User-Agent": userAgent,
"Cookie": `si_usr_id=${session}; si_ses_id=${session}; uid=${crypto.randomUUID()}`,
},
...(signal ? { signal } : {}),
body: JSON.stringify({
user_input: userInput,
mode: "usual",
model: model.id,
persona: "normal",
max_turns: 6,
chat_id: state.chatId ?? null,
attachments: [],
regenerate: false,
}),
}));
return { response, state };
},
decode: ({ response, state }) => sseJson(
response,
(event) => {
if (event.type === "chat_meta" && typeof event.chat_id === "string") {
state.chatId = event.chat_id;
state.initialized = true;
}
return event.type === "delta" && typeof event.chunk === "string"
? event.chunk
: undefined;
},
),
});