repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | /** |
| #2 | * Context Window Management |
| #3 | * |
| #4 | * Manages the conversation history for the agent loop. |
| #5 | * Handles summarization to keep within token limits. |
| #6 | */ |
| #7 | const MAX_CONTEXT_TURNS = 20; |
| #8 | const SUMMARY_THRESHOLD = 15; |
| #9 | /** |
| #10 | * Build the message array for the next inference call. |
| #11 | * Includes system prompt + recent conversation history. |
| #12 | */ |
| #13 | export function buildContextMessages(systemPrompt, recentTurns, pendingInput) { |
| #14 | const messages = [ |
| #15 | { role: "system", content: systemPrompt }, |
| #16 | ]; |
| #17 | // Add recent turns as conversation history |
| #18 | for (const turn of recentTurns) { |
| #19 | // The turn's input (if any) as a user message |
| #20 | if (turn.input) { |
| #21 | messages.push({ |
| #22 | role: "user", |
| #23 | content: `[${turn.inputSource || "system"}] ${turn.input}`, |
| #24 | }); |
| #25 | } |
| #26 | // The agent's thinking as assistant message |
| #27 | if (turn.thinking) { |
| #28 | const msg = { |
| #29 | role: "assistant", |
| #30 | content: turn.thinking, |
| #31 | }; |
| #32 | // If there were tool calls, include them |
| #33 | if (turn.toolCalls.length > 0) { |
| #34 | msg.tool_calls = turn.toolCalls.map((tc) => ({ |
| #35 | id: tc.id, |
| #36 | type: "function", |
| #37 | function: { |
| #38 | name: tc.name, |
| #39 | arguments: JSON.stringify(tc.arguments), |
| #40 | }, |
| #41 | })); |
| #42 | } |
| #43 | messages.push(msg); |
| #44 | // Add tool results |
| #45 | for (const tc of turn.toolCalls) { |
| #46 | messages.push({ |
| #47 | role: "tool", |
| #48 | content: tc.error |
| #49 | ? `Error: ${tc.error}` |
| #50 | : tc.result, |
| #51 | tool_call_id: tc.id, |
| #52 | }); |
| #53 | } |
| #54 | } |
| #55 | } |
| #56 | // Add pending input if any |
| #57 | if (pendingInput) { |
| #58 | messages.push({ |
| #59 | role: "user", |
| #60 | content: `[${pendingInput.source}] ${pendingInput.content}`, |
| #61 | }); |
| #62 | } |
| #63 | return messages; |
| #64 | } |
| #65 | /** |
| #66 | * Trim context to fit within limits. |
| #67 | * Keeps the system prompt and most recent turns. |
| #68 | */ |
| #69 | export function trimContext(turns, maxTurns = MAX_CONTEXT_TURNS) { |
| #70 | if (turns.length <= maxTurns) { |
| #71 | return turns; |
| #72 | } |
| #73 | // Keep the most recent turns |
| #74 | return turns.slice(-maxTurns); |
| #75 | } |
| #76 | /** |
| #77 | * Summarize old turns into a compact context entry. |
| #78 | * Used when context grows too large. |
| #79 | */ |
| #80 | export async function summarizeTurns(turns, inference) { |
| #81 | if (turns.length === 0) |
| #82 | return "No previous activity."; |
| #83 | const turnSummaries = turns.map((t) => { |
| #84 | const tools = t.toolCalls |
| #85 | .map((tc) => `${tc.name}(${tc.error ? "FAILED" : "ok"})`) |
| #86 | .join(", "); |
| #87 | return `[${t.timestamp}] ${t.inputSource || "self"}: ${t.thinking.slice(0, 100)}${tools ? ` | tools: ${tools}` : ""}`; |
| #88 | }); |
| #89 | // If few enough turns, just return the summaries directly |
| #90 | if (turns.length <= 5) { |
| #91 | return `Previous activity summary:\n${turnSummaries.join("\n")}`; |
| #92 | } |
| #93 | // For many turns, use inference to create a summary |
| #94 | try { |
| #95 | const response = await inference.chat([ |
| #96 | { |
| #97 | role: "system", |
| #98 | content: "Summarize the following agent activity log into a concise paragraph. Focus on: what was accomplished, what failed, current goals, and important context for the next turn.", |
| #99 | }, |
| #100 | { |
| #101 | role: "user", |
| #102 | content: turnSummaries.join("\n"), |
| #103 | }, |
| #104 | ], { |
| #105 | maxTokens: 500, |
| #106 | temperature: 0, |
| #107 | }); |
| #108 | return `Previous activity summary:\n${response.message.content}`; |
| #109 | } |
| #110 | catch { |
| #111 | // Fallback: just use the raw summaries |
| #112 | return `Previous activity summary:\n${turnSummaries.slice(-5).join("\n")}`; |
| #113 | } |
| #114 | } |
| #115 | //# sourceMappingURL=context.js.map |