File size: 6,423 Bytes
64648ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type { AdapterExecutionContext, AdapterExecutionResult } from "../types.js";
import { asString, asNumber, parseObject, renderTemplate } from "../utils.js";
import { ensureSession, addMessage, commitSession, getSessionSummary, openVikingEnabled } from "./openviking-client.js";

const MAX_CHAT_ATTEMPTS = 3; // 1 initial context + up to 2 fresh-chat fallbacks
// Patient backoff for transient connection errors (e.g. flaky egress on the
// remote side) BEFORE giving up on the current chat context and starting a
// fresh one. Same-context retries preserve conversation continuity, so they
// are always preferred over a fresh chat when the failure looks transient.
const SAME_CONTEXT_RETRY_DELAYS_MS = [3000, 8000, 15000];

function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

function interpolateDeep(value: unknown, data: Record<string, unknown>): unknown {
  if (typeof value === "string") return renderTemplate(value, data);
  if (Array.isArray(value)) return value.map((v) => interpolateDeep(v, data));
  if (value && typeof value === "object") {
    return Object.fromEntries(
      Object.entries(value as Record<string, unknown>).map(([k, v]) => [k, interpolateDeep(v, data)]),
    );
  }
  return value;
}

function sessionKeyFor(ctx: AdapterExecutionContext): string {
  const taskKey = ctx.runtime.taskKey;
  return `paperclip-${ctx.agent.id}-${taskKey ?? "default"}`;
}

async function callRemote(
  url: string,
  method: string,
  headers: Record<string, string>,
  timeoutMs: number,
  body: Record<string, unknown>,
): Promise<{ status: number; ok: boolean; text: string; parsed: Record<string, unknown> | null }> {
  const controller = new AbortController();
  const timer = timeoutMs > 0 ? setTimeout(() => controller.abort(), timeoutMs) : null;
  try {
    const res = await fetch(url, {
      method,
      headers: { "content-type": "application/json", ...headers },
      body: JSON.stringify(body),
      ...(timer ? { signal: controller.signal } : {}),
    });
    const text = await res.text();
    let parsed: Record<string, unknown> | null = null;
    try {
      parsed = text ? (JSON.parse(text) as Record<string, unknown>) : null;
    } catch {
      parsed = null;
    }
    return { status: res.status, ok: res.ok, text, parsed };
  } finally {
    if (timer) clearTimeout(timer);
  }
}

export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExecutionResult> {
  const { config, runId, agent, context, runtime } = ctx;
  const url = asString(config.url, "");
  if (!url) throw new Error("HTTP adapter missing url");

  const method = asString(config.method, "POST");
  const timeoutMs = asNumber(config.timeoutMs, 0);
  const headers = parseObject(config.headers) as Record<string, string>;
  const payloadTemplate = parseObject(config.payloadTemplate);

  const templateData = {
    agentId: agent.id,
    companyId: agent.companyId,
    runId,
    company: { id: agent.companyId },
    agent,
    run: { id: runId, source: "on_demand" },
    context,
  };
  const renderedPayload = interpolateDeep(payloadTemplate, templateData) as Record<string, unknown>;
  const outgoingMessage = asString(renderedPayload.message, "");

  const sessionKey = sessionKeyFor(ctx);
  const ovOn = openVikingEnabled();
  if (ovOn) await ensureSession(sessionKey);

  const existingRemoteContext = asString(parseObject(runtime.sessionParams).openOperatorContext, "") || undefined;

  let remoteContext = existingRemoteContext;
  let chatAttempt = 0;
  let lastError: Error | null = null;
  let response: Awaited<ReturnType<typeof callRemote>> | null = null;
  let startedFreshChat = false;
  let retriedSameContext = false;

  while (chatAttempt < MAX_CHAT_ATTEMPTS && !response) {
    chatAttempt += 1;
    let messageForAttempt = outgoingMessage;

    // Hung-up fallback: the previous chat context was exhausted (all patient
    // retries on it failed). Start a brand-new remote chat (drop the old
    // context id) but seed it with a durable summary so it's not starting
    // blind.
    if (chatAttempt > 1) {
      startedFreshChat = true;
      remoteContext = undefined;
      const summary = ovOn ? await getSessionSummary(sessionKey) : null;
      if (summary) {
        messageForAttempt = `[Continuing after a dropped connection. Prior context summary:]\n${summary}\n\n[New message:]\n${outgoingMessage}`;
      }
    }

    const body: Record<string, unknown> = {
      ...renderedPayload,
      message: messageForAttempt,
      agentId: agent.id,
      runId,
      ...(remoteContext ? { context: remoteContext } : {}),
    };

    // Patient retry loop: keep hitting the SAME chat context/body a few
    // times with backoff before concluding it's actually broken. Most
    // failures we've observed are transient connection errors, not a truly
    // dead context, so retrying in place beats burning a fresh chat.
    for (let retryIdx = 0; retryIdx <= SAME_CONTEXT_RETRY_DELAYS_MS.length; retryIdx++) {
      if (retryIdx > 0) retriedSameContext = true;
      try {
        response = await callRemote(url, method, headers, timeoutMs, body);
        if (response.ok) break;
        lastError = new Error(`HTTP invoke failed with status ${response.status}: ${response.text.slice(0, 500)}`);
        response = null;
      } catch (err) {
        lastError = err instanceof Error ? err : new Error(String(err));
        response = null;
      }
      if (retryIdx < SAME_CONTEXT_RETRY_DELAYS_MS.length) {
        await sleep(SAME_CONTEXT_RETRY_DELAYS_MS[retryIdx]);
      }
    }
  }

  if (!response) {
    throw lastError ?? new Error("HTTP adapter failed after retries");
  }

  const returnedContext = asString(response.parsed?.context, "") || remoteContext || null;
  const replyText = asString(response.parsed?.message, "") || response.text;

  if (ovOn) {
    void addMessage(sessionKey, "user", outgoingMessage)
      .then(() => addMessage(sessionKey, "assistant", replyText))
      .then(() => commitSession(sessionKey))
      .catch(() => {});
  }

  return {
    exitCode: 0,
    signal: null,
    timedOut: false,
    summary: `HTTP ${method} ${url}${retriedSameContext ? " (retried same context)" : ""}${startedFreshChat ? " (new chat after hang-up)" : ""}`,
    sessionParams: returnedContext ? { openOperatorContext: returnedContext } : null,
    sessionDisplayId: returnedContext,
  };
}