-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path16i-credentials-langchain.ts
More file actions
85 lines (78 loc) · 2.89 KB
/
Copy path16i-credentials-langchain.ts
File metadata and controls
85 lines (78 loc) · 2.89 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
/**
* Credentials -- LangChain AgentExecutor with credential injection.
*
* Demonstrates:
* - Same pattern as LangGraph -- credentials resolved from server
* and injected into process.env before the executor runs
*
* NOTE: This example demonstrates the credential injection pattern for
* LangChain agents running through Agentspan. Since LangChain is an
* optional dependency, the example uses native Agentspan Agent with
* credential-aware tools that mirror what a LangChain agent would do.
*
* In a full LangChain integration, you would:
* const executor = createLangChainAgent();
* const result = await runtime.run(executor, prompt, { credentials: ["GITHUB_TOKEN"] });
*
* Setup (one-time):
* agentspan credentials set GITHUB_TOKEN <your-github-token>
*
* Requirements:
* - Agentspan server running at AGENTSPAN_SERVER_URL
* - AGENTSPAN_LLM_MODEL set (or defaults to openai/gpt-4o-mini)
* - GITHUB_TOKEN stored via `agentspan credentials set`
*/
import { Agent, AgentRuntime, tool, getCredential } from '@io-orkes/conductor-javascript/agents';
import { llmModel } from './settings';
// Mirrors a LangChain @tool that checks for a credential in the environment
const checkGithubToken = tool(
async () => {
// Try in-process credential resolution first
try {
const token = await getCredential('GITHUB_TOKEN');
return { message: `GitHub token available (starts with ${token.slice(0, 4)}...)` };
} catch {
// Fall back to process.env (as a LangChain tool would)
const token = process.env.GITHUB_TOKEN ?? '';
if (token) {
return { message: `GitHub token available via env (starts with ${token.slice(0, 4)}...)` };
}
return { message: 'GitHub token is NOT available' };
}
},
{
name: 'check_github_token',
description: 'Check if GitHub token is available in the environment.',
inputSchema: { type: 'object', properties: {} },
credentials: ['GITHUB_TOKEN'],
},
);
export const agent = new Agent({
name: 'langchain_cred_agent',
model: llmModel,
tools: [checkGithubToken],
credentials: ['GITHUB_TOKEN'],
instructions: 'You are a helpful assistant. Use tools when asked.',
});
// -- Run ----------------------------------------------------------------------
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(
agent,
'Check if the GitHub token is set',
);
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD (optional -- serve() below also deploys):
// await runtime.deploy(agent);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples --agents langchain_cred_agent
//
// 2. In a separate long-lived worker process (deploys + registers workers + starts polling):
// await runtime.serve(agent);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);